Skip to content
Advertisement

Convert tfrecords to image

I found a training dataset which is a set of tfrecords files,im trying to convert them into images but with no results,is it possible to convert them to images ?

Advertisement

Answer

To find out what is inside a tf.record use tf.data.TFRecordDataset and tf.train.Example:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

ds = tf.data.TFRecordDataset(['/content/sv_0_128.tfrecords'])
for batch in ds.take(1):
  example = tf.train.Example()
  example.ParseFromString(batch.numpy())
  print(example)

To parse the records, use tf.data.TFRecordDataset with tf.io.parse_single_example and tf.io.parse_tensor:

def decode_fn(record_bytes):
  return tf.io.parse_single_example(
      record_bytes,
      {"air_temperature_at_2_metres_1hour_Maximum": tf.io.FixedLenFeature([], dtype=tf.string),
       "air_temperature_at_2_metres_1hour_Minimum": tf.io.FixedLenFeature([], dtype=tf.string),
       "elevation": tf.io.FixedLenFeature([], dtype=tf.string),
       "landcover": tf.io.FixedLenFeature([], dtype=tf.string), 
       "ndvi": tf.io.FixedLenFeature([], dtype=tf.string),
       "todays_fires": tf.io.FixedLenFeature([], dtype=tf.string),
       "todays_frp": tf.io.FixedLenFeature([], dtype=tf.string),
       "tomorrows_fires": tf.io.FixedLenFeature([], dtype=tf.string)}
  )

for batch in ds.map(decode_fn).take(1):
  f, axarr = plt.subplots(2,4)
  rows = np.repeat([0, 1], 4)
  cols = np.repeat([[0, 1, 2, 3]], 2, axis=0).ravel()
  for v, r, c in zip(batch.values(), rows, cols):
    axarr[r,c].imshow(tf.io.parse_tensor(v, out_type=tf.float32), cmap='gray')

enter image description here

Also check the source code of Satellite VU.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement