将 tfrecords 转换为图像

Convert tfrecords to image

我找到了一个训练数据集,它是一组 tfrecords 文件,我正在尝试将它们转换为图像但没有结果,是否可以将它们转换为图像?

要找出 tf.record 中的内容,请使用 tf.data.TFRecordDatasettf.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)

要解析记录,请使用 tf.data.TFRecordDatasettf.io.parse_single_exampletf.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')

同时检查 Satellite VU 的 source code