简体   繁体   English

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

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

I follow the tutorial at https://tensorflow-object-detection-api-tutorial.readthedocs.io to fine-tune a pre-trained model to detect new objects in images. 我按照https://tensorflow-object-detection-api-tutorial.readthedocs.io上的教程来微调预先训练的模型以检测图像中的新对象。 The pre-trained model is ssd_inception_v2_coco . 预训练的模型是ssd_inception_v2_coco

I trained and evaluated the model successfully after a few thousand steps with loss from 26 down to 1. However, I failed to create the frozen model with this code: 几千步之后我成功地训练和评估了模型,从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();

The error is: 错误是:

在此输入图像描述

It seems the fine-tuned model has lost its output node names . 似乎微调模型已丢失其输出节点名称 There are these output node names in the original pre-trained model (change the checkpoint files in the code above to the ones in the original trained model): detection_boxes, detection_classes, detection_scores, and num_detections . 原始预训练模型中有这些输出节点名称(将上面代码中的检查点文件更改为原始训练模型中的检查点文件): detection_boxes,detection_classes,detection_scores和num_detections The output node names are exactly in the original one, here are their indices (from the node name 'for' loop above): 输出节点名称与原始名称完全相同,这里是它们的索引(来自上面的'loop'节点名称):

在此输入图像描述

My question is how to keep the output node names from the original pre-trained model ? 我的问题是如何保持输出节点名称不受原始预训练模型的影响 The node names are defined in code, but there's no code here, only some configs and the file 'train.py'. 节点名称在代码中定义,但这里没有代码,只有一些配置和文件'train.py'。

PS. PS。 There's something called summary_op after total_loss, but I don't know whether it is the output(?): 在total_loss之后有一个名为summary_op的东西,但我不知道它是否是输出(?):

在此输入图像描述

In order to have ' image_tensor ' (input), and other output node names ' detection_boxes ', ' detection_classes ', ' detection_scores ', ' num_detections ', use the utility script in tensorflow/models/research/object_detection named ' export_inference_graph.py '. 为了使' image_tensor '(输入)和其他输出节点名称' detection_boxes ',' detection_classes ',' detection_scores ',' num_detections ',使用名为' export_inference_graph.py '的tensorflow / models / research / object_detection中的实用程序脚本。 This script even optimises the frozen graph (frozen model) for inference. 该脚本甚至可以优化冻结图(冻结模型)以进行推理。 As checked on my test model, the number of nodes reduced from 26,000 down to 5,000; 在我的测试模型上检查,节点数从26,000减少到5,000; this is great for inference speed. 这对推理速度很有帮助。

Here's the link to export_inference_graph.py: https://github.com/tensorflow/models/blob/0558408514dacf2fe2860cd72ac56cbdf62a24c0/research/object_detection/export_inference_graph.py 这是export_inference_graph.py的链接: https//github.com/tensorflow/models/blob/0558408514dacf2fe2860cd72ac56cbdf62a24c0/research/object_detection/export_inference_graph.py

How to run: 怎么运行:

#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 

The .pb creating code in question only works on model created from scratch with node names defined manually, for a model checkpoint fine-tuned from a pre-trained model downloaded from TensorFlow Model Zoo https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md , it won't work! 有问题的.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.

相关问题 如何使用预训练模型进行文本分类?比较经过微调的 model 与未经微调的预训练 model - How to use pre-trained models for text classification?Comparing a fine-tuned model with a pre-trained model without fine-tuning 当使用TensorFlow slim调整预训练模型时,如何知道要排除或训练的范围? - How to know which scopes to exclude or to train when fine tuning a pre-trained model with TensorFlow slim? 张量流对象检测从现有检查点微调模型 - tensorflow object detection Fine-tuning a model from an existing checkpoint Tensorflow Python未加载预训练的模型 - Tensorflow python not loading the pre-trained model 在微调预训练模型的同时,在Keras中预处理图像的正确方法是什么 - What is the right way to preprocess images in Keras while fine-tuning pre-trained models 来自预训练 tensorflow model 的混淆矩阵 - Confusion matrix from pre-trained tensorflow model 预训练张量流模型的烧瓶 - flask with pre-trained tensorflow model 如何在 tensorflow 中使用预训练的 model 进行预测? - how to predict with pre-trained model in tensorflow? 如何在TensorFlow中使用预训练模型 - How to use pre-trained model in TensorFlow 为预先训练的模型添加新的输出 - add new output for pre-trained model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM