Search code examples
pythonnumpytensorflowconv-neural-networkmax-pooling

Max pool a single image in tensorflow using "tf.nn.avg_pool"


I want to apply "tf.nn.max_pool()" on a single image but I get a result with dimension that is totally different than the input:

import tensorflow as tf
import numpy as np

ifmaps_1 = tf.Variable(tf.random_uniform( shape=[ 7, 7, 3], minval=0, maxval=3, dtype=tf.int32))

ifmaps=tf.dtypes.cast(ifmaps_1, dtype=tf.float64)

ofmaps_tf = tf.nn.max_pool([ifmaps], ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="SAME")[0] # no padding

init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    print("ifmaps_tf = ")
    print(ifmaps.eval())
    print("ofmaps_tf = ")
    result = sess.run(ofmaps_tf)
    print(result)

I think this is related to trying to apply pooling to single example not on a batch. I need to do the pooling on a single example.

Any help is appreciated.


Solution

  • Your input is (7,7,3), kernel size is (3,3) and stride is (2,2). So if you do not want any paddings, (state in your comment), you should use padding="VALID", that will return a (3,3) tensor as output. If you use padding="SAME", it will return (4,4) tensor.

    Usually, the formula of calculating output size for SAME pad is:

    out_size = ceil(in_sizei/stride)
    

    For VALID pad is:

    out_size = ceil(in_size-filter_size+1/stride)