gpt4 book ai didi

database - 如何从 png 图像创建 TFRecords 文件

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:11 24 4
gpt4 key购买 nike

我一周前才开始使用 TensorFlow,我遇到了一些基本问题。

最主要的是我没有找到创建包含我所有数据的 TFRecords 的方法。我知道这个过程对于用几百万 32x32 像素的图像训练我自己的网络是必要的。

我找到了很多教程和很多文档都提到了“input_pipeline”,但是这些教程都没有清楚地解释我如何使用自己的图像创建自己的数据库。

我有几个主要文件夹和一些子文件夹,每个大约 300,000 张 png 图像,其中标签在图像名称中(0 或 1 - 二进制分类)。

获取这些图像的方法是通过 (glob) 行:

"/home/roishik/Desktop/database/train/exp*/*png"
"/home/roishik/Desktop/database/train/exp*/tot*/*png"

所以我的问题是:

如何创建包含这些图像及其标签的 TFRecords 文件?

非常感谢您的帮助!我被这个问题困扰了将近两天,我只找到了关于 MNIT 和 ImageNet 的真正具体的答案。

谢谢!

最佳答案

数百万张 32x32 图片?听起来很像 CIFAR。查看TensorFlow Models ,他们有一个脚本可以下载 CIFAR10 并将其转换为 TFRecords:download_and_convert_data.py .如果您的数据不是 CIFAR,请检查代码,它可能会对您有所帮助。

加载 CIFAR10 的代码如下所示:

with tf.Graph().as_default():
image_placeholder = tf.placeholder(dtype=tf.uint8)
encoded_image = tf.image.encode_png(image_placeholder)

with tf.Session('') as sess:
for j in range(num_images):
[...] # load image and label from disk
image = [...]
label = [...]

png_string = sess.run(encoded_image,
feed_dict={image_placeholder: image})

example = dataset_utils.image_to_tfexample(
png_string, 'png', _IMAGE_SIZE, _IMAGE_SIZE, label)
tfrecord_writer.write(example.SerializeToString())
[...]

image_to_tfexample() 函数如下所示:

def image_to_tfexample(image_data, image_format, height, width, class_id):
return tf.train.Example(features=tf.train.Features(feature={
'image/encoded': bytes_feature(image_data),
'image/format': bytes_feature(image_format),
'image/class/label': int64_feature(class_id),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}))

int_64_feature() 函数看起来像这样(bytes_feature() 函数类似):

def int64_feature(values):
if not isinstance(values, (tuple, list)):
values = [values]
return tf.train.Feature(int64_list=tf.train.Int64List(value=values))

编辑

更多细节:

  • TFRecordWriter 是这样创建的(这也会创建文件):

    with tf.python_io.TFRecordWriter(training_filename) as tfrecord_writer:
    [...] # use the tfrecord_writer
  • documentation for tf.image.encode_png()表示图像应具有形状 [height, width, channels],其中 channels = 1 用于灰度,channels = 2 用于灰度 + alpha , 3 表示 RGB 颜色,channels = 4 表示 RGB 颜色 + alpha (RGBA)。

关于database - 如何从 png 图像创建 TFRecords 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39460436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com