利用c++调用tensorflow中训练的ssd模型

mac2024-05-26  30

**1.**在tensorflow中训练完成后会生成.ckpt文件,这时需要export_inference_graph.py(路径E:\models-master\research\object_detection)将.ckpt文件转化为.pb文件。 其中只需要修改pipeline_config_path(模型的默认参数文件); trained_checkpoint_prefix(训练产生的.ckpt文件) output_directory(输出的.pb文件放置的位置) 2opencv调用模型不仅需要.pb文件,还需要.pbtxt文件。下面则是.pbtxt文件生成的方法 对第一步生成的.pb文件,经过python文件(如下图)转化。

import tensorflow as tf from tensorflow.tools.graph_transforms import TransformGraph with tf.gfile.FastGFile('E:/models-master/research/object_detection\pb_model/ssd_mobilenet_v1_coco_11_06_2017_tank/frozen_inference_graph.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) graph_def = TransformGraph(graph_def, ['image_tensor'], ['detection_boxes', 'detection_classes', 'detection_scores', 'num_detections'], ['sort_by_execution_order']) with tf.gfile.FastGFile('./sorted_inference_graph.pb', 'wb') as f: f.write(graph_def.SerializeToString())

再基于opencv4.0中的tf_text_graph_ssd.py(D:\opencv\sources\samples\dnn)文件来转化成.ptxt文件 执行cmd中的命令

python tf_text_graph_ssd.py --input ssd.pb --config pipeline.config --output ssd.pbtxt

成功即可通过opencv调用。

最新回复(0)