Train TF models in Python and Invoke models in Java
来源:互联网 发布:淘宝旺铺怎么装修 编辑:程序博客网 时间:2024/04/28 04:27
- Plan A
#Train in Pythonimport tensorflow as tf# good idea# https://stackoverflow.com/documentation/tensorflow/10718/save-tensorflow-model-in-python-and-load-with-java#t=201709030336395954421tf.reset_default_graph()# DO MODEL STUFF# Pretrained weighting of 2.0W = tf.get_variable('w', initializer=tf.constant(2.0), dtype=tf.float32)# Model input xx = tf.placeholder(tf.float32, name='x')# Model output y = W*xy = tf.multiply(W, x, name='y')# DO SESSION STUFFsess = tf.Session()sess.run(tf.global_variables_initializer())# SAVE THE MODELbuilder = tf.saved_model.builder.SavedModelBuilder("/tmp/model" )builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING])builder.save()
//Invoke in Javaimport org.tensorflow.SavedModelBundle;import org.tensorflow.Session;import org.tensorflow.Tensor;import org.tensorflow.TensorFlow;import java.io.IOException;import java.nio.FloatBuffer;/** * Created by apollo on 17-9-3. * https://stackoverflow.com/documentation/tensorflow/10718/save-tensorflow-model-in-python-and-load-with-java#t=201709030336395954421 */public class LoadModel { public static void main(String[] args) throws IOException { // good idea to print the version number, 1.2.0 as of this writing System.out.println(TensorFlow.version()); final int NUM_PREDICTIONS = 1; /* load the model Bundle */ SavedModelBundle b = SavedModelBundle.load("/tmp/model", "serve"); // create the session from the Bundle Session sess = b.session(); // create an input Tensor, value = 2.0f Tensor x = Tensor.create( new long[]{NUM_PREDICTIONS}, FloatBuffer.wrap(new float[]{2.0f}) ); // run the model and get the result, 4.0f. float[] y = sess.runner() .feed("x", x) .fetch("y") .run() .get(0) .copyTo(new float[NUM_PREDICTIONS]); // print out the result. System.out.println(y[0]); }}
==============================================
||||Plan B|||| {only in python , only import}
On the Python side, Tensorflow suggests to use a Saver object to save a model to disk. It creates a .meta file that has the definition and has .data files for the weights. In Python, I use new_saver=tf.train.import_meta_graph(var_filename)
new_saver.restore(sess, model_filename) to read the model from the disk.
||||Plan C|||| {only in python, only save}
tf.train.write_graph(sess.graph_def, “./data”, “aaa.pb”);
this aaa.pb contains graph and variables , not like Plan A(that pb only contain graph)
||||Plan D|||| {only in python, only save , import and perdict have error}
//https://github.com/jiegzhan/multi-class-text-classification-cnn-rnn
saver = tf.train.Saver(tf.all_variables())
error===
saver = tf.train.import_meta_graph(“{}.meta”.format(checkpoint_file[:-5]))
saver.restore(sess, checkpoint_file)
path = saver.save(sess, checkpoint_prefix, global_step=current_step)
||||Plan E||||
https://stackoverflow.com/questions/43598953/loading-sklearn-model-in-java-model-created-with-dnnclassifier-in-python
classifier = learn.DNNClassifier(hidden_units=[10, 20, 5], n_classes=5,feature_columns=feature_columns)
A model_saved.pbtxt file is created.
SavedModelBundle bundle=SavedModelBundle.load(“/java/…/ModelSave”,”serve”);
Reference Website:
http://blog.csdn.net/michael_yt/article/details/74737489
http://blog.csdn.net/lujiandong1/article/details/53385092
https://blog.metaflow.fr/tensorflow-saving-restoring-and-mixing-multiple-models-c4c94d5d7125
http://www.cnblogs.com/nowornever-L/p/6991295.html
- Train TF models in Python and Invoke models in Java
- CS 20SI: Basic Models in TF
- Metrics and Models in Software Quality Engineering, Second Edition
- Replication models in distributed systems
- Models in TensorFlow from GitHub
- High performance models in TensorFlow
- Models in TensorFlow from GitHub
- Models in TensorFlow from GitHub
- <Probabilistic Graphical Models>2 Graphical Models in Action
- Handbook of Mathematical Models in Computer Vision
- Some Shading Models in realtime rendering
- Some Shading Models in realtime rendering
- Models of Metaphor in NLP读书笔记
- Custom Lighting models in Surface Shaders
- ASP.NET-MVC4-07-Models-in-mvc
- Physically Based Deformable Models in Computer Graphics
- Security and Versioning Models in the Windows Forms Engine Help You Create and Deploy Smart Clients
- The Idea of Combining Random Matrix and Graphical Models in Machine Learning
- lua源码阅读(1)----概览
- Windows10下安装numpy和scipy(避免“ImportError: cannot import name NUMPY_MKL”)
- 网易2017编程题之前n个数的最大奇约数之和
- 文件I/O实现拷贝功能
- Session用法案例 -->实现简单购物车功能(实际项目可能不会这么使用)
- Train TF models in Python and Invoke models in Java
- JVM 垃圾回收器介绍
- 舒适的前端开发环境是怎样一种体验?
- POJ总结(3080)
- sublime 快捷键
- Ubuntu16.04将.png/.jpg图片转换为.eps/.pdf格式
- HTTP协议详解
- PAT乙级1065. 单身狗(25)
- 图片悬浮实例