【发布时间】: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