Search code examples
pythondeep-learningneural-networkconv-neural-networkimage-resizing

How to change my image into a desired shape in python?


I am working on CNN model for MNIST fashion dataset. I have created a successful CNN model. But I want to test the model for classification for another image that I downloaded from internet .

My all train and test set is of the shape (28, 28, 1). But now for the image I want to predict I resized it into (28,28) and it made it into one channel of RGB using

cv2.cvtColor(load_img_rz, cv2.COLOR_BGR2GRAY)

Now the shape of image is (28, 28). I tried to input it into the model and its shows error

ValueError: Input 0 of layer sequential_6 is incompatible with the layer: : expected min_ndim=4, 
found ndim=3. Full shape received: (None, 28, 3)

I think shape is the issue. So how can I convert it into the shape(28,28,1) if that is the issue.

And does CNN work better in one channel RGB than 3 channel RGB?


Solution

  • A very useful command for me in Deep Learning is the expand_dims from numpy.

    your_image.shape
    >>> (28, 28)
    
    your_new_array = np.expand_dims(your_image, axis=-1)
    your_new_array.shape
    >>> (28, 28, 1)
    

    You can play around with the axis parameter to get a better feeling of what is going on here.