How to do minimum pooling in tensorflow?

Viewed 1059

Tensorflow has operations for average and max pooling, but not for minimum pooling.

Is there some sort of work around to get min pooling?

1 Answers

we can manually recreate MaxPooling in this way...

x = np.random.uniform(0,1, (5,30,30,3)).astype('float32')

n_channel = x.shape[-1]
patches = tf.image.extract_patches(x, 
                                   sizes = [1, 3, 3, 1], 
                                   strides = 4*[1], 
                                   rates = 4*[1], 
                                   padding = 'VALID')

channel_pool = [tf.reduce_max(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)]
res = tf.concat(channel_pool, axis=-1)

tf.reduce_all(res == MaxPool2D(pool_size=(3, 3), strides=(1,1), padding="valid")(x)) ## TRUE !!! 

given the example above, we can switch to MinPooling simply using tf.reduce_min

def min_pool(x):

    n_channel = x.shape[-1]
    patches = tf.image.extract_patches(x, 
                                       sizes = [1, 3, 3, 1], 
                                       strides = 4*[1], 
                                       rates = 4*[1], 
                                       padding = 'VALID')

    channel_pool = [tf.reduce_min(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)]

    return tf.concat(channel_pool, axis=-1)

we can wrap the whole procedure inside a Lambda layer in order to use it inside a keras model: Lambda(min_pool)(x)

Related