Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom median pooling in tensorflow

I am trying to implement a median pooling layer in tensorflow.

However there is neither tf.nn.median_pool and neither tf.reduce_median.

Is there a way to implement such pooling layer with the python api ?

like image 605
user2682877 Avatar asked Feb 05 '23 09:02

user2682877


1 Answers

You could use something like:

patches = tf.extract_image_patches(tensor, [1, k, k, 1], ...)
m_idx = int(k*k/2+1)
top = tf.top_k(patches, m_idx, sorted=True)
median = tf.slice(top, [0, 0, 0, m_idx-1], [-1, -1, -1, 1])

To accommodate even sized median kernels and multiple channels, you will need to extend this, but this should get you most of the way.

like image 104
Alex Avatar answered Feb 08 '23 15:02

Alex