首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何可视化TFRecord?

如何可视化TFRecord?
EN

Stack Overflow用户
提问于 2018-05-17 20:52:24
回答 3查看 3.6K关注 0票数 13

我在另一个论坛上被问到了这个问题,但我想我应该把它贴在这里,给那些对TFRecords有问题的人。

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

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

我找到的最好的方法是对TFRecord进行解码,并使用TF工具绘制它。下面是一些代码:

代码语言:javascript
复制
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()
EN

回答 3

Stack Overflow用户

发布于 2019-06-11 21:18:30

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

票数 7
EN

Stack Overflow用户

发布于 2019-02-23 17:35:17

谢谢你的代码,@Steve!我在github repo上到处寻找,但找不到检查tfrecord的方法。

我只想指出,似乎缺少一个导入行:

代码语言:javascript
复制
from google.protobuf import text_format 

在添加了这个之后,它对我来说运行正常。

票数 2
EN

Stack Overflow用户

发布于 2021-09-15 19:53:30

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50391967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档