How do I convert a directory of jpeg images to TFRecords file in tensorflow?

后端 未结 7 672
后悔当初
后悔当初 2020-11-30 17:37

I have training data that is a directory of jpeg images and a corresponding text file containing the file name and the associated category label. I am trying to convert thi

相关标签:
7条回答
  • 2020-11-30 18:19

    Note that images will be saved in TFRecord as uncompressed tensors, possibly increasing the size by a factor of about 5. That's wasting storage space, and likely to be rather slow because of the amount of data that needs to be read.

    It's far better to just save the filename in the TFRecord, and read the file on demand. The new Dataset API works well, and the documentation has this example:

    # Reads an image from a file, decodes it into a dense tensor, and resizes it
    # to a fixed shape.
    def _parse_function(filename, label):
      image_string = tf.read_file(filename)
      image_decoded = tf.image.decode_jpeg(image_string)
      image_resized = tf.image.resize_images(image_decoded, [28, 28])
      return image_resized, label
    
    # A vector of filenames.
    filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])
    
    # `labels[i]` is the label for the image in `filenames[i].
    labels = tf.constant([0, 37, ...])
    
    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
    dataset = dataset.map(_parse_function)
    
    0 讨论(0)
提交回复
热议问题