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

Viewed 371

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?

2 Answers

since you don't include your code, i'll assume that you have a problem with your input layer. So, you need to specified the number of unit and inpt dim into your input layer first:

model = Sequential()
model.add(Dense(X.shape[1], activation='something you desired', input_dim=X.shape[1]))

and so on

hard to understand what are you dealing with and what you want to achieve since you don't specified / share anything, not even the code.

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.

Related