python - 如何可视化 TFRecord?

标签 python tensorflow object-detection tfrecord

我在另一个论坛上被问到这个问题,但我想我会把它发布在这里,以供遇到 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()

最佳答案

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

TFRecord Viewer screenshot

关于python - 如何可视化 TFRecord?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50391967/

相关文章:

python - Tensorflow - 在多个 GPU 上复制模型并共享变量?

python - 测量图像中线段的长度(以像素为单位)

python - 如何在 Mac OS X 上的 PyCharm 中使用 Tensorflow 调试器?

tensorflow - Tensorboard eval.py IOU 用于对象检测

android - 如何用opencv在白色背景上检测白纸

python - 如何将 numpy 数组转换为标准 TensorFlow 格式?

python - Django:如何返回项目属性 choice_set 不为空的查询集?

python - 使用日期时间索引转置数据框

python - 是否可以使用 multiprocessing.Queue 在两个 python 脚本之间进行通信?

tensorflow - AI 平台上的 BigQuery 连接器未注册操作类型\'IO>BigQueryClient\'