我遵循https://tensorflow-object-detection-api-tutorial.readthedocs.io的教程,微调一个预先训练过的模型,以检测图像中的新对象。预先训练的模型是ssd_inception_v2_coco.。
在几千步之后,我成功地训练和评估了模型,损失从26步降到1步。然而,我未能用以下代码创建冻结的模型:
#this code runs in model dir
import tensorflow as tf
#make .pb file from model at step 1000
saver = tf.train.import_meta_graph(
'./model.ckpt-1000.meta', clear_devices=True)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
saver.restore(sess, "./model.ckpt-1000")
#node names
i=0
for n in tf.get_default_graph().as_graph_def().node:
print(n.name,i);
i+=1
#end for
print("total:",i);
output_node_names=[
"detection_boxes","detection_classes",
"detection_scores","num_detections"
];
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess,input_graph_def,output_node_names);
#save to .pb file
output_graph="./model.pb"
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(output_graph_def.SerializeToString());
#end with
sess.close();错误是:

似乎已经失去了它的输出节点名。原始预培训模型中有以下输出节点名(将上面代码中的检查点文件更改为原始训练模型中的检查点文件):detection_boxes、detection_classes、detection_scores和num_detections。输出节点名称完全位于原始节点名称中,下面是它们的索引(来自上面的节点名称'for‘循环):

我的问题是如何使输出节点名与原始的预培训模型保持一致?节点名称是用代码定义的,但是这里没有代码,只有一些信任和文件‘tra.py’。
PS。有一个名为summary_op的total_loss,但我不知道它是否是输出(?):

发布于 2019-04-19 05:36:10
为了让'image_tensor‘(输入)和其他输出节点名,'detection_boxes','detection_classes','detection_scores','num_detections',使用tensorflow/models/research/object_detection中的实用程序脚本'export_inference_graph.py'.该脚本甚至优化冻结图(冻结模型)以进行推理。根据我的测试模型,节点数量从26,000个减少到5,000个;这对推理速度来说是很好的。
下面是指向export_inference_graph.py:graph.py的链接
如何运行:
#bash command
python3 export_inference_graph.py \
--input_type image_tensor \
--pipeline_config_path PATH_TO_PIPELINE.config \
--trained_checkpoint_prefix PATH_TO/model.ckpt-NUMBER \
--output_directory PATH_TO_NEW_DIR .pb创建代码仅适用于从头创建的模型,其节点名称是手动定义的,对于从TensorFlow model Zoo zoo.md下载的经过预先培训的模型进行微调的模型检查点,它将无法工作!
https://stackoverflow.com/questions/55738456
复制相似问题