如何在经过预训练的模型但缺少标签文件(.pbtxt)的情况下使用Tensorflows对象检测模型Zoo

时间:2019-06-13 18:09:01

标签: python tensorflow label

我正在尝试运行Tensorflow对象检测。不幸的是,我发现Tensorflow的预训练模型都没有标签文件。如何获得这些文件?我要做的就是测试目标检测中的几张图片并显示标签。到目前为止,以下代码是我所拥有的。不幸的是,几乎所有教程都使用我没有的标签文件(.pbtxt)。在Tensorflow Tensorflow detection model zoo的相应下载页面上,标签文件包含在下载文件中,但没有。我已经下载了不同的模型。这些模型都没有标签文件。如果有人可以帮助我,我将不胜感激。

到目前为止,我的代码:

import tensorflow as tf
import cv2
import os

def get_frozen_graph(graph_file):
    """Read Frozen Graph file from disk."""
    with tf.gfile.FastGFile(graph_file, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    return graph_def

# The TensorRT inference graph file downloaded from Colab or your local machine.
pb_fname = os.path.join(os.getcwd(), "faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28", "frozen_inference_graph.pb")
trt_graph = get_frozen_graph(pb_fname)

input_names = ['image_tensor']

# Create session and load graph
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
tf_sess = tf.Session(config=tf_config)
tf.import_graph_def(trt_graph, name='')

tf_input = tf_sess.graph.get_tensor_by_name(input_names[0] + ':0')
tf_scores = tf_sess.graph.get_tensor_by_name('detection_scores:0')
tf_boxes = tf_sess.graph.get_tensor_by_name('detection_boxes:0')
tf_classes = tf_sess.graph.get_tensor_by_name('detection_classes:0')
tf_num_detections = tf_sess.graph.get_tensor_by_name('num_detections:0')


IMAGE_PATH = os.path.join(os.getcwd(), "testimages", "000002_491724089556.png")
image = cv2.imread(IMAGE_PATH)
image = cv2.resize(image, (300, 300))

scores, boxes, classes, num_detections = tf_sess.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict={
    tf_input: image[None, ...]
})
boxes = boxes[0]  # index by 0 to remove batch dimension
scores = scores[0]
classes = classes[0]
num_detections = int(num_detections[0])

1 个答案:

答案 0 :(得分:0)

我最终可以自己解决问题。 Tensorflow文档当场有故障。标签文件(.pbtxt)随Tensorflow一起提供,位于文件夹/ models / research / object_detection / data /中。当您仅获得Model Zoo模型时,它们不在您下载的文件夹中。