简体   繁体   English

Keras:为什么 Conv2D 层的 output 大小与特征 map 的预期形状不匹配?

[英]Keras: Why the output size of a Conv2D layer doesn't match the expected shape of feature map?

I am trying to run a convolutional neural.network on MNIST images (28x28 gray-scale images) as inputs.我正在尝试在 MNIST 图像(28x28 灰度图像)上运行卷积神经网络作为输入。 To do it, I am using Keras (code is shown below).为此,我使用 Keras(代码如下所示)。 What I am having trouble understanding is why the shape of the convolutional layers doesn't match the shape I would expect them to have.我无法理解的是为什么卷积层的形状与我期望的形状不匹配。

Particularizing to the first convolutional layer, what I am trying to use are 32 feature maps, convolving all of them with 3x3 kernels.特别是第一个卷积层,我尝试使用的是 32 个特征图,将它们全部与 3x3 内核进行卷积。 The batch-size I am using is 200.我使用的批量大小是 200。

Given this I would expect that the shape of of the first Conv2D layer was (200, 26, 26, 32) ie 200 samples processed (due to batch size), 32 feature maps whose size was 26x26 (due to the convolution applied to the 28x28 input images with a 3x3 kernel).鉴于此,我预计第一个 Conv2D 层的形状为(200, 26, 26, 32),即处理了 200 个样本(由于批量大小),32 个大小为26x26的特征图(由于卷积应用于具有 3x3 内核的 28x28 输入图像)。

However, the size I am getting by using model.summary() is (200, 9, 9, 32) as shown below:但是,我使用model.summary()得到的大小是(200, 9, 9, 32) ,如下所示:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (200, 9, 9, 32)           320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (200, 4, 4, 32)           0         
_________________________________________________________________
dropout_1 (Dropout)          (200, 4, 4, 32)           0         
_________________________________________________________________
flatten_1 (Flatten)          (200, 512)                0         
_________________________________________________________________
dense_2 (Dense)              (200, 256)                131328    
_________________________________________________________________
dense_3 (Dense)              (200, 10)                 2570      
=================================================================
Total params: 134,218
Trainable params: 134,218
Non-trainable params: 0
_________________________________________________________________

The code I am using is:我使用的代码是:

%tensorflow_version 2.x
# MNIST
from tensorflow.keras.datasets import mnist
from keras.utils import np_utils
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(X_train.shape[0],28,28,1).astype('float32')
X_test  = X_test.reshape(X_test.shape[0],28,28,1).astype('float32')

X_train = X_train/255
X_test  = X_test/255

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

model = Sequential()
model.add(Convolution2D(32,3,3,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256,activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

history = model.fit(X_train, y_train, validation_data = (X_test, y_test), epochs = 10, batch_size = 200, verbose = 2)

model.summary()

Thanks in advance!提前致谢!

It is due to the fact that Convolution2D(32, 3, 3) is a convolution layer with 32 features maps in output, a kernel size of 3x3 and a stride of 3 .这是因为Convolution2D(32, 3, 3)是一个卷积层,在 output 中有 32 个特征映射,kernel 大小为 3x3,步幅为 3

What you meant is probably Convolution2D(32, (3, 3)) or simply Convolution2D(32, 3) .您的意思可能是Convolution2D(32, (3, 3))或只是Convolution2D(32, 3)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM