Batching in Tensorflow1 and Tensorflow2

211 views Asked by At

I am trying to convert an image homography code from version TF1 to TF2, just a TF script conversion isn't working here. I am stuck with batching the datasets as the images, image_patch and image_Indices have different shapes. While TF1 had no problem in ingesting and batching the pack of dataset's but TF2 has trouble with it.

imgs= np.random.rand(11,240,320,3)
pts = np.random.randint(100, size =(11,8))
patch = np.random.rand(11,128,128,1)

imgs = tf.convert_to_tensor(imgs)
pts = tf.convert_to_tensor(pts)
patch = tf.convert_to_tensor(patch)

pts= tf.cast(pts,dtype=tf.float64)

tensorflow2:

    img_batch,pts_batch,patch_batch = tf.data.Dataset.from_tensor_slices([imgs,pts,patch]).shuffle(buffer_size=batch_size*4)

Here 11 is number of images, 240 and 320 are image dimensions and 3 is number of channels.

error -

tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [11,240,320,3] != values[2].shape = [11,128,128,1] [Op:Pack] name: component_0

tensorflow1:

tf.compat.v1.train.batch([imgs,pts,patch], batch_size=5)

output -

[<tf.Tensor 'batch_2:0' shape=(5, 11, 240, 320, 3) dtype=float64>,
 <tf.Tensor 'batch_2:1' shape=(5, 11, 8) dtype=float64>,
 <tf.Tensor 'batch_2:2' shape=(5, 11, 128, 128, 1) dtype=float64>]

How to batch datasets of different dimension in tensorflow2? Also running, "tf.compat.v1.train.batch()" doesn't work in TF2 (tensoflow version 2.3) as it gives eager execution error.

What is the correct way of batching such datasets in TF2?

1

There are 1 answers

0
Frederik Bode On

The problem here is not the batching, but the generation of the tf.data.Dataset itself. The error is caused by img_batch,pts_batch,patch_batch = tf.data.Dataset.from_tensor_slices([imgs,pts,patch]), noy by the .shuffle(batch_size=...).

I think .from_tensor_slices is too high level here, look into tf.data.Dataset.from_generator.