![](/img/trans.png)
[英]Cannot solve ValueError: Input 0 of layer sequential_1 is incompatible with the layer
[英]ValueError: Input 0 of layer sequential_1 is incompatible with the layer
我在 Keras 中编写了以下模型,但是在进行预测时,我遇到了 ValueError 。 我查看了 StackOverflow 上的其他问题,但无法与我的代码完全相关。
我的训练模型如下:
#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)
)
我尝试在另一个 .py 文件中使用以下代码进行预测:
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)
但问题是我收到的价值错误是:
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]
所以任何人都可以帮助我解决这个错误?
它基本上说你的第一层期望形状为(32, 32, 1)
ip_conv_0 = Conv2D(filters=32, kernel_size=kernelSize, input_shape=im_shape, activation=ip_activation)
,所以im_shape=(32,32,1)
在这里,但在预测时,它会(32,32,3)
形状为(32,32,3)
的 3 通道图像。
我认为您使用灰度图像训练了您的网络,并尝试使用不适合您构建的网络模型的彩色图像 (RGB) 进行推断。 您可以做的是,您可以使用形状(32,32,3)
图像训练模型,这不是我认为的选项,或者您可以使 RGB(彩色)图像灰度化,使图像具有形状(32,32,1)
,然后您可以推断出您的模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.