C4W2 - Why RandomFlip requires a batched image?

I am struggling to understand this in the second assignment.

Found this in the provided code: augmented_image = data_augmentation(tf.expand_dims(first_image, 0))

I was wondering why tf.expand_dims call is needed in the above example, so I removed it to see what happens and I got an error ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [160, 160, 3]. I traced this in PyCharm’s debugger and think this comes from the use of RandomFlip in the data_augmenter, which is expecting a 4-dimentional input.

I kind of proved to myself that this is caused by following

rf = layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical")
print(rf.input_spec) # shows InputSpec(ndim=4)

When I check the document of RandomFlip: tf.keras.layers.RandomFlip  |  TensorFlow Core v2.6.0, it seems to suggest 3D input is also accepted: " 3D (unbatched) or 4D (batched) tensor with shape: (..., height, width, channels) , in "channels_last" format."

Even stranger, I also checked Resizing (tf.keras.layers.Resizing  |  TensorFlow Core v2.6.0), which also states 3D accepted in doc, and works as expected: (below does not error)
layers.experimental.preprocessing.Resizing(IMG_SIZE, IMG_SIZE)(image)
And when I print its input_spec:

rs = layers.experimental.preprocessing.Resizing(IMG_SIZE, IMG_SIZE)
print(rs.input_spec) #prints None

Why is this happening? Am I understanding the API’s correctly? Is there a rule of thumb for the input shape in TF?

I think the ability to accept unbatched tensor is perhaps a feature in the newer versions of tensorflow rather than 2.3.0 which the assignment uses, (where these preprocessing layers themselves are experimental). It should work properly in the newer versions of tensorflow (2.6.0 at the very least as the documentation is for that version) and take an unbatched 3D value.

Ah, thank you XpRienzo! Yes, looks like you are right. The v2.3 doc specifies 4D as required input: tf.keras.layers.experimental.preprocessing.RandomFlip