tensorflow训练的模型在java中的使用
2017-09-15 00:31
405 查看
tensorflow训练模型通常使用python api编写,简单记录下这些模型保存后怎么在java中调用。
python中训练完成,模型保存使用如下api保存:
[python] view
plain copy
# 保存二进制模型
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())
保存为二进制pb文件,主要的点是output_node_names数组,该数据的名称表示需要保存的tensorflow tensor名。既是在python中定义模型时指定的计算操作的name。填写什么就保存到什么节点。在cnn模型中,通常是分类输出的名称。
例如模型定义时代码为:
[python] view
plain copy
y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='y_conv_add') # cnn输出层,名称y_conv_add
# 训练和评价模型
softmax = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)
模型在java中使用需要关心模型输入tensor和输出tensor名,所以定义模型时,所有的输入tensor最好指定名称,如输入x和dropout名。
java中调用代码片段:
[java] view
plain copy
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"); //将图片处理为输入对应张量格式
// 输入图片
tfi.feed("x_input", floatValues, 1, 2048); //将数据复制给输入张量x_input即为模型定义时的x名称
tfi.run(new String[] { "y_conv_add" }, false);//输出张量
float[] outPuts = new float[numClasses];//结果分类
tfi.fetch("y_conv_add", outPuts);//接收结果 outPuts保存的即为预测结果对应的概率,最大的一个通常为本次预测结果
[java] view
plain copy
}
TensorFlowInferenceInterface参考: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/android/java/org/tensorflow/contrib/android/TensorFlowInferenceInterface.java java api和tensorflow的依赖: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java
调用过程参考: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageClassifier.java
python中训练完成,模型保存使用如下api保存:
[python] view
plain copy
# 保存二进制模型
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())
保存为二进制pb文件,主要的点是output_node_names数组,该数据的名称表示需要保存的tensorflow tensor名。既是在python中定义模型时指定的计算操作的name。填写什么就保存到什么节点。在cnn模型中,通常是分类输出的名称。
例如模型定义时代码为:
[python] view
plain copy
y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='y_conv_add') # cnn输出层,名称y_conv_add
# 训练和评价模型
softmax = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)
模型在java中使用需要关心模型输入tensor和输出tensor名,所以定义模型时,所有的输入tensor最好指定名称,如输入x和dropout名。
java中调用代码片段:
[java] view
plain copy
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"); //将图片处理为输入对应张量格式
// 输入图片
tfi.feed("x_input", floatValues, 1, 2048); //将数据复制给输入张量x_input即为模型定义时的x名称
tfi.run(new String[] { "y_conv_add" }, false);//输出张量
float[] outPuts = new float[numClasses];//结果分类
tfi.fetch("y_conv_add", outPuts);//接收结果 outPuts保存的即为预测结果对应的概率,最大的一个通常为本次预测结果
[java] view
plain copy
}
TensorFlowInferenceInterface参考: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/android/java/org/tensorflow/contrib/android/TensorFlowInferenceInterface.java java api和tensorflow的依赖: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java
调用过程参考: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageClassifier.java
相关文章推荐
- tensorflow训练的模型在java中的使用
- tensorflow 使用多块GPU同时训练多个模型
- Tensorflow实现cnn模型的训练与使用
- 使用TensorFlow训练循环神经网络语言模型
- TensorFlow的训练模型在Android和Java的应用及调用
- Tensorflow使用笔记(1) 怎么保存和使用训练好的参数和模型
- Tensorflow实现cnn模型的训练与使用
- TensorFlow使用object detection训练自己的模型用于物体识别
- tensorflow训练好的模型中java调用
- TensorFlow - 手写数字识别 (模型训练完成后的使用)
- tensorflow中mnist 使用cnn模型训练的输出层数为7x7的原因
- Tensorflow读取并使用预训练模型:以inception_v3为例
- Stanford NER模型使用,训练自己的NER模型,终端使用和java调用
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测
- tensorflow如何使用训练好的模型做测试
- Tensorflow保存模型,恢复模型,使用训练好的模型进行预测和提取中间输出(特征)
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测(tf.graph_util.convert_variables_to_constants函数)