简体   繁体   English

ValueError: 层序列 1 的输入 0 与层不兼容

[英]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 .我在 Keras 中编写了以下模型,但是在进行预测时,我遇到了 ValueError 。 I looked at other questions on StackOverflow but could not relate exactly on my code.我查看了 StackOverflow 上的其他问题,但无法与我的代码完全相关。

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:我尝试在另一个 .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)

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)它基本上说你的第一层期望形状为(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) . ,所以im_shape=(32,32,1)在这里,但在预测时,它会(32,32,3)形状为(32,32,3)的 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.我认为您使用灰度图像训练了您的网络,并尝试使用不适合您构建的网络模型的彩色图像 (RGB) 进行推断。 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.您可以做的是,您可以使用形状(32,32,3)图像训练模型,这不是我认为的选项,或者您可以使 RGB(彩色)图像灰度化,使图像具有形状(32,32,1) ,然后您可以推断出您的模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解决 ValueError: 层序贯_1 的输入 0 与层不兼容 - Cannot solve ValueError: Input 0 of layer sequential_1 is incompatible with the layer Keras:ValueError:输入 0 层序列_1 与层不兼容:预期 ndim=3,发现 ndim=2 - Keras: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected ndim=3, found ndim=2 ValueError: 层“sequential_1”的输入 0 与层不兼容:预期形状=(None, 60, 1),发现形状=(None, 59, 1) - ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 60, 1), found shape=(None, 59, 1) “sequential_1 层的输入 0 与层不兼容”与 tf.data.Dataset - "Input 0 of layer sequential_1 is incompatible with the layer" with tf.data.Dataset ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError: 层序贯_1 的输入 0 与层不兼容: : 预期 min_ndim=4,发现 ndim=2。 收到的完整形状:(32768, 1) - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (32768, 1) ValueError: 层序号_3 的输入 0 与层不兼容: - ValueError: Input 0 of layer sequential_3 is incompatible with the layer: "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer ValueError: 层序号_2 的输入 0 与层不兼容 - ValueError: Input 0 of layer sequential_2 is incompatible with the layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM