I was trying to save images of different sizes into tf-records. I found that even though the images have different sizes, I can still load them with FixedLenFeature
.
By checking the docs on FixedLenFeature
and VarLenFeature
, I found the difference seems to be that VarLenFeauture
returns a sparse tensor.
Could anyone illustrate some situations one should use FixedLenFeature
or VarLenFeature
?
You can load images probably beacause you saved them using feature type tf.train.BytesList()
and whole image data is one big byte value inside a list.
If I'm right you're using tf.decode_raw
to get the data out of the image you load from TFRecord.
Regarding example use cases:
I use VarLenFeature
for saving datasets for object detection task:
There's variable amount of bounding boxes per image (equal to object in image) therefore I need another feature objects_number
to track amount of objects (and bboxes).
Each bounding box itself is a list of 4 float coordinates
I'm using following code to load it:
features = tf.parse_single_example(
serialized_example,
features={
# We know the length of both fields. If not the
# tf.VarLenFeature could be used
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64),
# Label part
'objects_number': tf.FixedLenFeature([], tf.int64),
'bboxes': tf.VarLenFeature(tf.float32),
'labels': tf.VarLenFeature(tf.int64),
# Dense data
'image_raw': tf.FixedLenFeature([],tf.string)
})
# Get metadata
objects_number = tf.cast(features['objects_number'], tf.int32)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
# Actual data
image_shape = tf.parallel_stack([height, width, depth])
bboxes_shape = tf.parallel_stack([objects_number, 4])
# BBOX data is actually dense convert it to dense tensor
bboxes = tf.sparse_tensor_to_dense(features['bboxes'], default_value=0)
# Since information about shape is lost reshape it
bboxes = tf.reshape(bboxes, bboxes_shape)
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, image_shape)
Notice that "image_raw" is fixed length Feature (has one element) and holds values of type "bytes", however a value of "bytes" type can itself have variable size (its a string of bytes, and can have many symbols within it). So "image_raw" is a list with ONE element of type "bytes", which can be super big.
To further elaborate on how it works: Features are lists of values, those values have specific "type".
Datatypes for features are subset of data types for tensors, you have:
You can check here tensors data types.
So you can store variable length data without VarLenFeatures
at all (actually well you do it), but first you would need to convert it into bytes/string feature, and then decode it.
And this is most common method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With