Tensorflow:深度可分离卷积是什么?

4
我想使用Tensorflow中的depthwise_conv2d。据我现在所了解的,它为每个单独的通道执行常规的2D卷积,每个通道都有一个depth_multiplier数量的特征。
如果depth_multiplier = 1,则应该期望输入通道数与输出通道数相同。但是,为什么我可以有256个输入通道和512个输出通道?额外的通道从哪里来的?
2个回答

9
过滤器的大小为[过滤器高度,过滤器宽度,输入通道数,通道倍增器]。如果通道倍增器=1,那么输出通道数和输入通道数相同。如果其等于N,则您将得到N*输入通道数作为输出通道数,每个输入通道N个过滤器卷积。

例如,

inputs = tf.Variable(tf.random_normal((20, 64,64,100)))
filters = tf.Variable(tf.random_normal((5,5,100,10)))
out = tf.nn.depthwise_conv2d(
      inputs,
      filters,
      strides=[1,1,1,1],
      padding='SAME')

您当前的问题是形状不对:shape=(20, 64, 64, 1000)

7
我已经修改了@vijay m的代码,并进一步详细解释。他的答案是绝对正确的。然而,我仍然不理解。
简单来说,“channel multiplier”是该参数一个令人困惑的名称。它可以称为“您希望每个通道应用的过滤器数量”。因此,请注意此代码片段的大小: filters = tf.Variable(tf.random_normal((5,5,100,10))) 这个大小允许您将10个不同的过滤器应用于每个输入通道。我创建了前面答案代码的一个版本,可能会更有启发性:
# batch of 2 inputs of 13x13 pixels with 3 channels each.
# Four 5x5 filters applied to each channel, so 12 total channels output
inputs_np = np.ones((2, 13, 13, 3))
inputs = tf.constant(inputs_np)
# Build the filters so that their behavior is easier to understand.  For these filters
# which are 5x5, I set the middle pixel (location 2,2) to some value and leave
# the rest of the pixels at zero
filters_np = np.zeros((5,5,3,4)) # 5x5 filters for 3 inputs and applying 4 such filters to each one.
filters_np[2, 2, 0, 0] = 2.0
filters_np[2, 2, 0, 1] = 2.1
filters_np[2, 2, 0, 2] = 2.2
filters_np[2, 2, 0, 3] = 2.3
filters_np[2, 2, 1, 0] = 3.0
filters_np[2, 2, 1, 1] = 3.1
filters_np[2, 2, 1, 2] = 3.2
filters_np[2, 2, 1, 3] = 3.3
filters_np[2, 2, 2, 0] = 4.0
filters_np[2, 2, 2, 1] = 4.1
filters_np[2, 2, 2, 2] = 4.2
filters_np[2, 2, 2, 3] = 4.3
filters = tf.constant(filters_np)
out = tf.nn.depthwise_conv2d(
      inputs,
      filters,
      strides=[1,1,1,1],
      padding='SAME')
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    out_val = out.eval()

print("output cases 0 and 1 identical? {}".format(np.all(out_val[0]==out_val[1])))
print("One of the pixels for each of the 12 output {} ".format(out_val[0, 6, 6]))
# Output:
# output cases 0 and 1 identical? True
# One of the pixels for each of the 12 output [ 2.   2.1  2.2  2.3  3.   3.1  3.2  3.3  4.   4.1  4.2  4.3]

1
谢谢,非常详细的解释。 - Il'ya Zhenin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接