Cheat sheet · Deep Learning
Convolutional neural networks (CNNs) cheat sheet
The key ideas on one page — plus common pitfalls and interview tips. Skim it before an interview, or print it for revision.
Cheat sheet
CNNs exploit spatial structure with learnable convolutional filters, weight sharing, and local connectivity, making them parameter-efficient and translation-equivariant. They remain the workhorse for on-device/efficient vision and dense prediction, and as of 2026 modern ConvNets (ConvNeXt V2) are competitive with Vision Transformers, while hybrids and ViT-based foundation models (CLIP, DINOv2, SAM) dominate large-scale and zero-shot settings.
Core Building Blocks
- Convolution layer: a small learnable kernel slides over the input computing dot products, producing feature maps. Key properties: local connectivity, weight sharing (same kernel everywhere), and translation EQUIVARIANCE (shifting the input shifts the output by the same amount).
- Output spatial size: O = floor((W - K + 2P) / S) + 1, where W=input size, K=kernel, P=padding, S=stride. 'same' padding keeps size when S=1 (needs P=(K-1)/2 for odd K); 'valid' uses no padding.
- Parameter count of a conv layer = (K_h * K_w * C_in + 1) * C_out (the +1 is one bias per output channel). Independent of spatial size — this is why CNNs scale to large images. FLOPs, however, scale with output H*W.
- Pooling (max/average) downsamples to enlarge the receptive field and gives LOCAL translation invariance; modern nets often replace it with strided convolutions. Global Average Pooling (GAP) collapses each channel to a single value before the classifier head.
- Activations: ReLU is the classic default; smooth variants (GELU, SiLU/Swish) and LeakyReLU reduce dead units. GELU/SiLU are common in modern ConvNets (e.g., ConvNeXt).
- Receptive field grows with depth, stride, and dilation; dilated (atrous) convolutions enlarge it without adding parameters — key for segmentation and detection.
Convolution Variants
- 1x1 convolution: mixes channels and changes depth cheaply; used as a bottleneck (ResNet) and for dimensionality reduction/expansion.
- Depthwise separable conv = depthwise (one spatial filter per channel) + pointwise (1x1) conv. Reduces FLOPs to roughly (1/C_out + 1/K^2) of a standard conv; backbone of MobileNet/Xception/EfficientNet.
- Grouped convolution splits channels into groups (ResNeXt); cardinality (number of groups) becomes a tuning axis alongside depth and width.
- Transposed (fractionally-strided) convolution upsamples in decoders/GANs; can cause checkerboard artifacts when kernel size is not divisible by stride — prefer resize (bilinear/nearest) + conv.
- Dilated convolution inserts gaps in the kernel to expand the receptive field at constant parameter cost; effective kernel size = K + (K-1)*(d-1).
- Large-kernel convs (7x7 depthwise in ConvNeXt) regained popularity to mimic ViT global context while staying fully convolutional.
Regularization & Normalization
- Batch Normalization normalizes activations per-channel over the batch; speeds/stabilizes training but is batch-size sensitive and behaves differently in train vs eval (uses running mean/var at inference).
- Alternatives for small/variable batches: GroupNorm, LayerNorm (used in ConvNeXt), InstanceNorm (style transfer). LayerNorm/GroupNorm/InstanceNorm are batch-independent.
- Standard Dropout is weak in conv layers (spatially correlated activations); prefer it in the dense head, or use DropBlock (structured spatial dropout) and Stochastic Depth.
- Weight decay (L2; use AdamW for decoupled decay), early stopping, and label smoothing are standard. Stochastic Depth randomly drops residual blocks during training in very deep nets.
- Data augmentation is the strongest regularizer in vision: flips/crops/color jitter, plus Mixup, CutMix, RandAugment, and TrivialAugment.
- At inference, fold BatchNorm into the preceding conv for speed; always set model.eval() / training=False so BN uses running stats and Dropout is disabled.
Landmark Architectures
- LeNet/AlexNet/VGG: deep stacks of small (3x3) convs; VGG is simple but parameter-heavy (its dense layers dominate the parameter count).
- GoogLeNet/Inception: multi-branch modules with 1x1 bottlenecks for efficiency.
- ResNet: residual/skip connections y = F(x) + x ease gradient flow and enable very deep nets; still the default backbone baseline.
- DenseNet (dense feature reuse), ResNeXt (cardinality), SENet (channel attention via squeeze-and-excite).
- Efficient families: MobileNetV1-V3, EfficientNet/EfficientNetV2 (compound scaling of depth/width/resolution), RegNet — strong accuracy/latency tradeoffs for deployment.
- Modern (2020s): ConvNeXt / ConvNeXt V2 modernize ResNets (large depthwise kernels, LayerNorm, GELU, inverted bottleneck; V2 adds masked-autoencoder pretraining) to rival ViTs; hybrids (CoAtNet, MobileViT) combine conv + attention.
Training & Practical Tips
- Use transfer learning: start from ImageNet or self-supervised (MAE, DINOv2) pretrained weights; fine-tune the head first, then unfreeze the backbone with a lower LR.
- Optimizers: SGD+momentum (strong generalization for CNNs) or AdamW (decoupled weight decay; common for ConvNeXt/transformers). Pair with cosine or one-cycle LR schedules plus warmup.
- Normalize inputs with the dataset/ImageNet mean-std and match the exact preprocessing (resize, crop, normalization) used during pretraining.
- Mixed precision (AMP fp16/bf16) and channels-last memory format speed up training on modern GPUs with minimal code change.
- Diagnose with learning curves: large train-val gap = overfit (add augmentation/regularization); both train and val poor = underfit (more capacity / longer training / tune LR).
- For deployment: quantization (INT8 PTQ or QAT), pruning, and operator fusion; export to ONNX/TensorRT/CoreML/TFLite and benchmark real latency and memory, not just FLOPs.
Tasks, Loss & Interpretability
- Classification: GAP + linear head with cross-entropy (often label smoothing). Detection: anchor-based (Faster R-CNN, YOLO family) or anchor-free / set-based (FCOS, DETR).
- Segmentation: encoder-decoder with skip connections (U-Net), FCN, DeepLabv3+ (atrous + ASPP); losses include cross-entropy, Dice, and focal. Promptable foundation models (SAM/SAM 2) are now common for zero-shot masks.
- Backbones feed downstream heads via Feature Pyramid Networks (FPN) to fuse multi-scale features.
- Interpretability: Grad-CAM weights each last-conv feature map by the GAP of the gradient of the target class score, then ReLU-sums them into a class-discriminative heatmap.
- CNNs are translation-equivariant but NOT rotation- or scale-invariant by default — use augmentation or specialized layers.
- Robustness concerns: adversarial perturbations, texture bias (CNNs lean on texture over shape), and distribution shift; evaluate on corrupted/OOD sets (e.g., ImageNet-C).