我想使用Tensorflow的。据我所知,它为每个通道执行常规2D卷积,每个通道都有depth_multiplier
个特征。
然后我应该期望,如果depth_multiplier = 1
,输入通道的数量与输出通道的数量相同。但为什么我有256个输入通道和512个输出通道?额外频道来自哪里?
答案 0 :(得分:5)
过滤器的大小为[filter_height, filter_width, in_channels, channel_multiplier]
。如果是channel_multiplier = 1
,那么您将获得与输出相同数量的输入通道。如果是N
,则N*input_channels
为output channels
,每个input channe
l与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')
你得到out
形状:shape=(20, 64, 64, 1000)
答案 1 :(得分:3)
我修改了@vijay m的代码以进一步说明问题。他的回答绝对正确。但是,我仍然没有得到它。
快速回答是"频道乘数"这个论点是一个令人困惑的名字。它可以被称为"您希望每个频道应用的过滤器数量"。因此,请注意此代码段的大小:
# 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]
它的大小允许您将10个不同的过滤器应用于每个输入通道。我已经创建了以前答案代码的一个版本,可能很有启发性:
final Logger rootLogger = Logger.getRootLogger();
for (Handler h: rootLogger.getHandlers())
rootLogger.removeHandler(h);