Convolutional Layer Analysis: Shapes, Parameters, and Efficiency
Arrays & HashingAuto-graded · 19 test casesLast 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(denotedH × 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:
H_out: Height of the output feature mapW_out: Width of the output feature mapstandard_params: Total trainable parameters in a standard convolution (weights and biases)standard_macs: Number of multiply-accumulate operations for a standard convolutiondepthwise_separable_params: Total trainable parameters when using depthwise separable convolutiondepthwise_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:
- Depthwise: One
f × fspatial filter per input channel (no cross-channel mixing), producingCoutput channels. Stride and padding are applied at this stage. - Pointwise: A
1 × 1convolution mapping fromCchannels toKchannels, 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
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution computes six related metrics by applying convolution arithmetic formulas.
Output Dimensions: When a kernel of size f slides over a padded input with stride s, the number of valid positions is determined by how many times the kernel fits. The padded input has dimension H + 2p, and after placing the first kernel at position 0, subsequent positions are at s, 2s, 3s, ... until the kernel would extend beyond the padded boundary. This gives us floor((H + 2p - f) / s) + 1 valid positions. The floor operation handles cases where the stride doesn't divide evenly, dropping any incomplete positions at the boundary.
Standard Convolution Parameters: Each output filter is a 4D tensor of shape f × f × C × 1, containing f²C weights that span all input channels. With K such filters and one bias parameter per filter, the total is (f²C + 1)K. Note that parameter count is independent of input spatial dimensions due to weight sharing.
Standard Convolution MACs: Each output element requires a dot product between the f × f × C kernel and a corresponding input patch, totaling f²C multiply-accumulate operations. With H_out × W_out × K output elements, the total is H_out · W_out · K · f² · C.
Depthwise Separable Convolution: This architecture factorizes a standard convolution into two cheaper operations. The depthwise stage applies one f × f spatial filter to each of the C input channels independently, with stride s and padding p. This produces C intermediate feature maps at the output resolution. The pointwise stage then applies a 1 × 1 convolution to mix these C channels into K output channels. Critically, both stages operate at the strided output resolution, not the input resolution.
For parameters: depthwise contributes (f² + 1)C (one f × f filter plus bias per channel), and pointwise adds (C + 1)K (a C-dimensional weight vector plus bias for each of K output channels).
For MACs: depthwise performs H_out · W_out · C · f² operations, and pointwise adds H_out · W_out · C · K.
Why This Works: The depthwise separable design trades representational capacity for efficiency. A standard convolution jointly learns spatial and channel relationships with K · f² · C degrees of freedom, while the factorized version has only C · f² + C · K parameters. When K and f are reasonably large, the savings are substantial—often reducing compute by 8× to 9× for f = 3 and moderate K.
Complexity
- Time: O(1) — all operations are direct arithmetic calculations
- Space: O(1) — only a fixed-size dictionary is allocated
Solution
from typing import Dict
def compute_conv_metrics(height: int, width: int, in_channels: int, out_filters: int, kernel_size: int, stride: int, padding: int) -> Dict[str, int]:
# Calculate output spatial dimensions using floor division
H_out = (height + 2 * padding - kernel_size) // stride + 1
W_out = (width + 2 * padding - kernel_size) // stride + 1
# Standard convolution: K filters, each with f*f*C weights plus one bias
standard_params = (kernel_size * kernel_size * in_channels + 1) * out_filters
standard_macs = H_out * W_out * out_filters * kernel_size * kernel_size * in_channels
# Depthwise separable: split into depthwise and pointwise stages
# Depthwise: one f*f filter per input channel, plus bias
depthwise_params = (kernel_size * kernel_size + 1) * in_channels
# Pointwise: 1x1 conv from C to K channels, plus bias
pointwise_params = (in_channels + 1) * out_filters
depthwise_separable_params = depthwise_params + pointwise_params
# Both stages operate at output resolution (stride applied in depthwise)
depthwise_macs = H_out * W_out * in_channels * kernel_size * kernel_size
pointwise_macs = H_out * W_out * in_channels * out_filters
depthwise_separable_macs = depthwise_macs + pointwise_macs
return {
"H_out": H_out,
"W_out": W_out,
"standard_params": standard_params,
"standard_macs": standard_macs,
"depthwise_separable_params": depthwise_separable_params,
"depthwise_separable_macs": depthwise_separable_macs,
}