'Creating a tensorflow.data.Dataset for object detection containing bounding-box tensors with different shapes

I am trying to create a tf.data.Dataset to be able to train an object detection model. Besides images, the dataset will contain bounding-box and label information. For each image the number of bounding boxes/labels can differ.

Let's assume I have the following two tensors:

<tf.Tensor: shape=(1, 2, 4), dtype=float32, numpy=
array([[[0.12723215, 0.43973213, 0.46205357, 0.39285713],
        [0.5089286 , 0.19196428, 0.25223213, 0.25892857]]], dtype=float32)>,
<tf.Tensor: shape=(1, 3, 4), dtype=float32, numpy=
array([[[0.14285715, 0.45982143, 0.4174107 , 0.2857143 ],
        [0.546875  , 0.18973215, 0.24776785, 0.25892857],
        [0.66741073, 0.10044643, 0.01785714, 0.02455357]]], dtype=float32)>

With similar size tensors I can concatenate them tf.concat([tensor1, tensor2], axis=0) and create a dataset with tf.data.Dataset.from_tensor_slices(concatenated_tensor), but how do I get these tensors with different sizes into a dataset?



Sources

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

Source: Stack Overflow

Solution Source