首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TFRecord -将png转换为字节

TFRecord -将png转换为字节
EN

Stack Overflow用户
提问于 2018-12-31 04:18:35
回答 1查看 541关注 0票数 1

创建tfrecord的代码:

代码语言:javascript
复制
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

我的问题是,当我从文件中读取时,图像无法正确解码:

代码语言:javascript
复制
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`

有人知道这是为什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-02 02:38:43

找到了解决方案,希望我的愚蠢错误能帮助其他人。

当将张量重塑为4维的张量[batch_size, height, width, channels]时,我切换了宽度和高度。

正确的整形代码是:

代码语言:javascript
复制
x_reshaped = session.run(tf.reshape(tensor=decoded_png_uint8, shape=[batch_size, height, width, channels], name="x_reshaped"))

但是我有shape=[batch_size, width, height, channels]。啊,好吧。每一天都是上学的日子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53981111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档