Adrian
Adrian

Reputation: 395

Configure Tensorflow on GPU Java

How to set following config "gpu_options.allow_growth = True" for Tensorflow, using Java API?

I tried in this way:

model.session().runner()
                .setOptions("gpu_options.allow_growth = True".getBytes())
                .feed("image_tensor", input).fetch("detection_scores")
                .fetch("detection_classes").fetch("detection_boxes").fetch("num_detections").run();

I get the following error: Unparseable RunOptions proto.

Upvotes: 1

Views: 743

Answers (2)

Adrian
Adrian

Reputation: 395

I think this is the way to configure:

      ConfigProto config = ConfigProto.newBuilder()
                .setGpuOptions(GPUOptions.newBuilder().setAllowGrowth(true))
                .build();

      model.session().runner()
                    .setOptions(config.toByteArray())
                    .feed("image_tensor", input).fetch("detection_scores")
                    .fetch("detection_classes").fetch("detection_boxes").fetch("num_detections").run();

Upvotes: 3

GhostCat
GhostCat

Reputation: 140427

The API documentation says:

public Session.Runner setOptions (byte[] options)

(Experimental method): set options (typically for debugging) for this run.

The options are presented as a serialized RunOptions protocol buffer.

Still looking for an exact example, but I assume: just taking a string and turning that into an array of bytes isn't what you should be doing.

These examples indicate that you need something like:

Session.Run r = runner.setOptions(RunStats.runOptions()).runAndFetchMetadata();
fetchTensors = r.outputs;

if (runStats == null) {
  runStats = new RunStats();
}

Long story short: you have to dig into RunStats to figure how to get your options in there, to then provide an object of that class to the setOptions() method.

Upvotes: 1

Related Questions