我将一些图像编码为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。”“” 返回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]
答案 0 :(得分:0)
我可以使用tf.io.decode_raw()解码TFRecords,然后使用tf.reshape()获得原始图像。虽然仍然不知道何时使用tf.io.decode_raw()和何时使用tf.io.decode_jpeg()。