How to decode a list of bytes stored in tfrecords using TensorFlow?

Viewed 643

I have stored a varying number of images as a list of bytes:

img.append(trajectory_step['img'].tostring())
feature['img'] = _bytes_feature(img)
...
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())

I also made sure to save the number of images for later decoding (experiment_length).

Now I fail to decode the images in the following way:

features = {
'img': tf.VarLenFeature(tf.string),
...
}
parsed_features = tf.parse_single_example(example_proto, features)
img = tf.decode_raw(parsed_features['img'], out_type=tf.uint8)
img = tf.reshape(img, tf.stack([experiment_length, 120, 160, 3]))

Which yields following error:

TypeError: Expected string passed to parameter 'bytes' of op 'DecodeRaw', got of type 'SparseTensor' instead.

If I choose to use tf.FixedLenFeature I get the following error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Name: , Key: img, Index: 0. Number of bytes values != expected. Values size: 5 but output shape: []

How do I properly decode a list of bytes? And: Is tf.VarLenFeature in this case correct or should I use tf.FixedLenFeature?

Thank you

0 Answers
Related