The TensorFlow training model is usually written using the Python API and simply records how the models are invoked in Java after they are saved.
In Python, the model is saved using the following API:
# Save binary model
Output_graph_def = tf.graph_util.convert_variables_to_constants (Sess, Sess.graph_def, Output_node_ names=[' Y_conv_add ']
with Tf.gfile.FastGFile ('/LOGS/MNIST.PB ', mode= ' WB ') as F:
F.write (output_graph_def. Serializetostring ())
Save as a binary PB file, the main point is the output_node_names array, the name of the data represents the TensorFlow tensor name that needs to be saved. is the name of the calculation operation specified when the model is defined in Python. Fill in what is saved to what node. In the CNN model, it is usually the name of the category output.
For example, the code for the model definition is:
Y_conv = Tf.add (Tf.matmul (H_fc1_drop, W_FC2), B_FC2, name= ' Y_conv_add ') # CNN output layer, name Y_conv_add
# Training and evaluation Model
Softmax = Tf.nn.softmax_cross_entropy_with_logits (Labels=y_, Logits=y_conv)
Models in Java use the need to care about the model input tensor and output tensor names, so when defining a model, all input tensor best specify names, such as input x and dropout names.
Invoke code fragment in Java:
public static void Main (string[] args) {
String labels = "17,16,7,8,3,15,4,14,2,5,12,18,9,10,1,11,13,6";
Tensorflowinferenceinterface TFI = new Tensorflowinferenceinterface ("D:/TF_MODE/OUTPUT_GRAPH.PB", "ImageType");
Final Operation Operation = tfi.graphoperation ("Y_conv_add");
Output output = operation.output (0);
Shape shape = output.shape ();
Final int numclasses = (int) shape.size (1);
float[] floatvalues = Getimagepixel ("d:/tf_mode/ci/ci/333.jpg"); Process the picture as input corresponding tensor format
//input picture
tfi.feed ("X_input", floatvalues, 1, 2048);//copy data to input tensor x_input the X name
for the model definition Tfi.run (new string[] {"Y_conv_add"}, False);//output tensor
float[] outputs = new float[numclasses];//result classification
Tfi.fetch ("Y_conv_add", outputs)//receive the result outputs the probability of the predicted result, the biggest one is usually the forecast result}
Tensorflowinferenceinterface Reference:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/android/java/org/tensorflow/contrib/ Android/tensorflowinferenceinterface.java
Java APIs and TensorFlow dependencies:
Https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java
Call Procedure Reference:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/ Tensorflowimageclassifier.java
Note: The contents are conveniently recorded, not checked typos, omissions, etc.
This provides Python and Java invocation implementations in view of the code that many people want:
Http://pan.baidu.com/s/1mi1hklM under the background Python implementation is based on a. pb file two training, only the last layer of the classification of the Code, the project initially to solve a single Chinese character recognition problem.