python - Tensorflow Depthwise Convolution Understanding -
i'm trying understand how tensorflow's depthwise convolution works. far i've understood, each channel in input image convolved it's own set of filters, , results concatenated. i'm going stick parameter depth_multiplier=1
sake of simplicity in remainder, n_inputchannels == n_outputchannels
.
so in theory, split depthwise convolution n
individual, regular conv2ds
, correct? why following code produce different results wondering - precision issue? i'm following documentation ordering [filter_height, filter_width, in_channels, 1]
depthwise convolution filters, , [filter_height, filter_width, in_channels, out_channels]
regular convolutions, , nhwc
data format.
import tensorflow tf import numpy np import random width = 128 height = 128 channels = 32 kernel_width = 3 kernel_height = 3 tf.session() 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 = [] 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 in range(channels): channel_diff += np.sum(depth_result[:,:,:,i]-concat_result[:,:,:,i]) print(channel_diff)
here i'm computing first normal tf.nn.depthwise_conv2d
, slice input , weights accordingly , tf.nn.conv2d
s individually. these parameters 1e-5
difference, tends higher when increase number of channels.
i glad if explain me what's going on :) thanks!
Comments
Post a Comment