写入:
ima = img_array[index][1].tobytes()
example = tf.train.Example(
features=tf.train.Features(
feature={
'label': _int64_feature( int(img_array[index][0])), # label
'data': _bytes_feature(ima) # 图像的数据
}))
img_array[index][1].shape -- (1764,)
读取:
features = tf.parse_single_example(
serialized_example,
features={
'data':tf.FixedLenFeature([],tf.string),
'label':tf.FixedLenFeature([],tf.int64)
})
images = tf.decode_raw(features['data'],tf.uint8)
images --- shape -- (7056,0)
原因:
因为img_array 数据类型为 float32 转化为字节的时候 被分成7056
解决: 把 img_array数据类型转化为unit8: img_array[index][1].astype(np.uint8)

本文介绍使用TensorFlow处理图像数据时,遇到因数据类型不匹配导致的形状异常问题及解决方案。具体而言,在将float32类型的图像数组写入TFRecord文件时,由于类型不一致,读取后的图像数据形状出现异常。文章详细解释了问题原因,并提供了一种通过将数据类型转换为uint8来解决此问题的方法。
1万+

被折叠的 条评论
为什么被折叠?



