title: Reading back a custom dataset TFRecords
body: <p>I am trying to create a custom dataset in <code>TFRecords</code> for a CycleGAN model. The model requires a new type of dataset which is not available so I need to create one. I have a few JPG images of 256x256. Following <a href="https://www.kaggle.com/gpreda/convert-jpg-images-to-tfrecords" rel="nofollow noreferrer">this</a> link, I created TFrecords file for my images, below code:</p>
  <pre><code>def _int64_feature(value):
      return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  
  
  def _bytes_feature(value):
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  
  # images input
  def convert_to(images, output_directory, name):
      num_examples = images.shape[0]
      rows = images.shape[1]
      cols = images.shape[2]
      depth = 1
  
      filename = os.path.join(output_directory, name + '.tfrecords')
      print('Writing', filename)
      writer = tf.python_io.TFRecordWriter(filename)
      for index in range(num_examples):
          image = images[index]
          image_raw = images[index].tobytes()
          example = tf.train.Example(features=tf.train.Features(feature={
              'height': _int64_feature(rows),
              'width': _int64_feature(cols),
              'depth': _int64_feature(depth),
              'image_raw': _bytes_feature(image_raw)}))
          writer.write(example.SerializeToString())
  
  def read_image(file_name, images_path):
      image = skimage.io.imread(images_path + file_name)
      return image
  
  def get_name(img_name):
      remove_ext = img_name.split(&quot;.&quot;)[0]
      name = remove_ext.split(&quot;_&quot;)
      return name[0]
  
  images_path = &quot;data/train/&quot;
  image_list = os.listdir(images_path)
  images = []
  for img_name in tqdm(image_list):
      tfrec_name = get_name(img_name)
      print(tfrec_name)
      img_data = read_image(img_name, images_path)
      convert_to(img_data, &quot;data/cat_image_tfrecords&quot;, tfrec_name+&quot;_cat_image&quot;)
  </code></pre>
  <p>Once the TFRecords are written, I use below code to read and decode it back</p>
  <pre><code>PHOTO_FILENAMES = tf.io.gfile.glob(str('data/cat_image_tfrecords/*.tfrecords'))
  
  IMAGE_SIZE = [256, 256]
  
  def decode_image(image):
      image = tf.image.decode_jpeg(image, channels=3)
      image = (tf.cast(image, tf.float32) / 127.5) - 1
      image = tf.reshape(image, [*IMAGE_SIZE, 3])
      return image
  
  def read_tfrecord(example):
      tfrecord_format = {
      'height': tf.io.FixedLenFeature([], tf.int64),
      'width': tf.io.FixedLenFeature([], tf.int64),
      'depth': tf.io.FixedLenFeature([], tf.int64),
      'image_raw': tf.io.FixedLenFeature([], tf.string),
      }
      example = tf.io.parse_single_example(example, tfrecord_format)
      image = decode_image(example['image_raw'])
      return image
  
  def load_dataset(filenames, labeled=True, ordered=False):
      dataset = tf.data.TFRecordDataset(filenames)
      dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)
      return dataset
  
  photo_ds = load_dataset(PHOTO_FILENAMES, labeled=False).batch(1)
  example_photo = next(iter(photo_ds))
  </code></pre>
  <p>The decoding does not work since I get the below error in the last line</p>
  <pre><code>InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got unknown format starting with ']LBhXKeQFVC4S=/1'
       [[{{node DecodeJpeg}}]]
  </code></pre>
  <p>Clearly there is a mismatch between how I am writing the TFRecord in <code>convert_to</code> function and how I am reading it back in <code>read_tfrecord</code> function. But I am not sure how to fix it. Any suggestion?</p>
  <p><strong>EDIT</strong></p>
  <p>@sebastian-sz solution solves the problem. I tried to display one image like below</p>
  <pre><code>import matplotlib.pyplot as plt
  plt.subplot(121)
  plt.title('Photo')
  plt.imshow(example_photo[0])
  </code></pre>
  <p>It displays the image but I see that the color/light of the image is much darker than the original image. Not sure what is going on though. Attached screenshot. Original image at the bottom.</p>
  <p><a href="https://i.stack.imgur.com/xJosR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xJosR.png" alt="enter image description here" /></a></p>