I am following the instructions provided below to load a trained Tensorflow model in Java:
How to import an saved Tensorflow model train using tf.estimator and predict on input data
I successfully saved my python model and imported it in my java code.
Here is the portion of my python code which saves my model:
# Export trained model
def serving_input_receiver_fn():
inputs = {"my_feature": tf.placeholder(shape=[None, 1], dtype=tf.float32)}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
export_dir = kmeans.export_savedmodel(
export_dir_base="/tmp/path_to_model_directory",
serving_input_receiver_fn=serving_input_receiver_fn,
assets_extra={"file_name":"/tmp/path_to_file"
})
And here is the java code for reading the model, making a prediction and reading the result:
Tensor x = Tensor.create(
new long[] {1, 1},
FloatBuffer.wrap(
new float[] {
10000.0f
}));
final String xName = "inputs_tensor_info_name";
final String yName = "outputs_tensor_info_name";
List<Tensor<?>> outputTensor = session.runner()
.feed(xName, x)
.fetch(yName)
.run();
for (int i=0; i < outputTensor.size(); i++ ) {
Integer[] copy = new Integer[1];
System.out.println(outputTensor.get(i));
System.out.println(outputTensor.get(i).copyTo(copy));
}
However when trying to load the prediction in an array the following exception occurs:
Exception in thread "main" java.lang.IllegalArgumentException: cannot create non-scalar Tensors from arrays of boxed values
at org.tensorflow.Tensor.objectCompatWithType(Tensor.java:722)
at org.tensorflow.Tensor.throwExceptionIfTypeIsIncompatible(Tensor.java:742)
at org.tensorflow.Tensor.copyTo(Tensor.java:450)
at com.pivot.maven.quickstart.HelloTF.main(HelloTF.java:105)
Having failed to figure out how to go about this, I would appreciate if someone pointed me in the right direction. Thank you.