tensorflow - TFRecord - 将 png 转换为字节
问题描述
创建 tfrecord 的代码:
def convert(self):
with tf.python_io.TFRecordWriter(self.tfrecord_out) as writer:
example = self._convert_image()
writer.write(example.SerializeToString())
def _convert_image(self):
for (path, label) in zip(self.image_paths, self.labels):
label = int(label)
# Read image data in terms of bytes
with open(path, 'rb') as fid:
png_bytes = fid.read()
example = tf.train.Example(features=tf.train.Features(feature={
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[png_bytes]))
}))
return example
我的问题是当我从文件中读取图像无法正确解码时:
def parse(self, serialized):
features = \
{
'image': tf.FixedLenFeature([], tf.string)
}
parsed_example = tf.parse_single_example(serialized=serialized,
features=features)
image_raw = parsed_example['image']
image = tf.image.decode_png(contents=image_raw, channels=3, dtype=tf.uint8)
image = tf.cast(image, tf.float32)
return image`
有谁知道这是为什么?
解决方案
推荐阅读
- html - ul 中的条件换行符
- c - C中的执行时间差
- jquery - jQuery Materialize Css 选项卡下一个按钮
- python - OSError: [Errno 98] 地址已在 heroku 中使用
- python - 分隔符取所有列
- javascript - 如何防止 Slotted HTML 元素呈现,直到它位于 shadowRoot 内?
- json - 如何将lua类编码为json?
- html - 无法使用 If else 更改 context.FillStyle 颜色?
- pdf - 用 ghostscript 替换 PDFTK
- c# - 如何列出游戏对象上的所有组件排除变换以及如何销毁(删除)组件?