'When to use decode_raw with FixedLenFeature?

I'm pretty confused at the way TFRecord decoding and encoding work, specifically encoding using tf.train.Feature(bytes_list=tf.train.BytesList(...)) and then subsequently decoding it.

I want to write three kinds of data my TFRecord: width/height values (integer), image data, and a string label. I've looked at code samples online and I'm not sure when I need to use decode_raw and when I don't.

For example, let's say this is my record when I write it:

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)
                }))

Upon reading, currently I decode the image_raw feature because it turns a string of bytes into numbers, which is what I want. But my label should just be the original string and the width/height should be the original numbers:

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?

I'm guessing I only use decode_raw when I've written numerical data as a byte string -- is that correct?



Solution 1:[1]

Yes, decode_raw will only output numerical types so only use it when you want to convert your byte string to numerical values like you are doing with your image.

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

edit: Updated source link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1