Writing tfrecords with images and multimets for classification

I want to perform multi-label classification using TensorFlow. I have about 95,000 images, and for each image there is a corresponding label vector. There are 7 shortcuts for each image. These 7 marks are presented as a tensor of size 7. Each image has a shape (299,299.3).

How can I now write an image with the corresponding label vector / tensor to a .tfrecords file

my current code / approach:

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)

And after I created the .tfrecords file, can I then read it from my TensorFlow Training Code and load the data automatically?

+4
source share
2 answers

tf.train.Example, example = tf.train.Example(). , API python.

0

Alexandre, - :

# 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.

, , false.

0

Source: https://habr.com/ru/post/1658108/


All Articles