首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么时候将decode_raw与FixedLenFeature结合使用?

什么时候将decode_raw与FixedLenFeature结合使用?
EN

Stack Overflow用户
提问于 2018-02-19 12:02:58
回答 1查看 1.2K关注 0票数 1

我对TFRecord解码和编码的工作方式非常困惑,特别是使用tf.train.Feature(bytes_list=tf.train.BytesList(...))进行编码,然后再对其进行解码。

我想编写三种类型的数据--我的TFRecord:宽度/高度值(整数)、图像数据和字符串标签。我在网上查看了代码示例,我不确定何时需要使用decode_raw,何时不需要使用。

例如,假设这是我写它时的记录:

代码语言:javascript
复制
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

example = tf.train.Example(features=
            tf.train.Features(feature={
                'width': _int64_feature(256),
                'height': _int64_feature(256),
                'label': tf.train.Feature(bytes_list=tf.train.BytesList(
                    value=['LABEL{}'.format(np.random.choice(range(5))).encode('utf-8')]
                    )),
                'image_raw': _bytes_feature(raw)
                }))

阅读后,我对image_raw特性进行了解码,因为它将字节字符串转换为数字,这正是我想要的。但是,我的label应该是原始字符串,width/height应该是原始数字:

代码语言:javascript
复制
features = {
        'width': tf.FixedLenFeature([], tf.int64),
        'height': tf.FixedLenFeature([], tf.int64),
        'label': tf.FixedLenFeature([], tf.string),
        'image_raw': tf.FixedLenFeature([], tf.string),
    }
parsed_features = tf.parse_single_example(example_proto, features)

# Decode the raw bytes so it becomes a tensor with type.
image = tf.decode_raw(parsed_features['image_raw'], tf.uint16)

# Is parsed_features['label'] a valid string now?
# Are parsed_features['width'] and ['height'] good to use now?

我猜只有当我把数字数据写成字节字符串时才使用decode_raw --这是对的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-19 17:45:09

是的,decode_raw只会输出数值类型,所以只在您想要将字节字符串转换为数字值时才使用它,就像您对图像所做的那样。

来源:raw

编辑:更新的源链接

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

https://stackoverflow.com/questions/48865823

复制
相关文章

相似问题

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