gpt4 book ai didi

tensorflow - 如何将 jpeg 图像目录转换为 tensorflow 中的 TFRecords 文件?

转载 作者:行者123 更新时间:2023-12-03 06:02:58 25 4
gpt4 key购买 nike

我有训练数据,它是 jpeg 图像的目录和包含文件名和关联类别标签的相应文本文件。我正在尝试将此训练数据转换为 tfrecords 文件,如 tensorflow 文档中所述。我花了相当多的时间试图让它工作,但 tensorflow 中没有示例演示如何使用任何读取器读取 jpeg 文件并使用 tfrecordwriter 将它们添加到 tfrecord

最佳答案

我希望这有帮助:

filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) #  list of files to read

reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)

my_img = tf.image.decode_png(value) # use decode_png or decode_jpeg decoder based on your files.

init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)

# Start populating the filename queue.

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

for i in range(1): #length of your filename list
image = my_img.eval() #here is your image Tensor :)

print(image.shape)
Image.show(Image.fromarray(np.asarray(image)))

coord.request_stop()
coord.join(threads)

要将所有图像作为张量数组获取,请使用以下代码示例。

Github repo of ImageFlow

<小时/>

更新:

在前面的回答中我只是讲述了如何读取 TF 格式的图像,但没有将其保存在 TFRecords 中。为此,您应该使用:

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

# images and labels array as input
def convert_to(images, labels, name):
num_examples = labels.shape[0]
if images.shape[0] != num_examples:
raise ValueError("Images size %d does not match label size %d." %
(images.shape[0], num_examples))
rows = images.shape[1]
cols = images.shape[2]
depth = images.shape[3]

filename = os.path.join(FLAGS.directory, name + '.tfrecords')
print('Writing', filename)
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'depth': _int64_feature(depth),
'label': _int64_feature(int(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())

更多信息here

您可以这样读取数据:

# Remember to generate a file name queue of you 'train.TFRecord' file path
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
dense_keys=['image_raw', 'label'],
# Defaults are not specified since both keys are required.
dense_types=[tf.string, tf.int64])

# Convert from a scalar string tensor (whose single string has
image = tf.decode_raw(features['image_raw'], tf.uint8)

image = tf.reshape(image, [my_cifar.n_input])
image.set_shape([my_cifar.n_input])

# OPTIONAL: Could reshape into a 28x28 image and apply distortions
# here. Since we are not applying any distortions in this
# example, and the next step expects the image to be flattened
# into a vector, we don't bother.

# Convert from [0, 255] -> [-0.5, 0.5] floats.
image = tf.cast(image, tf.float32)
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5

# Convert label from a scalar uint8 tensor to an int32 scalar.
label = tf.cast(features['label'], tf.int32)

return image, label

关于tensorflow - 如何将 jpeg 图像目录转换为 tensorflow 中的 TFRecords 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849617/

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