Search code examples
pythontensorflowlayerpooling

Proper use of tf.layers.MaxPooling


I'm building a model in Tensorflow using tf.layers objects. When I run the following code using tf.layers.MaxPooling2D my model does not reduce in size. I've only recently switched from using Keras to Tensorflow directly so I presume I'm misunderstanding the usage.

import tensorflow as tf
import numpy as np

features = tf.constant(np.random.random((20,128,128,3)), dtype=tf.float32)
y_true = tf.constant(np.random.random((20,1)), dtype=tf.float32)

print('features = %s' % features)
conv = tf.layers.Conv2D(32,(2,2),padding='same')(features)
print('conv = %s' % conv)
pool = tf.layers.MaxPooling2D((2,2),(1,1),padding='same')(conv)
print('pool = %s' % pool)

# and so on ...

I see this output:

features = Tensor("Const:0", shape=(20, 128, 128, 3), dtype=float32)
conv = Tensor("conv2d/BiasAdd:0", shape=(20, 128, 128, 32), dtype=float32)
pool = Tensor("max_pooling2d/MaxPool:0", shape=(20, 128, 128, 32), dtype=float32)

I was expecting to see the output from the MaxPool layer to have a shape of (20,64,64,32).

Am I using this this correctly?


Solution

  • If you want to downsample by a factor of 2 your feature map, you should use a stride 2.

    In [1]: tf.layers.MaxPooling2D(2, 2, padding='same')(conv)
    Out[1]: <tf.Tensor 'max_pooling2d/MaxPool:0' shape=(20, 64, 64, 32) dtype=float32>