我目前正在尝试了解Tensorflow Depthwise Convolution的工作原理。据我所知,输入图像中的每个通道都与它自己的一组过滤器进行卷积,然后将结果连接起来。为了简化起见,我将坚持使用参数depth_multiplier=1
,所以n_inputchannels == n_outputchannels
。
所以从理论上讲,我可以将深度卷积分解为N
个体,定期Conv2Ds
,对吗?为什么下面的代码产生不同的结果然后我想知道 - 这是一个精确的问题吗?我正在关注深度卷积滤镜的排序[filter_height, filter_width, in_channels, 1]
和常规卷积的[filter_height, filter_width, in_channels, out_channels]
以及NHWC
数据格式的文档。
import tensorflow as tf
import numpy as np
import random
width = 128
height = 128
channels = 32
kernel_width = 3
kernel_height = 3
with tf.Session() as sess:
_input = np.float32(np.random.rand(1, height, width, channels))
_weights = np.float32(np.random.rand(kernel_height, kernel_width, channels, 1))
_input_ph = tf.placeholder(tf.float32, shape=(1, height, width, channels))
_weights_pc = tf.placeholder(tf.float32, shape=(kernel_height, kernel_width, channels, 1))
feed = { _input_ph: _input, _weights_pc : _weights }
result = tf.nn.depthwise_conv2d(_input_ph, _weights_pc, [1,1,1,1], 'SAME')
individual_results = []
for i in range(channels):
individual_results.append(tf.nn.conv2d(tf.expand_dims(_input_ph[:,:,:,i],axis=3), tf.expand_dims(_weights_pc[:,:,i,:],axis=3), [1,1,1,1], 'SAME'))
depth_result = sess.run(result, feed_dict=feed)
concat_result = sess.run(tf.concat(individual_results, axis=3), feed_dict=feed)
channel_diff = 0.0
for i in range(channels):
channel_diff += np.sum(depth_result[:,:,:,i]-concat_result[:,:,:,i])
print(channel_diff)
这里我首先计算正常tf.nn.depthwise_conv2d
,然后相应地对输入和权重进行切片并单独执行tf.nn.conv2d
。对于这些参数,我得到1e-5
差异,但当我增加频道数时,这种情况往往会更高。
如果有人能向我解释发生了什么,我会很高兴:) 谢谢!