用图像和多标签编写tfrecord以进行分类

ZCD*_*DEV 5 deep-learning tensorflow

我想使用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)
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*sos 0

创建一个tf.train.Example简单的example = tf.train.Example(). 然后,您可以使用普通的协议缓冲区 python API来操作它。