![](/img/trans.png)
[英]How to load and predict a pre-trained tensorflow model into Java code?
[英]Load pre-trained models in Tensorflow for Java
我正在嘗試使用Java API在Tensorflow中加載預訓練的模型 。
我注意到隨着時間的推移,已保存的模型文件的格式已更改,現在已經保存了文件格式為.pb
, .ckpt
模型以及模型目錄為model.ckpt.data-00000-of-00001 , model.ckpt.index
。
我正在遵循讀取LabelImage示例中指定的模型的方法。 但是在此示例中,文件格式為protobuf .pb
。 我看到最新保存的模型以.ckpt
或model.ckpt.data-00000-of-00001 , model.ckpt.index
格式保存。
我試圖用SavedModelBundle與方法export_dir
包含文件- model.ckpt.data-00000-of-00001
和model.ckpt.index
,但我得到這個錯誤
`2018-07-18 16:54:00.388790: I tensorflow/cc/saved_model/loader.cc:291] SavedModel load for tags { }; Status: fail. Took 95 microseconds.
Exception in thread "main" org.tensorflow.TensorFlowException: SavedModel not found in export directory: /path/to/model_dir at org.tensorflow.SavedModelBundle.load(Native Method) at org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:39)
有人可以告訴我我做錯了什么,或者讓我知道如何讀取Java中.pb
以外的文件格式保存的已保存模型。
我認為有兩種方法可以嘗試解決問題:
將保存的模型還原到當前會話后:sess,
# Freeze the graph, with output _node_names is the name of the output when construct the model
# Eg. output_node_names = ["prediction"]
frozen_graph_def = tf.graph_util.convert_variables_to_constants (sess, sess.graph_def, output_node_names)
# Save the frozen graph
with open (frozen_graph_file, "wb") as f:
f.write(frozen_graph_def.SerializeToString()
它應該將以前的格式轉換為新的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.