Conv2D vs Depthwise Conv2D calculation

Viewed 391

I am trying to understand the similarities/differences in the calculation of 2D convolutional and 2D depthwise convolutional neural networks. (I understand the concepts).

For example, say there is an input image that is 3x3 with 3 channels (RGB) and padding of 1 and stride of 1. The filter is 2x2.

What is the output? (can disregard activation and bias)

I know that regular conv2D will have 1 3x3 output, whereas dw conv2D will have 3. Beyond that I am a little confused. Thanks

1 Answers

For example, let's consider the input image shape to be (5,5,3).

Convolution Layer:

In Conv2D, 24 Filters of size 3*3*3 are convoluted with input 5*5*3.

Conv2D

The depth of each filter in any convolution layer is going to be same as the depth of the input shape of the layer:

input_shape = (1, 5, 5, 3)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(24, 3, activation='relu', input_shape=(5,5,3))(x)
print(y.shape) #(1,3,3,24)

Depthwise Convolution layer:

In Depth-wise convolution, we don't specify the number of filters; we only use 1 filter:

z = tf.keras.layers.DepthwiseConv2D(3,activation='relu', input_shape=input_shape[1:])(x)
print(z,shape) #(1,3,3,3)

SeparableConvolution

Related