Reputation: 122
I tried to recreate this Flower Recognition CNN in Keras. The model seems to work, at least in the notebook (while getting predictions on the validation set), but I need to use the model somewhere else. Photos are 150x150 and this is how I build the CNN:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Conv2D(filters=96, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Conv2D(filters=96, kernel_size=(3,3), padding='Same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(5, activation="softmax"))
Still, when I try to predict the result locally with sample photos and model loaded :
model = model_from_json(open("model.json", "r").read())
model.load_weights('model.h5')
img = cv2.imread('/path/to/the/dir/testimage.jpg')
img = cv2.resize(img, (150, 150))
data = np.array(img)
model.summary()
result = model.predict(data)
I receive this error:
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 150, 3]
The output of model summary is:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 150, 150, 32) 2432
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 75, 75, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 75, 75, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 37, 37, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 37, 37, 96) 55392
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 18, 18, 96) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 18, 18, 96) 83040
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 9, 9, 96) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 7776) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 3981824
_________________________________________________________________
activation_1 (Activation) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 5) 2565
=================================================================
Total params: 4,143,749
Trainable params: 4,143,749
Non-trainable params: 0
I cannot understand why would it receive a different shape. I have read that maybe Reshape would do the thing, but I am using fit_generator
to fit on training set and I am not sure if that's approachable.
Upvotes: 3
Views: 3463
Reputation: 64
Make sure you have the first dimension as 1 (1 image to predict). Your model is expecting the None dimension to be number of samples, 2nd + 3rd to be the image resolution, and 4th to be your RGB channels for the image.
data = np.expand_dims(data, axis=0)
Will add an extra dimension to your first axis.
See: How can I add new dimensions to a Numpy array?
Upvotes: 3