Inputs:x is a batch of 1 image with Cin=3 channels on an 8×8 grid; the depthwise kernel holds one 3×3 filter per channel (3×1×3×3); the pointwise kernel holds a weight Po,c per (output, input) channel pair (16×3×1×1).
Depthwise step: channel c is filtered only by its own kernel, Dc(i,j)=∑u,vxc(i+u,j+v)kc(u,v). With no padding and stride 1, a 3×3 window fits in 8−3+1=6 positions per axis: 1×3×6×6, channels unmixed.
Pointwise step: at every pixel, yo(i,j)=∑c=13Po,cDc(i,j), which mixes the 3 channels into 16 without touching the 6×6 grid: tensor of shape (1, 16, 6, 6).
Check with all-ones tensors: each depthwise value is 9, each output value is 3×9=27.
Notice the savings:3⋅9+16⋅3=75 weights versus 16⋅3⋅9=432 for a standard conv, a ratio of 161+91≈0.174.
Depthwise separable convolution replaces one expensive convolution with two cheaper ones: a depthwise spatial convolution applied independently per input channel, followed by a pointwise1×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:
Pstd=Cout⋅Cin⋅K2
Depthwise separable params:
Pdwsep=Cin⋅K2+Cout⋅Cin
Relative cost:
PstdPdwsep=Cout1+K21
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').
PRO
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
PRO
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.