Keras Convolution2D Input: error while checking model input: it is expected that convolution2d_input_1 will take the form

I work through this great tutorial on creating an image classifier using Keras. Once I have prepared the model, I will save it to a file, and then reload it into the model in the test script shown below.

I get the following exception when evaluating a model using a new, never-before-seen image:

Mistake:

Traceback (most recent call last): File "test_classifier.py", line 48, in <module> score = model.evaluate(x, y, batch_size=16) File "/Library/Python/2.7/site-packages/keras/models.py", line 655, in evaluate sample_weight=sample_weight) File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 1131, in evaluate batch_size=batch_size) File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 959, in _standardize_user_data exception_prefix='model input') File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 108, in standardize_input_data str(array.shape)) Exception: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 150, 150) but got array with shape (1, 3, 150, 198)` 

Is there a problem with the model I trained, or with the way I call the evaluation method?

the code:

  from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Convolution2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img import numpy as np img_width, img_height = 150, 150 train_data_dir = 'data/train' validation_data_dir = 'data/validation' nb_train_samples = 2000 nb_validation_samples = 800 nb_epoch = 5 model = Sequential() model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.load_weights('first_try.h5') img = load_img('data/test2/ferrari.jpeg') x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150) x = x.reshape( (1,) + x.shape ) # this is a Numpy array with shape (1, 3, 150, 150) y = np.array([0]) score = model.evaluate(x, y, batch_size=16)` 
+7
python numpy theano machine-learning keras
source share
3 answers

The problem was doubled:

  • The test image was incorrect. It was 150 x 198 and should have been 150 x 150.

  • I had to change the dense layer from model.add(Dense(10)) to model.add(Dense(1)) .

I still do not understand how to make the model give me a forecast, but at least for now, the model is being evaluated.

+1
source share

The problem is due to the wrong size of the test images. For me

 train_datagen.flow_from_directory( 'C:\\Users\\...\\train', # this is the target directory target_size=(150, 150), # all images will be resized to 150x150 batch_size=32, class_mode='binary') 

It does not work properly. So I used the matlab command to resize all test images, and it worked great

+5
source share

I have the same problem and I use this function: All images in the target folder (.jpg and .png) will be resized to height and width. And it is divided by 255. Plus, I added another 1 size (the required input form).

 from scipy import misc import os def readImagesAsNumpyArrays(targetPath, i_height, i_width): files = os.listdir(targetPath) npList = list() for file in files: if ".jpg" or ".png" in str(file): path = os.path.join(targetPath, file) img = misc.imread(path) img = misc.imresize(img, (i_height, i_width)) img = img * (1. / 255) img = img[None, :, :,: ] npList.append(img) return npList 
0
source share

All Articles