Qub*_*bix 3 convolution tensorflow
我想用depthwise_conv2d从Tensorflow。据我所知,它为每个通道执行常规的2D卷积,每个卷积都有depth_multiplier许多功能。
然后,如果depth_multiplier = 1,我应该期望输入通道的数量与输出通道的数量相同。但是为什么我可以有256个输入通道和512个输出通道?额外渠道从何而来?
我修改了 @vijay m 的代码以进一步说明。他的回答完全正确。然而,我还是没有明白。
快速回答是“通道倍增器”是该参数的一个令人困惑的名称。它可以称为“您希望每个通道应用的过滤器数量”。因此,请注意此代码片段的大小:
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]
Run Code Online (Sandbox Code Playgroud)
过滤器的大小[filter_height, filter_width, in_channels, channel_multiplier]。如果为channel_multiplier = 1,则您将获得与输出相同数量的输入通道。如果为N,则得到N*input_channelsas output channels,每个input channel与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')
Run Code Online (Sandbox Code Playgroud)
你得到out的形状:shape=(20, 64, 64, 1000)
| 归档时间: |
|
| 查看次数: |
2573 次 |
| 最近记录: |