美文网首页
Tensorflow针对不定尺寸的图片读写tfrecord文件排

Tensorflow针对不定尺寸的图片读写tfrecord文件排

作者: CapsulE_07 | 来源:发表于2018-11-29 17:23 被阅读0次

上一篇文章描述了两种读取tfrecord的方式。
然而,在对比了两种方式生成的tfrecord占用的存储空间。 大约13G的图片数据,会生成54G大小的tfrecord, 所以不推荐使用tobytes 进行存储。

import cv2
import io
import PIL
image_fg_path = "D:\\dl_projectX\\matting\\51.jpg"
image_fg = cv2.imread(image_fg_path)
print(image_fg.shape)

pil_image_fg = PIL.Image.fromarray(image_fg)
bytes_io = io.BytesIO()
pil_image_fg.save(bytes_io, format='JPEG')
encoded_fg = bytes_io.getvalue()
print(len(image_fg.tobytes()))
print(len(encoded_fg))
image = PIL.Image.open(io.BytesIO(encoded_fg))
print(image)
#结果
(801, 640, 3)
1537920
164170
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x801 at 0x75697B8>

读取时可以使用tf.image.decode_jpeg 进行解码读取

def _tf_example_parser(serialized):
    features = {
        'image/encoded_image_fg':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format':
            tf.FixedLenFeature((), tf.string, default_value=''), 
        'imgae/width': 
            tf.FixedLenFeature([], tf.int64),
        'imgae/width': 
            tf.FixedLenFeature([], tf.int64)}
    image_fg = tf.image.decode_jpeg(parsed_example['image/encoded_image_fg'])
    image_fg = tf.image.convert_image_dtype(image_fg, dtype=tf.float32)
    image_fg = tf.image.resize_images(image_fg, [320, 320])

此处必须加上 image_fg = tf.image.convert_image_dtype(image_fg, dtype=tf.float32), 否则,resize时会因为图片数据类型不是float32而出现错误。


tf.image.decode_jpeg之后的图像 没转换数据类型直接resize的图片 转换数据类型且resize后的图片

相关文章

网友评论

      本文标题:Tensorflow针对不定尺寸的图片读写tfrecord文件排

      本文链接:https://www.haomeiwen.com/subject/fvlrqqtx.html