0
votes

I'm trying to execute a prediction on a model that I trained using "Finetuning AlexNet with TensorFlow" https://kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html

I saved the model using tf.saved_model.builder.SavedModelBuilder in Python, and loaded the model in Java using SavedModelBundle.load. the main part of the code is:

    SavedModelBundle smb = SavedModelBundle.load(path, "serve");
    Session s = smb.session();
    byte[] imageBytes = readAllBytesOrExit(Paths.get(path));
    Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes);
    Tensor result = s.runner().feed("input_tensor", image).fetch("fc8/fc8").run().get(0);
    final long[] rshape = result.shape();
    if (result.numDimensions() != 2 || rshape[0] != 1) {
        throw new RuntimeException(
                String.format(
                        "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
                        Arrays.toString(rshape)));
    }
    int nlabels = (int) rshape[1];
    float [] a =  result.copyTo(new float[1][nlabels])[0];`

I'm getting this Exception:

Exception in thread "main" java.lang.IllegalArgumentException: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float [[Node: Placeholder_1 = Placeholder_output_shapes=[[]], dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

I saw that the code above worked for some people, and I can't figure out what's missing here. Note that the net is familiar with the nodes "input_tensor" and "fc8/fc8", since it didn't say that it doesn't know them.

1

1 Answers

1
votes

From the error message, it appears that the model you're using expects to be fed another value (whose node name in the graph is Placeholder_1 and the expected type is a float scalar tensor).

It seems you've customized your model (as opposed to following the article you linked to verbatim). That said, the article shows multiple placeholders that need to be fed, one for the image and another to control dropout. Defined in the article as:

keep_prob = tf.placeholder(tf.float32)

And the value of this placeholder needs to be fed. If you're doing inference, then you want to set keep_prob to 1.0. Something like:

Tensor keep_prob = Tensor.create(1.0f);
Tensor result = s.runner()
  .feed("input_tensor", image)
  .feed("Placeholder_1", keep_prob)
  .fetch("fc8/fc8")
  .run()
  .get(0);

Hope that helps.