#130Why CNNs — conv vs dense parameter countMediumCNNComputer Vision
Why CNNs — conv vs dense parameter count
Background
The headline reason CNNs replaced dense nets for images: weight sharing. A dense layer over a flattened image has a weight for every pixel × every output unit:
A convolution reuses one small kernel across the whole image, so its parameter count is independent of image size:
(for F filters of size k×k over C input channels, with one bias each).
Problem statement
Implement conv_vs_dense_params(image_shape, dense_units, conv_filters, kernel_size) returning (dense_params, conv_params).
Input
image_shape— tuple(H, W, C).dense_units—int, output units of the dense layer.conv_filters—int, number of conv filtersF.kernel_size—int, kernel widthk.
Output
Returns (dense_params, conv_params), both int.
Examples
Example 1 — the lesson's 224×224×3 case
Input: image_shape = (224, 224, 3), dense_units = 1000, conv_filters = 64, kernel_size = 3
Output: (150529000, 1792)
Explanation: dense ; conv .
Constraints
dense = H*W*C*dense_units + dense_units.conv = (k*k*C + 1) * conv_filters.- Return both as
int.
Notes
- The conv count doesn't depend on
HorWat all — the same kernel slides everywhere. That's how CNNs look at megapixel images with only thousands of parameters per layer.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example — 224x224x3 lesson case
- •Reference — small 4x4x1 image
- •Sample — conv independent of image size