When both Java and Python tensorflow versions are 1.2.0, it seems we can use SavedModelBundle (Java) and Saved Model API (Python) in order to save a trained model in Python tensorflow and load the model in Java tensorflow (Not with Maven).
However, I could not find a way to properly load a model in Java when the version of Python is below 1.0.
I trained a model and saved it as .pb, .sd, and .txt files in Python tensorflow (0.9.0) and followed the example instruction in the tensorflow website to load the model. However, I got the following error:
Exception in thread "main" java.lang.IllegalStateException: Attempting
to use uninitialized value policy/mean_network/hidden_1/b
[[Node: _retval_policy/mean_network/hidden_1/b_0_0 =
_Retval[T=DT_FLOAT, index=0,
_device="/job:localhost/replica:0/task:0/cpu:0"]
(policy/mean_network/hidden_1/b)]]
at org.tensorflow.Session.run(Native Method)
at org.tensorflow.Session.access$100(Session.java:48)
at org.tensorflow.Session$Runner.runHelper(Session.java:285)
at org.tensorflow.Session$Runner.run(Session.java:235)
at Carpole.executeGraph(Carpole.java:42)
at Carpole.main(Carpole.java:30)
Does anyone know how to properly load a pretrained model in Java in the most recent version (since I cannot find previous version API anymore) without using Saved Model API?
Thanks in advance!
Here is my Python code for saving:
with tf.Session() as sess:
self.saver = tf.train.Saver(tf.all_variables())
sess.run(tf.initialize_all_variables())
…..
saver_def = self.saver.as_saver_def()
print(saver_def.filename_tensor_name)
print(saver_def.restore_op_name)
self.saver.save(sess, 'trained_model'+str(itr)+'.sd')
tf.train.write_graph(sess.graph_def, '.', 'trained_model'+str(itr)+'.pb', as_text=False)
tf.train.write_graph(sess.graph_def, '.', 'trained_model'+str(itr)+'.txt', as_text=True)
And here is my Java code
public static void main(String[] args) throws Exception {
String dataDirPath = args[0];
byte[] graphDef = readAllBytesOrExit(Paths.get(dataDirPath, "trained_model10.pb"));
List<String> labels = readAllLinesOrExit(Paths.get(dataDirPath, "trained_model10.txt"));
float[] vector = new float[4];
vector[0] = (float) -0.09341373;
vector[1] = (float) -0.07540844;
vector[2] = (float) 0.00930138;
vector[3] = (float) -0.14317159;
Tensor input = Tensor.create(vector);
float[] labelProbabilities = executeGraph(graphDef, input);
int bestLabelIdx = maxIndex(labelProbabilities);
System.out.println(String.format("BEST MATCH: %s (%.2f%% likely)",labels.get(bestLabelIdx), labelProbabilities[bestLabelIdx] * 100f));
}
private static float[] executeGraph(byte[] graphDef, Tensor input_tensor) {
try (Graph g = new Graph()) {
g.importGraphDef(graphDef);
System.out.println(g);
try (Session s = new Session(g); Tensor result = s.runner().feed("policy/mean_network/input/input",input_tensor).fetch("policy/mean_network/hidden_1/b").run().get(0)) {
final long[] rshape = result.shape();
}
int nlabels = (int) rshape[1];
return result.copyTo(new float[1][nlabels])[0];
}
}