Depthwise Separable Convolution
Implement depthwise separable convolution, a key efficiency technique in MobileNets.
Standard convolution: CinβΓHΓWβCoutβΓHβ²ΓWβ² Parameters: CoutβΓCinβΓKΓK
Depthwise separable breaks this into:
- Depthwise: Each input channel convolved separately
- Parameters: CinβΓKΓK
- Pointwise: 1Γ1 convolution to mix channels
- Parameters: CoutβΓCinβ
Efficiency gain: Coutβ1β+K21β of standard conv!
Example:
input: (1, 3, 8, 8) # RGB image depthwise: (3, 1, 3, 3) # 3x3 per channel pointwise: (16, 3, 1, 1) # Expand to 16 channels
tensor of shape (1, 16, 6, 6)
Depthwise (no padding): (1,3,8,8) β (1,3,6,6) Each channel filtered independently
Pointwise: (1,3,6,6) β (1,16,6,6) Linear combination of channels at each spatial location
Total params: 3Γ9 + 16Γ3 = 27 + 48 = 75 Standard 3Γ3 conv: 16Γ3Γ9 = 432 (5.8Γ more!)
Constraints:
- input: Tensor (batch, channels, height, width)
- depthwise_kernel: (channels, 1, k, k)
- pointwise_kernel: (out_channels, channels, 1, 1)
- Return: Output tensor
Depthwise separable convolution replaces one expensive convolution with two cheaper ones: a depthwise spatial convolution applied independently per input channel, followed by a pointwise 1Γ1 convolution that mixes channel information.ThispreservesthesameoverallmappingfromCinβΓHΓWtoCoutβΓHβ²ΓWβ²,butwithfarfewerparametersandFLOPs,whichiswhyitiscentraltoMobileNetβstylearchitectures.
1. Background Knowledge (key concepts)
- Standard 2D convolution in CNNs
A standard convolution layer with kernel size K \times K maps an input of shape (C_{in}, H, W) to an output of shape (C_{out}, H', W') using a weight tensor of shape (C_{out}, C_{in}, K, K). Each output channel is a weighted sum over all input channels and spatial neighbors, so both spatial and cross-channel mixing happen in a single operation.
- Depthwise separable convolution
MobileNet factorizes this into two steps:
- Depthwise convolution
- One K \times K filter per input channel.
- Weights: shape (C_{in}, 1, K, K), often stored as (C_{in}, K, K).
- Each channel is convolved independently (no mixing between channels here).
- Pointwise convolution
- A 1 \times 1 convolution across channels to mix them.
- Weights: shape (C_{out}, C_{in}, 1, 1), typically viewed as a matrix (C_{out}, C_{in}).
This dramatically reduces computational cost while maintaining accuracy for many vision tasks.
-
Parameter / FLOP reduction
-
Standard conv params:
- Depthwise separable params:
- Relative cost:
The same ratio holds (up to constants) for multiply-add operations, which explains the efficiency gain often cited for MobileNets.
2. Algorithm / Approach Pattern
Conceptually, the algorithm is:
- Depthwise pass For each input channel c:
- Convolve the 2D slice X_c \in \mathbb{R}^{H \times W} with its own K \times K kernel W^{dw}_c.
- Store the result as an intermediate βdepthwise outputβ of shape (C_{in}, H', W').
- Pointwise pass For each output channel o:
- Take the vector at each spatial position (i,j) across all depthwise channels (C_{in}).
- Multiply by a 1 \times 1 kernel (a vector) W^{pw}o \in \mathbb{R}^{C{in}}.
- This is equivalent to a matrix multiplication between a (C_{out} \times C_{in}) weight matrix and the C_{in}-dim feature vector at each spatial position.
In code terms (framework-agnostic): first perform a grouped convolution with groups = C_in, then a standard 1\times1 convolution.
3. Step-by-Step Strategy to Implement
Assume input tensor shape: (N, C_in, H, W).
- Understand required shapes
- Depthwise weights: (C_in, 1, K, K) or equivalently (C_in, K, K) depending on the API.
- Pointwise weights: (C_out, C_in, 1, 1) or equivalently (C_out, C_in).
- Implement depthwise convolution
- Loop over batch and input channels.
- For each channel, apply a 2D convolution with:
- Stride, padding, and dilation matching the original full conv.
- No mixing between channels.
- Store result in an intermediate tensor dw_out of shape (N, C_in, H', W').
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.