TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

52. Depthwise Separable Convolution

Medium

Implement depthwise separable convolution as used in MobileNet. This factorizes a standard convolution into two cheaper operations:

  1. Depthwise convolution: apply one filter per input channel (no cross-channel mixing). Each channel is convolved independently.
  2. Pointwise convolution: a 1×1 convolution that mixes channels, projecting from C_in to C_out.

This reduces computation by roughly a factor of k² (kernel size squared) compared to a full convolution.

Signature: def depthwise_separable_conv(x, dw_kernel, pw_kernel)

  • x: (H, W, C_in)
  • dw_kernel: (kH, kW, C_in) — one spatial filter per input channel (no C_out axis)
  • pw_kernel: (C_out, C_in) — 1×1 pointwise weights
  • Returns: (H_out, W_out, C_out) where H_out = H - kH + 1

Step 1 — depthwise: For each channel c and output position (i, j):

dw_out[i, j, c] = sum over (kh, kw) of x[i+kh, j+kw, c] * dw_kernel[kh, kw, c]

Step 2 — pointwise: mix the depthwise-output channels into the C_out channels using the pw_kernel 1×1 weights at every spatial position. Equivalent to a per-pixel linear projection from C_in to C_out.

Math

out[i,j,cout​]=cin​∑​​kh​,kw​∑​x[i+kh​,j+kw​,cin​]⋅wdw​[kh​,kw​,cin​]​⋅wpw​[cout​,cin​]

Asked at

NumPy

import numpy as np

 

def depthwise_separable_conv(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?