1
$\begingroup$

I have done research but cannot seem to find what's wrong here

I have created this model for Mnist digit clasification :

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D , MaxPool2D,Dropout,Flatten
from tensorflow.keras.datasets import mnist


model = Sequential()
model.add(Conv2D(filters = 32 , kernel_size = (3,3),activation ='relu' , input_shape = input_shape ))
model.add(Conv2D(64,(3,3),activation = 'relu'))
model.add(MaxPool2D(2,2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128,activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation ='softmax'))

and I have this image to feed into the model:

enter image description here

So i reshaped the image like this:

from skimage import data
from skimage.transform import resize
image = image_gray
image = resize(image, (28, 28,1)).shape
image

But when i try to predict using this image i get this error:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (3, 1)

I have tried multiple ways using skimage but I can't seem to get the model to fit. What can I do?

$\endgroup$
0
$\begingroup$

model.predict(img) requires a batch axis, i.e. img needs to have a shape of (BS, H, W, C). Try model.predict(np.expand_dims(img,0)).

$\endgroup$
  • $\begingroup$ I'm getting this error : ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1, 3) $\endgroup$ – Ankit Chawla Jul 14 '20 at 8:33
  • $\begingroup$ Shouldn't this line: image = resize(image, (28, 28,1)).shape be image = resize(image, (28, 28,1)) ? $\endgroup$ – Vincent Yong Jul 14 '20 at 8:37
  • $\begingroup$ oh yeah that worked . $\endgroup$ – Ankit Chawla Jul 14 '20 at 9:14
  • $\begingroup$ wait but now I'm getting this new error: ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (28, 28, 1) but i think that it matches the shape right? $\endgroup$ – Ankit Chawla Jul 14 '20 at 9:21
  • $\begingroup$ Did you use model.predict(np.expand_dims(img,0))? Your img array has 3 dimensions now: (H,W,C). To predict, you need a batch axis as the first dimension. $\endgroup$ – Vincent Yong Jul 14 '20 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.