1
votes

I am doing image classification and I trained a model and saved a model. When I try to predict the model, it shows input error. I am building a classifier using ResNet Architecture and initially declared input_size as 224 x 224. Now I need to predict the class of the test image.

I converted the image into 224x224 numpy array. when I try the below code

#plot the figure
fig = plt.figure()

for num,data in enumerate(test_data):

    img_num = data[1]
    img_data = data[0]

    y = fig.add_subplot(9,3,num+1)
    orig = img_data
    data = img_data.reshape(1,IMG_SIZ,IMG_SIZ,3)

    #predict the model
    model_out = model.predict_classes([orig])[0]

    if np.argmax(model_out) == 1: str_label='Dog'
    else: str_label='Cat'

    y.imshow(orig,cmap = 'gray')
    plt.title(str_label)
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)


plt.show()
plt.savefig('test_labeled.jpg')

It shows me the following error

ValueError: cannot reshape array of size 50176 into shape (1,224,224,3)

in what size I have to reshape the correct dimension?

Thanks!

2
What is data[0]? Are you trying to resize an image by using .reshape()? - eugenhu
in test_data there are two variables grouped together. data[1] is image-id and data[0] is the image matrix of shape(224x224) - Madhi
Seems your input is of size [224, 224, 1] instead of [224, 224, 3], so reshape accordingly. - vijay m
I changed the dimensions into (224x224x1) but now this error popups ValueError: Error when checking input: expected resnet50_input to have shape (None, None, 3) but got array with shape (224, 224, 1) - Madhi
can you check the whole code? I post in the github gist code - Madhi

2 Answers

6
votes

Seems your input is of size [224, 224, 1] instead of [224, 224, 3]. Looks like you converting your inputs to gray scale in process_test_data()

you may need to change:

img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ))

to:

img = cv2.imread(path)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ),3)
6
votes

In my case function was expecting RGB image and it has failed cause it was RGBA one what automatically means it's got 4 channels instead of 3. so I've refurbished their function in order to be able to swallow RGBA

def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    if image.getdata().mode == "RGBA":
        image = image.convert('RGB')
    np_array = np.array(image.getdata())
    reshaped = np_array.reshape((im_height, im_width, 3))
    return reshaped.astype(np.uint8)