Custom median pooling in tensorflow

我怕爱的太早我们不能终老 提交于 2019-12-23 20:14:23

问题


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 ?


回答1:


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.




回答2:


As of March 2017, an easier answer (that under the hood works similarly to how Alex suggested) is to do this:

patches = tf.extract_image_patches(x, [1, k, k, 1], [1, k, k, 1], 4*[1], 'VALID')
medians = tf.contrib.distributions.percentile(patches, 50, axis=3)



回答3:


For me, Alex's answer is not working for tf 1.4.1. tf.top_k should be tf.nn.top_k and should get values of tf.nn.top_k

Also, if the input is [1, H, W, C], either answer could not only work on height and width and neglect the channel.




回答4:


Channel-wise median-pooling can be done by some addition reshapes on top of other answers:

# assuming NHWC layout
strides = rates = [1, 1, 1, 1]
patches = tf.extract_image_patches(x, [1, k, k, 1], strides, rates, 'VALID')
batch_size = tf.shape(x)[0]
n_channels = tf.shape(x)[-1]
n_patches_h = (tf.shape(x)[1] - k) // strides[1] + 1
n_patches_w = (tf.shape(x)[2] - k) // strides[2] + 1
n_patches = tf.shape(patches)[-1] // n_channels
patches = tf.reshape(patches, [batch_size, k, k, n_patches_h * n_patches_w, n_channels])
medians = tf.contrib.distributions.percentile(patches, 50, axis=[1,2])
medians = tf.reshape(medians, (batch_size, n_patches_h, n_patches_w, n_channels))

Not very efficient though.



来源:https://stackoverflow.com/questions/41522517/custom-median-pooling-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!