PIXELBANKv9.1.0
Menu

Depthwise Separable Convolution

Implement depthwise separable convolution, a key efficiency technique in MobileNets.

Standard convolution: Cin×H×W→Cout×H′×W′C_{in} \times H \times W \rightarrow C_{out} \times H' \times W' Parameters: Cout×Cin×K×KC_{out} \times C_{in} \times K \times K

Depthwise separable breaks this into:

  1. Depthwise: Each input channel convolved separately
    • Parameters: CinΓ—KΓ—KC_{in} \times K \times K
  2. Pointwise: 1Γ—1 convolution to mix channels
    • Parameters: CoutΓ—CinC_{out} \times C_{in}

Efficiency gain: 1Cout+1K2\frac{1}{C_{out}} + \frac{1}{K^2} of standard conv!

Example:

Input:
input: (1, 3, 8, 8)  # RGB image
depthwise: (3, 1, 3, 3)  # 3x3 per channel
pointwise: (16, 3, 1, 1)  # Expand to 16 channels
Output:
tensor of shape (1, 16, 6, 6)
Reasoning:

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
πŸ”’

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.

solution.py

Test Results

0/0
Run code to see test results.