awslabs / keras-apache-mxnet

[DEPRECATED] Amazon Deep Learning's Keras with Apache MXNet support
https://github.com/awslabs/keras-apache-mxnet/wiki
Other
290 stars 65 forks source link

mxnet keras padding strides, maybe not correct? #156

Open sandeep-krishnamurthy opened 6 years ago

sandeep-krishnamurthy commented 6 years ago

Issue by @timprepscius (Initially created at - https://github.com/apache/incubator-mxnet/issues/12221)

import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, LSTM, ConvLSTM2D, Activation, Reshape, Input, Concatenate, concatenate
from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras import backend as K

i = Input(shape=(1, 256, 256))
c0 = Conv2D(7, kernel_size=3, activation="relu", padding="same", strides=1)
m0 = MaxPooling2D(pool_size=3, padding="same", strides=2)
c1 = Conv2D(7, kernel_size=7, activation="relu", padding="same", strides=2)

l = i
l = c0(l)
l = m0(l)
l = c1(l)

m = Model(inputs=[i], outputs=[l])

m.compile(
    loss=keras.losses.mean_squared_error,
    optimizer=keras.optimizers.Adadelta()
)

print(m.summary())

results in:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 1, 256, 256)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 7, 256, 256)       70        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 128, 128)       0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 7, 64, 64)         2408      
=================================================================
Total params: 2,478
Trainable params: 2,478
Non-trainable params: 0
_________________________________________________________________
None

(The output sized changes when stride is not 1, even though padding was "same")

I think it should result in:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 1, 256, 256)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 7, 256, 256)       70        
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 256, 256)       0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 7, 256, 256)       2408      
=================================================================
Total params: 2,478
Trainable params: 2,478
Non-trainable params: 0
_________________________________________________________________
None

But I could be wrong of course.