gpt4 book ai didi

tensorflow - 用图像和多标签编写 tfrecords 进行分类

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

我想用 TensorFlow 执行多标签分类。
我有大约 95000 张图像,每张图像都有一个相应的标签向量。每个图像有 7 个标签。这 7 个标签表示为一个大小为 7 的张量。每个图像的形状为 (299,299,3)。

我现在如何将带有相应标签向量/张量的图像写入 .tfrecords 文件

我目前的代码/方法:

def get_decode_and_resize_image(image_id):
image_queue = tf.train.string_input_producer(['../../original-data/'+image_id+".jpg"])
image_reader = tf.WholeFileReader()
image_key, image_value = image_reader.read(image_queue)
image = tf.image.decode_jpeg(image_value,channels=3)
resized_image= tf.image.resize_images(image, 299, 299, align_corners=False)
return resized_image



init_op = tf.initialize_all_variables()
with tf.Session() as sess:
# Start populating the filename queue.

sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

# get all labels and image ids
csv= pd.read_csv('../../filteredLabelsToPhotos.csv')

#create a writer for writing to the .tfrecords file
writer = tf.python_io.TFRecordWriter("tfrecords/data.tfrecords")

for index,row in csv.iterrows():

# the labels
image_id = row['photo_id']
lunch = tf.to_float(row["lunch"])
dinner= tf.to_float(row["dinner"])
reservations= tf.to_float(row["TK"])
outdoor = tf.to_float(row["OS"])
waiter = tf.to_float(row["WS"])
classy = tf.to_float(row["c"])
gfk = tf.to_float(row["GFK"])

labels_list = [lunch,dinner,reservations,outdoor,waiter,classy,gfk]
labels_tensor = tf.convert_to_tensor(labels_list)

#get the corresponding image
image_file= get_decode_and_resize_image(image_id=image_id)

#here : how do I now create a TFExample and write it to the .tfrecords file






coord.request_stop()
coord.join(threads)

在我创建了 .tfrecords 文件之后,我可以从我的 TensorFlow 训练代码中读取它并自动批处理数据吗?

最佳答案

要扩展亚历山大的答案,您可以执行以下操作:

# Set this up before your for-loop, you'll use this repeatedly
tfrecords_filename = 'myfile.tfrecords'
writer = tf.python_io.TFRecordWriter(tfrecords_filename)

# Then within your for-loop, you can write like so:
for ...:

#here : how do I now create a TFExample and write it to the .tfrecords file

example = tf.train.Example(features=tf.train.Features(feature={
'image_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_file])),
# the other features, labels you wish to include go here too
}))
writer.write(example.SerializeToString())

# then finally, don't forget to close the writer.
writer.close()

这假设您已经将图像转换为 image_file 中的字节数组。多变的。

我改编自 this very helpful post详细介绍了序列化图像,如果我上面的假设是错误的,可能会对您有所帮助。

关于tensorflow - 用图像和多标签编写 tfrecords 进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40111366/

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