【问题标题】:decode TFRecord fail. Expected image (JPEG, PNG, or GIF), got unknown format starting with '\257\解码 TFRecord 失败。预期的图像(JPEG、PNG 或 GIF),格式未知,以 '\257\
【发布时间】:2019-07-19 23:21:01
【问题描述】:

我将一些图像编码为 TFRecords 作为示例,然后尝试对其进行解码。但是,在解码过程中出现了一个错误,我真的无法修复它。

InvalidArgumentError:预期图像(JPEG、PNG 或 GIF),格式未知,以 '\257\222\244\257\222\244\260\223\245\260\223\245\262\225 开头\247\263' [[{{node DecodeJpeg}}]] [Op:IteratorGetNextSync]

编码: def _bytes_feature(值): """从字符串/字节返回一个 bytes_list。""" return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
  """Returns a float_list from a float / double."""
  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _int64_feature(value):
  """Returns an int64_list from a bool / enum / int / uint."""
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

src_path = r"E:\data\example"
record_path = r"E:\data\data"
sum_per_file = 4
num = 0
key = 3

for img_name in os.listdir(src_path):
    recordFileName = "trainPrecipitate.tfrecords"
    writer = tf.io.TFRecordWriter(record_path + recordFileName)
    img_path = os.path.join(src_path, img_name)
    img = Image.open(img_path, "r")
    height = np.array(img).shape[0]
    width = np.array(img).shape[1]
    img_raw = img.tobytes()
    example = tf.train.Example(features = tf.train.Features(feature={
        'image/encoded': _bytes_feature(img_raw),
        'image/class/label': _int64_feature(key),
        'image/height': _int64_feature(height),
        'image/width': _int64_feature(width)
    }))
    writer.write(example.SerializeToString())
writer.close()

解码: 导入 IPython.display 作为显示

train_files = tf.data.Dataset.list_files(r"E:\data\datatrainPrecipitate.tfrecords")
train_files = train_files.interleave(tf.data.TFRecordDataset)

def decode_example(example_proto):
    image_feature_description = {
    'image/height': tf.io.FixedLenFeature([], tf.int64),
    'image/width': tf.io.FixedLenFeature([], tf.int64),
    'image/class/label': tf.io.FixedLenFeature([], tf.int64, default_value=3),
    'image/encoded': tf.io.FixedLenFeature([], tf.string)
}
    parsed_features = tf.io.parse_single_example(example_proto, image_feature_description)
    height = tf.cast(parsed_features['image/height'], tf.int32)
    width = tf.cast(parsed_features['image/width'], tf.int32)
    label = tf.cast(parsed_features['image/class/label'], tf.int32)
    image_buffer = parsed_features['image/encoded']
    image = tf.io.decode_jpeg(image_buffer, channels=3)
    image = tf.cast(image, tf.float32)
    return image, label

def processed_dataset(dataset):
    dataset = dataset.repeat()
    dataset = dataset.batch(1)
    dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
#     print(dataset)
    return dataset

train_dataset = train_files.map(decode_example)
# train_dataset = processed_dataset(train_dataset)
print(train_dataset)
for (image, label) in train_dataset:
    print(repr(image))

InvalidArgumentError:预期图像(JPEG、PNG 或 GIF),格式未知,以 '\257\222\244\257\222\244\260\223\245\260\223\245\262\225 开头\247\263' [[{{node DecodeJpeg}}]] [Op:IteratorGetNextSync]

【问题讨论】:

    标签: image tensorflow deep-learning computer-vision tfrecord


    【解决方案1】:

    我可以使用 tf.io.decode_raw() 解码 TFRecords,然后使用 tf.reshape() 获取原始图像。虽然仍然不知道何时使用 tf.io.decode_raw() 以及何时使用 tf.io.decode_jpeg()。

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2020-01-03
      • 1970-01-01
      • 2020-10-14
      • 2018-08-20
      • 2021-09-12
      • 2021-08-01
      • 2023-01-16
      • 2018-09-01
      相关资源
      最近更新 更多