首页 > 解决方案 > Tensorflow 对象检测 API - 推理后如何读取 TFRecord?

问题描述

我已经使用以下方法运行了对象检测推理:

PYTHONPATH=$PYTHONPATH:$(readlink -f ..) \
python -m object_detection/inference/infer_detections \
  --input_tfrecord_paths=$TF_RECORD_FILES \
  --output_tfrecord_path=${SPLIT}_detections.tfrecord-00000-of-00001 \
  --inference_graph=faster_rcnn_inception_resnet_v2_atrous_oid/frozen_inference_graph.pb \
  --discard_image_pixels

如何读取生成的 TFRecord 文件以查看检测到的对象?

我有以下代码

    def read_decode(fname_queue):
    reader = tf.TFRecordReader()
    key, serialized_example = reader.read(fname_queue)
    features = tf.parse_single_example(serialized_example,
                                  features = {
                                    'image/encoded': tf.FixedLenFeature([], tf.string),
                                    'image/height': tf.FixedLenFeature([],tf.int64),
                                    'image/width': tf.FixedLenFeature([], tf.int64),
                                    'image/detection/bbox/xmin': tf.FixedLenFeature([], tf.float32),
                                    'image/detection/bbox/xmax': tf.FixedLenFeature([], tf.float32),
                                    'image/detection/bbox/ymin': tf.FixedLenFeature([], tf.float32),
                                    'image/detection/bbox/ymax': tf.FixedLenFeature([], tf.float32),
                                    'image/detection/label': tf.FixedLenFeature([], tf.int64),
                                    'image/detection/score': tf.FixedLenFeature([], tf.float32,)
                                              }
                                      )
    image_encoded = features["image/encoded"]
    image_raw = tf.image.decode_png(image_encoded, channels=3)
    height = features['image/height']
    width = features['image/width']
    xmin = features['image/detection/bbox/xmin']
    ymin = features['image/detection/bbox/ymin']
    xmax = features['image/detection/bbox/xmax']
    ymax = features['image/detection/bbox/ymax']
    label = features['image/detection/label']
    score = features['image/detection/score']
    bbox = [ymin,xmin,ymax,xmax]
    return [image_raw,bbox,score,label]

current_image = read_decode(fname_queue)
with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    for i in range(1):
        image_data = sess.run([current_image])
        img = Image.fromarray(image_data[0], "RGB")
        plt.imshow(img)
    coord.request_stop()
    coord.join(threads)
    sess.close()

但我收到错误:

InvalidArgumentError:键:图像/检测/bbox/xmin。无法解析序列化的示例。[[{{节点 ParseSingleExample_17/ParseSingleExample}} = ParseSingleExample[Tdense=[DT_FLOAT,DT_FLOAT,DT_FLOAT,DT_FLOAT,DT_INT64,DT_FLOAT,DT_STRING,DT_INT64,DT_INT64],dense_keys=[“图像/检测/bbox/xmax”,“图像/detection/bbox/xmin", "image/detection/bbox/ymax", "image/detection/bbox/ymin", "image/detection/label", "image/detection/score", "image/encoded", “图像/高度”、“图像/宽度”]、dense_shapes=[[]、[]、[]、[]、[]、[]、[]、[]、[]]、num_sparse=0、sparse_keys= [], sparse_types=[], _device="/job:localhost/replica:0/task:0/device:CPU:0"](ReaderReadV2_17:1, ParseSingleExample_17/Const, ParseSingleExample_17/Const, ParseSingleExample_17/Const,

标签: python-3.xtensorflowobject-detection-api

解决方案


只是一个建议(与错误无关),如果您的 Tensorflow 版本 >= 1.4,您可以尝试 tf.data 模块而不是使用线程、队列和协调器。它将有助于从代码中删除 for 循环、线程和协调器行。

以下是来自 TensorFlow 文档的评论:

注意:在 1.2 之前的 TensorFlow 版本中,我们建议使用多线程、基于队列的输入管道以提高性能。但是,从 TensorFlow 1.4 开始,我们>建议使用 tf.data 模块。(有关详细信息,请参阅数据集。在 TensorFlow 1.2 > 和 1.3 中,该模块称为 tf.contrib.data。) tf.data 模块提供了一个更易于使用的接口,用于构建高效的输入管道。此外,我们已经停止 > 开发旧的多线程、基于队列的输入管道。我们在此文件中保留了 > 文档,以帮助仍在维护旧代码的开发人员。

有关详细信息,请查看此页面: https ://www.tensorflow.org/guide/datasets


推荐阅读