繁体   English   中英

在从预训练模型进行微调后,丢失TensorFlow模型中的输出节点名称

[英]Losing output node names in TensorFlow model after fine-tuning from pre-trained model

我按照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 输出节点名称与原始名称完全相同,这里是它们的索引(来自上面的'loop'节点名称):

在此输入图像描述

我的问题是如何保持输出节点名称不受原始预训练模型的影响 节点名称在代码中定义,但这里没有代码,只有一些配置和文件'train.py'。

PS。 在total_loss之后有一个名为summary_op的东西,但我不知道它是否是输出(?):

在此输入图像描述

为了使' image_tensor '(输入)和其他输出节点名称' detection_boxes ',' detection_classes ',' detection_scores ',' num_detections ',使用名为' export_inference_graph.py '的tensorflow / models / research / object_detection中的实用程序脚本。 该脚本甚至可以优化冻结图(冻结模型)以进行推理。 在我的测试模型上检查,节点数从26,000减少到5,000; 这对推理速度很有帮助。

这是export_inference_graph.py的链接: https//github.com/tensorflow/models/blob/0558408514dacf2fe2860cd72ac56cbdf62a24c0/research/object_detection/export_inference_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下载的预训练模型微调的模型检查点https://github.com/tensorflow/models/ blob / master / research / object_detection / g3doc / detection_model_zoo.md ,它不起作用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM