Reputation: 189
I am trying to detect location and values of local minima on a 2D image map using tensorflow. Since this is not trivial I was wondering what a robust and efficient way in tf might be?
So far I thought of simple horizontal and vertical convolutions using [-1 1] kernels.
Upvotes: 3
Views: 1622
Reputation: 97
You can find your local maxima with pooling like this:
import tensorflow as tf
def get_local_maxima(in_tensor):
max_pooled_in_tensor = tf.nn.pool(in_tensor, window_shape=(3, 3), pooling_type='MAX', padding='SAME')
maxima = tf.where(tf.equal(in_tensor, max_pooled_in_tensor), in_tensor, tf.zeros_like(in_tensor))
return maxima
For local minima it would be easiest to negate the input and then find the maxima, since for pooling_type only AVG and MAX are supported so far.
Why does this work? The only time the value at some index of in_tensor
is the same as the the value at the same index in max_pooled_in_tensor
is if that value was the highest in the 3x3 neighborhood centered on that index in in_tensor
.
Upvotes: 6