maxpooling结果未显示在model.summary()输出中

时间:2019-03-01 15:31:45

标签: numpy tensorflow keras conv-neural-network max-pooling

我是Keras的初学者。我正试图建立一个我正在使用顺序模型的模型。当我试图通过使用maxpooling函数将输入大小从28减小到14或更小时,在调用model.summary()函数时不会显示maxpooling函数的结果。我正努力在训练后达到0.99或更高的精度,即在调用model.score()时,精度结果应为0.99或更高。 Model build my me so far can be seen here

from keras.layers import Activation, MaxPooling2D
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Convolution2D(32, 1, activation='relu'))
MaxPooling2D(pool_size=(2, 2))
model.add(Convolution2D(32, 26))
model.add(Convolution2D(10, 1))
model.add(Flatten())
model.add(Activation('softmax'))

model.summary()

输出-

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_29 (Conv2D)           (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_30 (Conv2D)           (None, 26, 26, 32)        1056      
_________________________________________________________________
conv2d_31 (Conv2D)           (None, 1, 1, 32)          692256    
_________________________________________________________________
conv2d_32 (Conv2D)           (None, 1, 1, 10)          330       
_________________________________________________________________
flatten_7 (Flatten)          (None, 10)                0         
_________________________________________________________________
activation_7 (Activation)    (None, 10)                0         
=================================================================
Total params: 693,962
Trainable params: 693,962
Non-trainable params: 0
____________________________

我正在使用的批量大小是32,纪元数是10。

model.compile(loss='categorical_crossentropy',
         optimizer='adam',
         metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)

score = model.evaluate(X_test, Y_test, verbose=0)
print(score)

培训后的输出-

[0.09016687796734459, 0.9814]

1 个答案:

答案 0 :(得分:0)

您没有在模型中添加Maxpooling2D图层...

model.add(MaxPooling2D(pool_size=(2, 2)))

此外,maxpooling的输出将具有形状(无,13、13、32),下一层(在您的情况下为26)的卷积核不能大于当前的尺寸(13)。您的代码应如下所示:

from keras.layers import Activation, MaxPooling2D, Dense

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Convolution2D(32, 1, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 8))
model.add(Convolution2D(10, 6))
model.add(Flatten())
model.add(Activation('softmax'))
print(model.summary())

输出

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 26, 26, 32)        1056      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 6, 6, 32)          65568     
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 1, 1, 10)          11530     
_________________________________________________________________
flatten_1 (Flatten)          (None, 10)                0         
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 78,474
Trainable params: 78,474
Non-trainable params: 0
___________________________________

P.S .:我会考虑在输出端使用较小的内核大小和FC层,因为在大多数情况下,这是比尝试匹配卷积输出形状更实用的解决方案