Home > Software design >  Convolutional layer without summation over different channels - Keras
Convolutional layer without summation over different channels - Keras

Time:12-19

Assuming I have 5x5x3 image and I have different filter for each channel - for example 3x3x3. In Cov2D first, each of the kernels in the filter are applied to three channels in the input layer, separately (which gives 3x3x3 - without padding and stride 1) and the these three channels are summed together (element-wise addition), gives 3x3x1.

I want instead of summation over channels (3x3x1), concatenate the three channels (3x3x3).

Thanks for help.

CodePudding user response:

What you are referring to is depthwise convolution where outputs' channels are concatenated rather than summed. (See https://www.tensorflow.org/api_docs/python/tf/keras/layers/DepthwiseConv2D for details)

Demonstration:

x = np.random.rand(1,5,5,3)
l = tf.keras.layers.DepthwiseConv2D(3, depth_multiplier=1)
print(l(x).shape)
'''
(1, 3, 3, 3)
'''

You can use depth_multiplier to control the number of depthwise kernels applied to each channel.

l2 = tf.keras.layers.DepthwiseConv2D(3, depth_multiplier=2)
print(l2(x).shape)
'''
(1, 3, 3, 6)
'''
  • Related