Fixing Keras Tensorflow ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=, found ndim=. Full shape received in Python

Keras Tensorflow ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=, found ndim=. Full shape received in Python is an error which occurs with Keras when you work with an image that has dimensions lower than what Keras expects.

My goal today is to provide a clear and detailed explanation of why this error is happening and how to solve it, we will also check out other ways to get rid of this problem for good.

Exploring the Keras Tensorflow ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=, found ndim=. Full shape received in Python

This is an error which occurs with Keras when you work with an image that has dimensions lower than what Keras expects.

The error should look like this. Double check in order to avoid mixing between errors.

                                                                       #
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, 
found ndim=3. Full shape received: [x, y, z]
                                                                       #

Bellow, I am going to explain the cause of the problem and propose multiple options.

The Method that has fixed my issue : Make sure the image dimensions are equal to those expected by Keras

Keras has a specific data format that you should respect. If you work with an image which has dimensions lower than the dimensions expected by Keras you are going to face the same error we are facing here.

When you do something like this

                                                                       #
(x, y), (testA, testB) = tf.keras.datasets.mnist.load_data()
                                                                       #

You can sometimes get an image with 3 dimensions instead of an image with 4 dimensions especially when you work with grayscale images.

To fix this, you can add an extra dimension like this

                                                                       #
mydata = tf.expand_dims(mydata, axis=-1)
                                                                       #

You can also use this line in order to add the extra dimension

                                                                       #
mydata = np.expand_dims(mydata, axis=-1)
                                                                       #

You can do this, if you work with a grayscale image for example

                                                                       #
mydata = mydata.reshape(-1, 28, 28, 1)
                                                                       #

I hope the fixes provided above solved your problem, thank you so much for reading this post to the end, cheers.

Summing-up : 

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/