Efficient CNNs: MobileNet, EfficientNet and ConvNeXt
Cheaper convolutions and better scaling. Depthwise separable layers, scaling depth, width and resolution together, and a ResNet rebuilt with Transformer habits.
What runs in CodeSplash
- DNN009MobileNet Runs in CodeSplash (Experimental)
- DNN010EfficientNet Runs as a notebook
- DNN011ConvNeXt Explained here
- Image collection profile (size, color, sharpness, duplicates) Experimental
- Object detection (Faster R-CNN, COCO) Experimental
- Features from a pretrained model (images, audio, text) Experimental
You will be able to
- Count the weights a depthwise separable convolution saves
- Explain compound scaling and why compute roughly doubles per step
- List what ConvNeXt changed in a ResNet block
- Say which of these CodeSplash runs
Read first
Split the convolution in two
ReadA standard k × k convolution mixes space and channels at once: k² × C_in × C_out weights. A depthwise separable convolutionDepthwise separable convolutionA convolution split in two: one small kernel per channel, then a 1 × 1 convolution that mixes channels. Far fewer weights. splits the job. First one small kernel per channel looks at neighbors (depthwise), then a 1 × 1 convolution mixes channels (pointwise): k² × C_in + C_in × C_out weights.
In the program below, a 4 × 4 image with 2 channels goes through a 3 × 3 depthwise step and a 2 × 2 pointwise mix and stays 4 × 4 × 2. MobileNet builds whole networks from such blocks so they run on phones.
import numpy as np
def conv2(x, k, pad=0):
x = np.pad(x, pad)
return np.array([[np.sum(x[i:i + 3, j:j + 3] * k) for j in range(x.shape[1] - 2)]
for i in range(x.shape[0] - 2)])
x = np.stack([np.arange(16.).reshape(4, 4) / 16, np.eye(4)], axis=-1) # 4 × 4 × 2
dw = np.stack([conv2(x[:, :, c], np.ones((3, 3)) / 9, pad=1) for c in range(2)], axis=-1)
y = dw @ np.array([[1., .5], [-.5, 1.]]) # pointwise 1 × 1 mix
print(y.shape, y[1, 1])conv2 <- function(x, k) {
z <- matrix(0, nrow(x) + 2, ncol(x) + 2); z[2:(nrow(x) + 1), 2:(ncol(x) + 1)] <- x
out <- matrix(0, nrow(x), ncol(x))
for (i in seq_len(nrow(x))) for (j in seq_len(ncol(x))) out[i, j] <- sum(z[i:(i + 2), j:(j + 2)] * k)
out
}
x <- array(c(matrix(0:15, 4, 4, byrow = TRUE) / 16, diag(4)), c(4, 4, 2))
dw <- array(c(conv2(x[, , 1], matrix(1 / 9, 3, 3)), conv2(x[, , 2], matrix(1 / 9, 3, 3))), c(4, 4, 2))
pointwise <- matrix(c(1, .5, -.5, 1), 2, byrow = TRUE)
print(dw[2, 2, ] %*% pointwise) # pixel (2, 2)How many weights saved?
PredictPredict first
A 3 × 3 layer maps 64 channels to 64. Standard: 9 × 64 × 64 weights. Depthwise separable: 9 × 64 + 64 × 64. Roughly how many times fewer weights does the separable version need?
Reveal
About 8 times fewer: 36,864 against 4,672. The saving approaches k² = 9 as the channel count grows, and compute shrinks by the same factor.
Scale everything together
ReadTo make a network bigger you can add layers, widen them or feed larger images. Compound scalingCompound scalingGrowing a network's depth, width and input resolution together by fixed ratios, instead of only one of them. (EfficientNet) grows all three together: depth by α^φ, width by β^φ and resolution by γ^φ, with α = 1.2, β = 1.1 and γ = 1.15 found by a small search.
Compute grows with depth × width² × resolution², about 1.92 times per step of φ: the program's table reaches 3.69 at φ = 2 and 7.08 at φ = 3. The ratios were searched on ImageNet, so they're a recipe, not a law.
ConvNeXt: a ResNet with Transformer habits
ReadConvNeXtConvNeXtA modernized ResNet that borrows Transformer habits: large depthwise kernels, layer norm, GELU and fewer activations. took a ResNet and changed one design choice at a time toward what vision transformers do: large 7 × 7 depthwise kernels, layer normLayer normalizationStandardizes each row across its own features, then applies a learned scale and shift; same in training and prediction. instead of batch norm, an inverted bottleneck that widens fourfold, GELUGELUA smooth activation, x times the normal CDF of x: close to ReLU for large inputs but curved near zero. Common in Transformers., fewer activations, and a small learned scale on each residual branch. The result matched transformers of similar size while staying a pure CNN.
In the reference program, a block with a tiny branch scale changes its input by about a ten-thousandth: 1, 2, 3 become 1.00004, 2.00014 and 3.00024.
What runs here
Read| Entry | In CodeSplash |
|---|---|
| MobileNet (DNN009) | Runs in CodeSplash: Image collection profile and Features from a pretrained model can embed images with a pretrained MobileNetV3-Small (about 10 MB, one-time download, Python only); Object detection has a fast MobileNetV3 detector |
| EfficientNet (DNN010) | Runs as a notebook (the reference program); no image model offers it |
| ConvNeXt (DNN011) | Explained here: no image model offers it; the image classifier offers ResNet-50 and DINOv2-small |
Check: efficient CNNs
CheckpointCheck yourself
What are the two steps of a depthwise separable convolution?
Show the answer
Answer: a
The depthwise step handles space, the pointwise step handles channels. A standard convolution does both at once, at far higher cost.
Check yourself
How do you use MobileNet in CodeSplash today?
Show the answer
Answer: a
The embedder turns each image into features for the map. The CNN learner trains its own small network from pixel columns.
Check yourself
What does compound scaling scale together?
Show the answer
Answer: a
EfficientNet grows all three by fixed ratios per step instead of only adding layers.
Check yourself
Why does compute grow with width squared and resolution squared, but only depth to the first power?
Show the answer
Answer: a
A convolution's cost is about k² × C_in × C_out × H × W, so channels and image sides each enter twice.
Check yourself
Which normalization does ConvNeXt use in place of ResNet's batch norm?
Show the answer
Answer: a
Layer norm doesn't depend on the batch, one of several habits ConvNeXt borrowed from vision transformers.
Check yourself
Can a CodeSplash learner fine-tune a ConvNeXt backbone today?
Show the answer
Answer: b
ConvNeXt is described for understanding. The pretrained image models CodeSplash ships are ResNet-50, DINOv2-small and, for embeddings only, MobileNetV3.
Recap
Recap- Depthwise separable convolutions split space and channel mixing, saving close to k² in weights and compute.
- Compound scaling grows depth, width and resolution together; compute roughly doubles per step.
- ConvNeXt rebuilt a ResNet with large depthwise kernels, layer norm, GELU and an inverted bottleneck.
- In CodeSplash, MobileNetV3 is an optional image embedder; EfficientNet and ConvNeXt are explained here.
Concepts in this lesson