如何使用可变长度字符串解码TFRecord数据样本?

6 python tensorflow

假设我们有一个TFRecord文件,其数据样本如下:

def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float32_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))

example = tf.train.Example(features=tf.train.Features(feature={
    'image/encoded': _bytes_feature(encoded_jpg),
    'label': _float_list_feature(label),
}))
Run Code Online (Sandbox Code Playgroud)

这encoded_jpg是编码的32x32 jpg图像的原始值,不同图像的长度可能完全不同; label是一个固定长度的矢量.

对于固定长度的字段,可以使用以下内容来解码样本:

features = tf.parse_single_example(
    serialized_example,
    features = {
        'image/encoded': tf.FixedLenFeature([], tf.string)
        'label': tf.FixedLenFeature([], tf.float32)
    }

)
Run Code Online (Sandbox Code Playgroud)

但这里的长度image/encoded不是恒定的,前面提到的不再适用.

如果我将代码更改为:

features = tf.parse_single_example(
    serialized_example,
    features = {
        'image/encoded': tf.VarLenFeature(tf.string)
        'label': tf.FixedLenFeature([], tf.float32)
    }
)

encoded = features['image/encoded']
Run Code Online (Sandbox Code Playgroud)

image/encoded 就像稀疏张量一样,我不知道如何从这些东西中解码图像.

以前有没有类似的经历?任何建议表示赞赏.

谢谢!

小智 5

以下代码可能有用:

转换为 tfrecord:

ex = tf.train.SequenceExample()
ex.context.feature["length"].int64_list.value.append(label)
ex_tokens = ex.feature_lists.feature_list["image/encoded"]
for value in range(encoded_jpg):
    ex_tokens.feature.add().int64_list.value.append(value)

with tf.python_io.TFRecordWriter(os.path.join(DATA_PATH, filename) + ".tfrecord") as filew:
    filew.write(ex.SerializeToString())
Run Code Online (Sandbox Code Playgroud)

读取 tfrecord

context_features = {
    "pose": tf.FixedLenFeature([], dtype=tf.float32)
}

sequence_features = {
    "image/encoded": tf.FixedLenSequenceFeature([], dtype=tf.int64),
}

tf_reader = tf.TFRecordReader()
tf_key, tf_serialized = tf_reader.read(tf_file_queue)
tf_context, tf_sequence = tf.parse_single_sequence_example(
    serialized = tf_serialized,
    context_features = context_features,
    sequence_features = sequence_features
)
encoded = tf_sequence['image/encoded']
Run Code Online (Sandbox Code Playgroud)