【问题标题】:How to visualize a TFRecord?如何可视化 TFRecord?
【发布时间】:2018-10-27 18:04:00
【问题描述】:

我在另一个论坛上被问到这个问题,但我想我会把它发在这里给任何遇到 TFRecords 问题的人。

如果 TFRecord 文件中的标签与您的 labels.pbtxt 文件中的标签不一致,TensorFlow 的对象检测 API 可能会产生奇怪的行为。它会运行,损失可能会减少,但网络不会产生良好的检测。

另外,我总是对 X-Y、行-列空间感到困惑,所以我总是喜欢仔细检查以确保我的注释实际上是在注释图像的正确部分。

我发现做到这一点的最佳方法是解码 TFRecord 并使用 TF 工具对其进行绘图。下面是一些代码:

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from object_detection.utils import visualization_utils as vu
from object_detection.protos import string_int_label_map_pb2 as pb
from object_detection.data_decoders.tf_example_decoder import TfExampleDecoder as TfDecoder
from google.protobuf import text_format
def main(tfrecords_filename, label_map=None):
    if label_map is not None:
        label_map_proto = pb.StringIntLabelMap()
        with tf.gfile.GFile(label_map,'r') as f:
            text_format.Merge(f.read(), label_map_proto)
            class_dict = {}
            for entry in label_map_proto.item:
                class_dict[entry.id] = {'name':entry.display_name}
    sess = tf.Session()
    decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False)
    sess.run(tf.tables_initializer())
    for record in tf.python_io.tf_record_iterator(tfrecords_filename):
        example = decoder.decode(record)
        host_example = sess.run(example)
        scores = np.ones(host_example['groundtruth_boxes'].shape[0])
        vu.visualize_boxes_and_labels_on_image_array( 
            host_example['image'],                                               
            host_example['groundtruth_boxes'],                                                     
            host_example['groundtruth_classes'],
            scores,
            class_dict,
            max_boxes_to_draw=None,
            use_normalized_coordinates=True)
plt.imshow(host_example['image'])
plt.show()

【问题讨论】:

    标签: python tensorflow object-detection tfrecord


    【解决方案1】:

    如果您想直观地检查边界框/标签,可以查看此 TFRecord 查看器:https://github.com/sulc/tfrecord-viewer

    【讨论】:

    • 谢谢米兰! ?
    【解决方案2】:

    感谢您提供代码,@Steve!我在 github repo 上到处找,找不到检查 tfrecord 的方法。

    只是想指出似乎缺少导入行:

    from google.protobuf import text_format 
    

    添加后它运行正常

    【讨论】:

      【解决方案3】:

      我建议试试这个:https://www.tensorflow.org/tutorials/load_data/tfrecord#read_the_tfrecord_file

      import tensorflow as tf
      
      import numpy as np
      import IPython.display as display
      
      raw_image_dataset = tf.data.TFRecordDataset('images.tfrecords')
      
      # Create a dictionary describing the features.
      image_feature_description = {
          'height': tf.io.FixedLenFeature([], tf.int64),
          'width': tf.io.FixedLenFeature([], tf.int64),
          'depth': tf.io.FixedLenFeature([], tf.int64),
          'label': tf.io.FixedLenFeature([], tf.int64),
          'image_raw': tf.io.FixedLenFeature([], tf.string),
      }
      
      def _parse_image_function(example_proto):
        # Parse the input tf.train.Example proto using the dictionary above.
        return tf.io.parse_single_example(example_proto, image_feature_description)
      
      parsed_image_dataset = raw_image_dataset.map(_parse_image_function)
      parsed_image_dataset
      

      【讨论】:

        猜你喜欢
        • 2020-02-21
        • 1970-01-01
        • 2017-12-11
        • 2019-11-18
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        • 2017-08-15
        • 2014-08-28
        相关资源
        最近更新 更多