Varying Image sizes in Tensorflow Malaria dataset | Dealing with unclean tensorflow data I am trying to build a CNN based image recognition system for the Tensorflow malaria dataset. I loaded the dataset (~27k RGB images) using conventional tensorflow_datasets syntax.
After some data exploration, I found that all the images are not of the same size. A print statement for few instances is shown in below snippet:
import tensorflow_datasets as tfds

ds_train, ds_info = tfds.load('malaria', split='train', as_supervised=True,with_info=True)

ds = ds_train.take(5)  #selecting 5 images

for image, label in tfds.as_numpy(ds):
  print(type(image),image.shape, type(label), label)


OUTPUT
&lt;class 'numpy.ndarray'&gt; (103, 103, 3) &lt;class 'numpy.int64'&gt; 1
&lt;class 'numpy.ndarray'&gt; (106, 121, 3) &lt;class 'numpy.int64'&gt; 1
&lt;class 'numpy.ndarray'&gt; (139, 142, 3) &lt;class 'numpy.int64'&gt; 0
&lt;class 'numpy.ndarray'&gt; (130, 118, 3) &lt;class 'numpy.int64'&gt; 1

The varying sizes of images across the dataset affect the initial CNN layer as flattening each image tensor yields a different sized array.
I understand that all the images need to be converted to a common aspect ratio before the modelling step and we can achieve that by using padding or some other preprocessing techniques of keras.preprocessing.image but I am not sure about the steps to efficiently implement it.
I will be grateful if someone could provide an elegant way around this.
Thank you in advance!
#Here image is your batch.
# Add &quot;batch&quot; and &quot;channels&quot; dimensions
image = image[tf.newaxis, ..., tf.newaxis]
image.shape.as_list()  # [batch, height, width, channels]


tf.image.resize(image, [height,width])[0,...,0].numpy()