As the following piece of code shows, the tensorflow tf.nn.dilation2D
function doesn't behave as a conventional dilation operator.
import tensorflow as tf
tf.InteractiveSession()
A = [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
kernel = tf.ones((3,3,1))
input4D = tf.cast(tf.expand_dims(tf.expand_dims(A, -1), 0), tf.float32)
output4D = tf.nn.dilation2d(input4D, filter=kernel, strides=(1,1,1,1), rates=(1,1,1,1), padding="SAME")
print(tf.cast(output4D[0,:,:,0], tf.int32).eval())
Returns the following tensor:
array([[1, 1, 1, 2, 2, 2, 1],
[1, 1, 2, 2, 2, 2, 2],
[1, 1, 2, 2, 2, 2, 2],
[1, 1, 2, 2, 2, 2, 2],
[1, 1, 1, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1]], dtype=int32)
I don't understand neither why it behaves like that, neither how I should use tf.nn.dilation2d
to retrieve the expected output:
array([[0, 0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0]], dtype=int32)
Can someone enlighten the succinct documentation of tensorflow and give an explanation of what the the tf.nn.dilation2D
function does ?