【问题标题】:How to convert a .png file to TFrecord tensorflow format?如何将 .png 文件转换为 TFrecord tensorflow 格式?
【发布时间】:2020-09-08 06:11:19
【问题描述】:

我有 .png 格式的图像和 .csv 格式的标签。我想将它们转换为 tfrecords 格式。我对张量流很陌生。如果有人可以指出我需要知道的所有事情以及如何做到这一点。会很棒的。

我已经在网上搜索过了。但有些已经过时,有些非常先进。

编辑:我的图像存储在一个目录中。

谢谢

【问题讨论】:

  • 你的图片是存放在一个目录还是多个目录?

标签: tensorflow deep-learning computer-vision tfrecord image-preprocessing


【解决方案1】:

您必须将图像转换为tf.train.Example 才能将其写入tfrecord 文件。 下面是一个简单的示例,说明如何执行此操作。

看看csv文件:

代码:

# The following functions can be used to convert a value to a type compatible
# with tf.train.Example.

def _bytes_feature(value):
    """Returns a bytes_list from a string / byte."""
    if isinstance(value, type(tf.constant(0))):
        value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
    """Returns a float_list from a float / double."""
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _int64_feature(value):
    """Returns an int64_list from a bool / enum / int / uint."""
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def image_example(image_string, label):
    image_shape = tf.image.decode_png(image_string).shape
    feature = {
      'height': _int64_feature(image_shape[0]),
      'width': _int64_feature(image_shape[1]),
      'depth': _int64_feature(image_shape[2]),
      'label': _int64_feature(label),
      'image_raw': _bytes_feature(image_string),
    }
    return tf.train.Example(features=tf.train.Features(feature=feature))

image_example 函数返回单个图像的 tf.train.Example 对象。

您必须遍历数据框以创建每个图像的tf.train.Example 对象并使用tf.io.TFRecordWriter 写入对象。

代码:

record_file = 'images.tfrecords'
image_labels = {
    'cat': 0,
    'bridge': 1,
}
with tf.io.TFRecordWriter(record_file) as writer:
    for row in df.index:
        full_path = 'data/img/new/' + df['filename'][row]
        label = image_labels[df['label'][row]]
        image_string = tf.io.read_file(full_path)
        tf_example = image_example(image_string, label)
        writer.write(tf_example.SerializeToString())

有关读取/写入 TFRecord 文件的完整教程,请参阅 this

如果您有多个标签,您可以在 image_example 内的特征字典中创建多个特征。 代码:

def image_example(image_string, label_color, label_type):
    image_shape = tf.image.decode_png(image_string).shape
    feature = {
      'height': _int64_feature(image_shape[0]),
      'width': _int64_feature(image_shape[1]),
      'depth': _int64_feature(image_shape[2]),
      'label_color': _int64_feature(label_color),
      'label_type': _int64_feature(label_type),
      'image_raw': _bytes_feature(image_string),
    }
    return tf.train.Example(features=tf.train.Features(feature=feature))

【讨论】:

  • 你能解释一下 TFRecordWrite 中的循环是做什么的吗?谢谢
  • 什么是多重标签?
  • TFRecordWriter 中的循环以字节格式读取存在于full_path 中的文件,标签取自代码(0 or 1) 中的映射。它们被传递到image_example。此函数返回 tfrecords 所需的 tf.train.Example 对象。您能否详细说明多个标签的含义?
  • 所以可以说我有牛仔裤的图像。牛仔裤一号是黑色和破洞的,二号是蓝色和正常的,三号是灰色和补丁的。所以我有这些标签的 csv 文件。第 1 行将有第 1 条牛仔裤,并且有黑色、蓝色、灰色、撕裂、修补、正常的列。对于 1 号牛仔裤,黑色和破洞的列将设置为 1,其他为 0。同样,对于 2 号牛仔裤,蓝色和正常的列将设置为 1,其他为 0。以此类推,对于 3 号牛仔裤和数千条牛仔裤.
  • SequenceExample 比这有什么优势吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 2015-09-21
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
相关资源
最近更新 更多