【问题标题】:When to use decode_raw with FixedLenFeature?何时将 decode_raw 与 FixedLenFeature 一起使用?
【发布时间】:2022-04-14 23:28:00
【问题描述】:

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

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

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

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 应该是原始数字:

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?

我猜我只在将 numerical 数据写为字节字符串时才使用decode_raw——对吗?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

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

    来源:https://www.tensorflow.org/api_docs/python/tf/io/decode_raw

    编辑:更新源链接

    【讨论】:

    • 链接是404
    • 更新了链接以指向 tf repo 中的最新版本。
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 2021-01-02
    • 2010-12-09
    • 2019-07-31
    • 2014-02-12
    • 2017-01-27
    • 2012-02-29
    • 2017-01-18
    相关资源
    最近更新 更多