简体   繁体   English

层“sequential_14”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),找到的形状=(None, 32, 32, 3)

[英]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

Normalize Images by dividing pixles by 255通过将像素除以 255 来标准化图像

(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.

相关问题 ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:[无,256] - ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError: 层“sequential_2”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),发现形状=(None, 1, 256, 256, 3) - ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 1, 256, 256, 3) ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) 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] TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) - TensorFlow ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] 层“lstm”的输入 0 与层不兼容:预期形状=(1, None, 1),找到的形状=(32, 9, 1) - Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,33714,12),找到形状 =(无,12) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 33714, 12), found shape=(None, 12)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM