0
votes

I have run inference for object detection using:

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

How can I read the generated TFRecord file to see the detected objects?

I have the following code

    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()

But I am getting the error:

InvalidArgumentError: Key: image/detection/bbox/xmin. Can't parse serialized Example. [[{{node 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=["image/detection/bbox/xmax", "image/detection/bbox/xmin", "image/detection/bbox/ymax", "image/detection/bbox/ymin", "image/detection/label", "image/detection/score", "image/encoded", "image/height", "image/width"], 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, ParseSingleExample_17/Const, ParseSingleExample_17/Const_4, ParseSingleExample_17/Const, ParseSingleExample_17/Const_6, ParseSingleExample_17/Const_4, ParseSingleExample_17/Const_4)]]

1

1 Answers

-1
votes

Just a suggestion (not related to error), you can try tf.data module instead of using threads, queues and coordinator if you have Tensorflow version >= 1.4. It will help in removing for-loop, thread and coordinator lines from the code.

Below is the comment from Tensorflow documentation:

Note: In versions of TensorFlow before 1.2, we recommended using multi-threaded, queue->based input pipelines for performance. Beginning with TensorFlow 1.4, however, we >recommend using the tf.data module instead. (See Datasets for details. In TensorFlow 1.2 >and 1.3, the module was called tf.contrib.data.) The tf.data module offers an easier-to->use interface for constructing efficient input pipelines. Furthermore, we've stopped >developing the old multi-threaded, queue-based input pipelines. We've retained the >documentation in this file to help developers who are still maintaining older code.

For details check this page: https://www.tensorflow.org/guide/datasets