【发布时间】:2019-07-08 06:32:52
【问题描述】:
如以下代码所示,tensorflow tf.nn.dilation2D function 的行为不像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())
返回以下张量:
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)
我不明白为什么它的行为是这样的,也不明白如何我应该使用tf.nn.dilation2d 来检索预期的输出:
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)
有人可以启发 tensorflow 的简洁文档并解释 tf.nn.dilation2D 函数的作用吗?
【问题讨论】:
-
@CrisLuengo,我尝试通过执行此帖子中的第一个代码框来重现该行为。我得到“AttributeError:模块'tensorflow'没有属性'InteractiveSession'”。当我删除“tf.InteractiveSession”时,我得到“TypeError: dilation2d_v2() got an unexpected keyword argument 'filter'”。您能否发布一个适用于 tensorflow v2.4.1 的代码版本?
-
@Mark,这不是我的代码。我从来没有使用过 tensorflow 进行图像处理。
-
谢谢,克里斯。 @Jav,有 cmets 吗?
-
这个问题是 2 年前提出的,因此适用于 tf1.14。如果要将 tf1 代码转换为 tf2,请阅读 tf2 文档。
-
将问题更新为 tensorflow2 无关紧要,因为此后行为可能已发生变化。
标签: python tensorflow image-processing mathematical-morphology image-morphology