[英]nput 0 of layer "sequential_14" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 32, 32, 3)
The Code is as follows:-守则如下:-
from tensorflow.keras.applications.mobil.net import preprocess_input
**Import of Datasets**
from tensorflow.keras.datasets import cifar10
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
Normalize pixel values to be between 0 and 1将像素值标准化为介于 0 和 1 之间
train_images, test_images = train_images / 255.0, test_images / 255.0
Convert Labels to Categories将标签转换为类别
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i])
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
Creating Convolution Base创建卷积基
m1 = Sequential()
m1.add(layers.Conv2D(128, (3, 3), activation='relu', input_shape=(256,256,3)))
m1.add(layers.MaxPooling2D((2, 2)))
m1.add(layers.Conv2D(64, (3, 3), activation='relu'))
m1.add(layers.MaxPooling2D((2, 2)))
m1.add(layers.Conv2D(32, (3, 3), activation='relu'))
m1.summary()
**Add Dense Layer On Top**
m1.add(layers.Flatten())
m1.add(layers.Dense(64, activation='relu'))
m1.add(layers.Dense(10))
m1.summary()
Compile And Train The CNN Architechture`编译和训练 CNN 架构`
After this Step the Error is Occuring在此步骤之后发生错误
m1 = Sequential()
m1.add(Conv2D(128,(3,3),activation='relu',input_shape=(256,256,3)))
m1.add(MaxPooling2D(pool_size=(2,2)))
m1.add(Conv2D(64,(3,3),activation='relu'))
m1.add(MaxPooling2D(pool_size=(2,2)))
m1.add(Conv2D(64,(3,3),activation='relu'))
m1.add(MaxPooling2D(pool_size=(2,2)))
m1.add(Flatten())
m1.add(Dense(32,activation='relu'))
m1.add(Dense(16,activation='relu'))
m1.add(Dense(10,activation='softmax'))
m1.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['acc']) # multiclass and therefore categorical_crossentropy
h1 = m1.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=10)
In the sequentail model, you are passing the input_shape
as (256,256,3)
in the first layer as shown below.在 sequentail model 中,您在第一层中将input_shape
作为(256,256,3)
传递,如下所示。
m1.add(layers.Conv2D(128, (3, 3), activation='relu', input_shape=(256,256,3)))
This means you are instructing your model to take images that are 256 pixels in height as well as width and have 3 channels namely RGB.这意味着您正在指示您的 model 拍摄高度和宽度均为 256 像素且具有 3 个通道(即 RGB)的图像。
But, pay attention now, you are passing cifar10 dataset to your model. Their shape is (32,32,3)
.但是,现在请注意,您正在将 cifar10 数据集传递给您的 model。它们的形状是(32,32,3)
。 Hence the error.因此错误。 Image size given and the size expected by model dont match.给定的图像大小与 model 预期的大小不匹配。
Do the following change in your first layer of model ie在您的第一层 model ie 中进行以下更改
m1.add(layers.Conv2D(128, (3, 3), activation='relu', input_shape=(32,32,3)))
This should remove this error.这应该消除此错误。
In the error you will notice a None
in shape ie None,256,256,3
.在错误中,您会注意到形状为None
,即None,256,256,3
。 It signifies batch size.它表示批量大小。 Models can take images in batch mode.模型可以以批处理模式拍摄图像。 They can take more than one image for training and prediction.他们可以拍摄不止一张图像进行训练和预测。 Since batch size is not fixed for anyone, it is represented as None.由于批量大小对任何人都不是固定的,因此它表示为 None。 It is left to the developer like you and me to decide whatever batch fits in our CPU or GPU.由像你我这样的开发人员来决定适合我们的 CPU 或 GPU 的批次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.