我正在尝试制作灰度图像的模型。 看起来输出形状有问题,我尝试向 conv2d 添加填充,但它在测试中给了我输入形状的错误。
实施:
model=keras.Sequential()
model.add(Conv2D(64, kernel_size=(48, 48), activation='relu', input_shape=(105,105,1)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Conv2D(128, kernel_size=(24, 24), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2DTranspose(128, (24,24), strides = (2,2), activation = 'relu', padding='same', kernel_initializer='uniform'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(64, (12,12), strides = (2,2), activation = 'relu', padding='same', kernel_initializer='uniform'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(
Dense(4096,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2383,activation='relu'))
model.add(Dense(5, activation='softmax'))
错误:
ValueError: One of the dimensions in the output is <= 0 due to downsampling in conv2d_9. Consider increasing the input size. Received input shape [None, 105, 105, 1] which would produce output shape with a zero or negative value in a dimension.
有点晚了,但如果你使用 padding = 'same' 它应该也可以工作:
model.add(Conv2D(64, kernel_size=(48, 48), activation='relu', input_shape=(105,105,1), padding='same'))
这基本上保持输出大小与输入大小相同。
我认为错误在于从 (1, 105,105) 减去 (48,48) 的内核大小。尝试使用 data_format:
将输入从 (105, 105, 1) 更改为 (1, 105,105)model.add(Conv2D(64, kernel_size=(48, 48), activation='relu', input_shape=(105,105,1), data_format='channels_first')))
您可以在这里阅读: “conv2d_2/卷积”的 1 减 3 导致的负维度大小
对于我的一维 CNN 训练,添加“ padding='same' ”也对我有用。