[英]Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (32, 28, 28)
I wrote a CNN function for fashion minst dataset, whenever I try to call it in the main, I receive this error: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3.我为 fashion minst 数据集写了一个 CNN function,每当我尝试在 main 中调用它时,我都会收到此错误:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 Full shape received: (32, 28, 28).
已收到完整形状:(32, 28, 28)。
Here's how did I call it:我是这样称呼它的:
cnn_model = create_CNN(28, 28, 3, 10)
Here's the CNN code:这是 CNN 代码:
def create_CNN(height, weight, channels, classes):
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(height, weight, channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(classes))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
def create_CNN(classes:int=10, input_shape:tuple=(28, 28, 3)):
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(classes, activation='softmax'))
model.compile(optimizer='adam', loss='Sparse_categorical_crossentropy', metrics=['accuracy'])
return model
and also reshape the value, if your data will store in x
variable then,并且重塑价值,如果你的数据将存储在
x
变量中,那么,
x = data # your Mnist Fashion Data
print(x.ndim, x.shape)# No. Of Dimensions 3 and Shape is (32, 28, 38)
x = data.reshape((32, 28, 28, 3)) # Reshaping the Data
print(x.ndim, x.shape) # After Reshaping, it No. Of Dimensiona 4 and shape is (32, 28, 28, 3)
I Hope, It will Work Properly.我希望,它会正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.