KatInterview

Convolutional Layer Analysis: Shapes, Parameters, and Efficiency

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingconvolutional-networksparameter-countingcomputational-complexity

Last updated

Calculate output dimensions, parameter counts, and multiply-accumulate operations for standard and depthwise separable convolutions.

You are analyzing a 2D convolutional layer in a neural network. Given the specifications of an input tensor and a convolution operation, compute key metrics about the layer's architecture and computational cost.

Input Specifications

You are given:

  • Input dimensions: height × width × channels (denoted H × W × C)
  • Number of output filters: K
  • Kernel size: f × f (square spatial filter)
  • Stride: s (applied equally in both spatial dimensions)
  • Padding: p (zero-padding added to all sides)

Required Outputs

Compute the following metrics and return them in a dictionary:

  1. H_out: Height of the output feature map
  2. W_out: Width of the output feature map
  3. standard_params: Total trainable parameters in a standard convolution (weights and biases)
  4. standard_macs: Number of multiply-accumulate operations for a standard convolution
  5. depthwise_separable_params: Total trainable parameters when using depthwise separable convolution
  6. depthwise_separable_macs: Number of multiply-accumulate operations for depthwise separable convolution

Convolution Details

Standard Convolution: Each of the K filters spans all C input channels with spatial size f × f, plus one bias term per filter.

Depthwise Separable Convolution: A two-stage operation:

  1. Depthwise: One f × f spatial filter per input channel (no cross-channel mixing), producing C output channels. Stride and padding are applied at this stage.
  2. Pointwise: A 1 × 1 convolution mapping from C channels to K channels, applied at the already-strided spatial resolution.

Each stage includes bias terms.

Constraints

  • 1 ≤ H, W ≤ 512
  • 1 ≤ C, K ≤ 1024
  • 1 ≤ f ≤ 11 (f is odd)
  • 1 ≤ s ≤ 4
  • 0 ≤ p ≤ 5
  • The combination of parameters guarantees at least one valid output position (H_out ≥ 1, W_out ≥ 1)
  • Use integer division (floor) when computing output dimensions
  • Count multiply-accumulate operations only (exclude standalone bias additions)

Examples

Example 1

Input: compute_conv_metrics(32, 32, 3, 16, 3, 1, 1)

Output: {'H_out': 32, 'W_out': 32, 'standard_params': 448, 'standard_macs': 442368, 'depthwise_separable_params': 94, 'depthwise_separable_macs': 76800}

Example 2

Input: compute_conv_metrics(28, 28, 64, 128, 3, 2, 1)

Output: {'H_out': 14, 'W_out': 14, 'standard_params': 73856, 'standard_macs': 14450688, 'depthwise_separable_params': 8960, 'depthwise_separable_macs': 1718528}

Example 3

Input: compute_conv_metrics(7, 7, 32, 32, 5, 1, 2)

Output: {'H_out': 7, 'W_out': 7, 'standard_params': 25632, 'standard_macs': 1254400, 'depthwise_separable_params': 1888, 'depthwise_separable_macs': 89376}

Hints

Hint 1

Output spatial dimensions follow the formula: floor((input_size + 2*padding - kernel_size) / stride) + 1

Hint 2

Each standard convolution filter has f×f×C weights spanning all input channels, plus one bias per output channel

Hint 3

In depthwise separable convolution, the stride is applied during the depthwise stage, so both stages operate at the output resolution