简体   繁体   中英

ValueError: Input 0 of layer sequential_1 is incompatible with the layer

I wrote the following model in Keras but while doing predictions, I am encountering ValueError . I looked at other questions on StackOverflow but could not relate exactly on my code.

My Training model is as:

#building the CNN model
    cnn = Sequential()

kernelSize = (3, 3)
ip_activation = 'relu'
ip_conv_0 = Conv2D(filters=32, kernel_size=kernelSize, input_shape=im_shape, activation=ip_activation)
cnn.add(ip_conv_0)

# Add the next Convolutional+Activation layer
ip_conv_0_1 = Conv2D(filters=64, kernel_size=kernelSize, activation='relu')
cnn.add(ip_conv_0_1)
# Add the Pooling layer
pool_0 = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding="same")
cnn.add(pool_0)

ip_conv_1 = Conv2D(filters=64, kernel_size=kernelSize, activation='relu')
cnn.add(ip_conv_1)
ip_conv_1_1 = Conv2D(filters=64, kernel_size=kernelSize, activation='relu')
cnn.add(ip_conv_1_1)
pool_1 = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding="same")
cnn.add(pool_1)

# Let's deactivate around 20% of neurons randomly for training
drop_layer_0 = Dropout(0.2)
cnn.add(drop_layer_0)


flat_layer_0 = Flatten()
cnn.add(Flatten())

# Now add the Dense layers
h_dense_0 = Dense(units=128, activation='relu', kernel_initializer='uniform')
cnn.add(h_dense_0)
# Let's add one more before proceeding to the output layer
h_dense_1 = Dense(units=64, activation='relu', kernel_initializer='uniform')
cnn.add(h_dense_1)

op_activation = 'softmax'
output_layer = Dense(units=n_classes, activation='softmax', kernel_initializer='uniform')
cnn.add(output_layer)

opt = 'adam'
loss = 'categorical_crossentropy'
metrics = ['accuracy']
# Compile the classifier using the configuration we want
cnn.compile(optimizer=opt, loss=loss, metrics=metrics)

cnn_summary = cnn.summary()

history = cnn.fit(x_train, y_train,
                  batch_size=40, epochs=20,
                  validation_data=(x_test, y_test)
                  )

I try to predict using the following code in another .py file:

import numpy as np
from keras.preprocessing import image 

from keras.models import load_model
model=load_model('trained_model.h5')

test_image = image.load_img('131.png', target_size=(32,32))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
pre = model.predict(test_image)

But the problem is that I'm getting value error as:

ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 32, 32, 3]

So anyone can help me with this error?

It basically says that your first layer expect a shape of (32, 32, 1)

ip_conv_0 = Conv2D(filters=32, kernel_size=kernelSize, input_shape=im_shape, activation=ip_activation)

, so im_shape=(32,32,1) here, but instead when prediction, it recieves a 3 channel image with shape (32,32,3) .

I think you trained your network with grayscale images, and you try to make inferences with colored images (RGB), that does not suit the network model you built. What you can do is you can either train your model with images of shape (32,32,3) which is not an option I believe, or you can make your RGB (colored) image grayscale such that your image has a shape (32,32,1) , then you can infer with your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM