Loss of Image Information

Solution for Loss of Image Information
is Given Below:

When reading a JPEG image from a TFRecord there seems to be loss of information. Here is an example:

The tf.data.Dataset was created using the Keras image_dataset_from_directory function and, each image tensor of shape 600x600x3 was encoded to a byte string using the tf.io.encode_jpeg:

image = tf.image.convert_image_dtype(image_tensor, dtype=tf.uint8)
image = tf.io.encode_jpeg(image, quality=100)

Each TFRecord example was created like this:

    # encoded_image is the output of encode_jpeg function
    image_feature = tf.train.Feature(
        bytes_list=tf.train.BytesList(value=[
            encoded_image
        ])
    )
    features = tf.train.Features(feature={
        'image': image_feature
    })
    example = tf.train.Example(features=features)
    return example.SerializeToString()

And below is the code that loads the TFRecords Dataset, using tf.image.decode_jpeg to decode the images back to tensors of shape 600x600x3, and then saves one image to disk using PIL:

def read_tfrecord(example):
    tfrecord = {
        "image": tf.io.FixedLenFeature([], tf.string)
    }
    example = tf.io.parse_single_example(example, tfrecord)
    image = tf.image.decode_jpeg(example['image'], channels=3)
    return image


def read_dataset(dataset_path):
    filenames = tf.io.gfile.glob(dataset_path + '/validation/*.tfrecord')

    dataset = tf.data.TFRecordDataset(filenames)
    dataset = dataset.map(read_tfrecord)
    dataset = dataset.repeat()
    dataset = dataset.batch(128)

    for image, label in dataset.take(1):
        Image.fromarray((image[0].numpy())).save('./images/img.jpeg')

I have absolutely no clue what is causing this apparent loss of image information so any help would be much appreciated!

Notes: