Search code examples
tensorflowdeep-learningcomputer-visiontfrecordimage-preprocessing

How to convert a .png file to TFrecord tensorflow format?


I have images in .png format and their labels in .csv format. I want to convert them in tfrecords format. I'm very new to tensorflow. If someone can point me towards all the things i need to know and how to do this. It'll be great.

I've scoured through the net. But some are outdated or some are very advanced.

Edit: My images are stored in a single directory.

Thanks


Solution

  • You have to convert your image into tf.train.Example in order to write it as tfrecord file. Here is a simple example of how you can do this.

    Taking a look at csv file:

    this

    Code:

    # The following functions can be used to convert a value to a type compatible
    # with tf.train.Example.
    
    def _bytes_feature(value):
        """Returns a bytes_list from a string / byte."""
        if isinstance(value, type(tf.constant(0))):
            value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
        return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
    
    def _float_feature(value):
        """Returns a float_list from a float / double."""
        return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
    
    def _int64_feature(value):
        """Returns an int64_list from a bool / enum / int / uint."""
        return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
    
    
    def image_example(image_string, label):
        image_shape = tf.image.decode_png(image_string).shape
        feature = {
          'height': _int64_feature(image_shape[0]),
          'width': _int64_feature(image_shape[1]),
          'depth': _int64_feature(image_shape[2]),
          'label': _int64_feature(label),
          'image_raw': _bytes_feature(image_string),
        }
        return tf.train.Example(features=tf.train.Features(feature=feature))
    

    The image_example functions return a tf.train.Example object of a single image.

    You have to iterate over the data frame to create tf.train.Example object of every image and write the object using tf.io.TFRecordWriter.

    Code:

    record_file = 'images.tfrecords'
    image_labels = {
        'cat': 0,
        'bridge': 1,
    }
    with tf.io.TFRecordWriter(record_file) as writer:
        for row in df.index:
            full_path = 'data/img/new/' + df['filename'][row]
            label = image_labels[df['label'][row]]
            image_string = tf.io.read_file(full_path)
            tf_example = image_example(image_string, label)
            writer.write(tf_example.SerializeToString())
    

    For a complete tutorial on Reading/Writing TFRecord files see this.

    If you have multiple labels you can create multiple features in your feature dictionary inside image_example. Code:

    def image_example(image_string, label_color, label_type):
        image_shape = tf.image.decode_png(image_string).shape
        feature = {
          'height': _int64_feature(image_shape[0]),
          'width': _int64_feature(image_shape[1]),
          'depth': _int64_feature(image_shape[2]),
          'label_color': _int64_feature(label_color),
          'label_type': _int64_feature(label_type),
          'image_raw': _bytes_feature(image_string),
        }
        return tf.train.Example(features=tf.train.Features(feature=feature))