MaxPooling2D((2, 2), border_mode='same')

783 views
Skip to first unread message

Mark Thompson

unread,
May 23, 2016, 11:27:42 PM5/23/16
to Keras-users

I'm working through the CNN based encoder example (at http://blog.keras.io/building-autoencoders-in-keras.html).

Question: border_mode for MaxPooling2D layer does not make sense to me. (It does make sense to me for the Convolution layers though).

The example uses:  MaxPooling2D((2, 2), border_mode='same')  which implies that the feature map is zero-padded by 1 before pooling? ...or does it imply a stride of 1?

Would someone enlighten me on this particular issue?

Thanks in advance,
Mark

Klemen Grm

unread,
May 24, 2016, 6:45:32 AM5/24/16
to Keras-users
Yes, if you look at the pool2d backend function that implements 2d pooling operations, the border_mode argument has similar functionality as it does in convolutional layers.

Mark Thompson

unread,
May 24, 2016, 12:37:56 PM5/24/16
to Keras-users

Thanks Klemen.  Also, thanks for reminding me to actually read the code (it's right there clear as day).  :)

Please consider the encoding layers below.  If I've calculated correctly to end up with 4x4x8 at the end.

My only issue is the MaxPool in Layer 1 which appears to have a ragged extra end that does not work (are these ends discarded?)


pooling (2,2), stride = 2, padding = 1 on only one sides means dimensions increase by 1 for width and height

conv2d padding = 1 on all sides means dimensions increase by 2 for width and height


MNIST input:  28,28,1

# Layer 1
x = Convolution2D(16, 3, 3, input_shape=(1, 28, 28), activation='relu', border_mode='same')(input_img)
28x28 -> 30x30 (with padding), convolve with 3x3 gives 28x28
resulting size: 16x28x28

x = MaxPooling2D((2, 2), border_mode='same')(x)
28x28 -> 29x29 (padded), pool with 2x2 stride=2 gives 14x14 with remainder of 1 (discard ragged ends?  what happens here?)
resulting size: 16x14x14

# Layer 2
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
14x14 -> 15x15 (padded), conv 3x3 give 13x13
resulting size: 8x13x13

x = MaxPooling2D((2, 2), border_mode='same')(x)
13x13 -> 14x14 (padded), pool 2x2 stride=2 gives 7x7
resulting size: 8x7x7

# Layer 3
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
7x7 -> 9x9 (padded), conv 3x3 gives 7x7
resulting size: 8x7x7

x = MaxPooling2D((2, 2), border_mode='same')(x)
7x7 -> 8x8 (padded), pool 2x2, stride=2 gives 4x4
resulting size: 8x4x4

# at this point the representation is (8, 4, 4) i.e. 128-dimensional


Mark Thompson

unread,
May 24, 2016, 12:41:33 PM5/24/16
to Keras-users

Ah, I see it!  In the Theano code, pool.pool_2d, "ignore_border" argument = True (for keras) and this will drop the ragged ends.


Reply all
Reply to author
Forward
0 new messages