AI & Machine Learning Cheatsheet
CNNs and Computer Vision
Use this AI & Machine Learning reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Why CNNs for Images?
A 224×224 RGB image has 224×224×3 = 150,528 values. An MLP applied to this flat vector has no awareness that neighboring pixels are related, and the parameter count explodes. CNNs exploit three properties of images:
- Local connectivity: nearby pixels are more correlated than distant ones
- Translation equivariance: a cat in the top-left is still a cat in the bottom-right
- Compositionality: complex patterns are composed of simpler local patterns
These are implemented via shared convolutional filters and pooling.
The Convolution Operation
A 2D convolution slides a filter K (kernel) over the input image I:
(I ∗ K)[i,j] = Σₘ Σₙ I[i+m, j+n] · K[m,n]
For each spatial position, compute the dot product between the kernel and the corresponding patch of the input.
Key hyperparameters: - Kernel size: 3×3 (most common), 5×5, 1×1 (channel mixing) - Stride: step size of the sliding window (stride=2 halves spatial size) - Padding: 'same' keeps output size; 'valid' reduces size by (k−1)/2 - Number of filters (output channels): each filter learns to detect a different feature
Output size formula: H_out = ⌊(H_in + 2P − K) / S⌋ + 1
where P=padding, K=kernel size, S=stride.
Parameter count for one conv layer: K × K × C_in × C_out + C_out (biases)
This is independent of spatial size H × W — the key efficiency of shared weights.
import torch.nn as nn # Conv2d(in_channels, out_channels, kernel_size, stride, padding) conv = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) # Input: (batch, 3, 224, 224) → Output: (batch, 64, 224, 224) # Params: 3*3*3*64 + 64 = 1,792 (vs 150528*64 = 9.6M for MLP)
Pooling Layers
Pooling reduces spatial dimensions and introduces limited translation invariance.
| Type | Operation | Use |
|---|---|---|
| Max pooling | Max value in window | Detect if feature is present |
| Average pooling | Mean of window | Smooth spatial representations |
| Global Average Pooling | Mean over all spatial positions | Classification head (replaces Flatten+FC) |
| Adaptive pooling | Output any target size | Model agnostic of input size |
pool = nn.MaxPool2d(kernel_size=2, stride=2) # halves H and W gap = nn.AdaptiveAvgPool2d((1, 1)) # (batch, C, H, W) → (batch, C, 1, 1)
Standard CNN Architecture Pattern
Input → [Conv → BN → ReLU → Pool] × N → Global Avg Pool → FC → Softmax
Each convolutional block: - Increases channel count (32 → 64 → 128 → 256) - Decreases spatial resolution (halved by stride or pooling)
class ConvBlock(nn.Module): def __init__(self, in_ch, out_ch, stride=1): super().__init__() self.block = nn.Sequential( nn.Conv2d(in_ch, out_ch, 3, stride=stride, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=True) ) def forward(self, x): return self.block(x) class SimpleCNN(nn.Module): def __init__(self, n_classes): super().__init__() self.features = nn.Sequential( ConvBlock(3, 32), ConvBlock(32, 32, stride=2), # /2 ConvBlock(32, 64), ConvBlock(64, 64, stride=2), # /2 ConvBlock(64, 128), ConvBlock(128, 128, stride=2), # /2 ) self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, n_classes) ) def forward(self, x): return self.classifier(self.features(x))
Landmark CNN Architectures
| Model | Year | Key Innovation | Parameters | Top-1 (ImageNet) |
|---|---|---|---|---|
| LeNet-5 | 1998 | First practical CNN | 60K | — |
| AlexNet | 2012 | Deep + GPU + dropout | 60M | 63.3% |
| VGGNet | 2014 | All 3×3 convs, depth | 138M | 74.4% |
| GoogLeNet/Inception | 2014 | Inception module (multi-scale) | 6.8M | 74.8% |
| ResNet-50 | 2015 | Residual connections | 25M | 76.1% |
| DenseNet | 2017 | Dense connections (all → all) | 7M | 77.1% |
| EfficientNet-B0 | 2019 | Compound scaling (W×D×R) | 5.3M | 77.1% |
| ConvNeXt | 2022 | Modernized ResNet, Transformer-style | 29M | 82.1% |
| ViT-B/16 | 2020 | Vision Transformer | 86M | 81.8% |
ResNet — Residual Learning
ResNet solves the "degradation problem" (deeper networks having higher error):
y = F(x, {Wᵢ}) + x
The residual block learns the residual function F(x) = target − input, which is easier to learn than the full transformation directly.
class ResBlock(nn.Module): def __init__(self, channels): super().__init__() self.conv1 = nn.Conv2d(channels, channels, 3, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(channels) self.conv2 = nn.Conv2d(channels, channels, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): residual = x out = self.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) return self.relu(out + residual)
EfficientNet — Compound Scaling
Instead of scaling only depth, width, or resolution independently, EfficientNet scales all three simultaneously with a compound coefficient φ:
depth: d = α^φ, width: w = β^φ, resolution: r = γ^φ subject to α·β²·γ² ≈ 2
This achieves better accuracy/efficiency tradeoff than prior architectures.
Vision Transformer (ViT)
Apply the Transformer architecture directly to image patches:
- Split image into 16×16 patches (224×224 image → 196 patches)
- Linearly embed each patch (patch embedding)
- Add positional encoding + class token
- Run through standard Transformer encoder
- Use class token output for classification
ViT matches CNN accuracy with enough data (>14M images); struggles with less. Hybrid models (e.g., Swin Transformer) add local inductive biases.
Transfer Learning for Computer Vision
Pre-trained models on ImageNet extract powerful visual features. Fine-tune for your task:
import torchvision.models as models import torch.nn as nn # Load pre-trained ResNet-50 model = models.resnet50(weights="IMAGENET1K_V2") # Freeze all layers except the classification head for param in model.parameters(): param.requires_grad = False # Replace final FC layer for new task num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, n_classes) # only this will be trained # Optionally unfreeze later layers (fine-tuning) for param in model.layer4.parameters(): param.requires_grad = True optimizer = torch.optim.AdamW([ {"params": model.layer4.parameters(), "lr": 1e-4}, {"params": model.fc.parameters(), "lr": 1e-3} ])
When to freeze vs. fine-tune:
| Your Dataset Size | Similar to Pre-train Data? | Strategy |
|---|---|---|
| Small (<1k) | Yes | Only train head |
| Small (<1k) | No | Train head, maybe last layer |
| Medium (1k–10k) | Yes | Fine-tune last few layers |
| Large (>10k) | Any | Fine-tune all layers |
Data Augmentation for Vision
Augmentation is critical for visual tasks — models must learn appearance invariance:
from torchvision import transforms # Standard augmentation for training train_transforms = transforms.Compose([ transforms.RandomResizedCrop(224, scale=(0.7, 1.0)), transforms.RandomHorizontalFlip(), transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3), transforms.RandomGrayscale(p=0.1), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # ImageNet stats ]) # Validation: only resize and center crop val_transforms = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])
Advanced augmentation: - CutMix: paste a patch from one image into another, mix labels - Mixup: blend two images linearly, blend labels - AugMix: chain multiple transformations - RandAugment: random selection from 14 augmentation types
Object Detection
Detect and localize multiple objects in an image (bounding boxes + class labels).
| Model | Type | Speed | Accuracy | Notes |
|---|---|---|---|---|
| Faster R-CNN | Two-stage | Slow | High | Proposal-based |
| YOLO v8/v9 | One-stage | Fast | Good | Real-time inference |
| DETR | Transformer | Medium | High | End-to-end, no anchors |
| SSD | One-stage | Fast | Medium | Anchor-based |
Metrics: mAP (mean Average Precision) at IoU thresholds (0.5, 0.5:0.95).
IoU (Intersection over Union): IoU = Area(A ∩ B) / Area(A ∪ B)
Image Segmentation
| Task | Output | Model |
|---|---|---|
| Semantic | Class label per pixel | FCN, DeepLab, SegFormer |
| Instance | Object mask per instance | Mask R-CNN, SOLO |
| Panoptic | Both semantic + instance | Panoptic-FPN |
U-Net (encoder-decoder with skip connections) is the standard architecture for medical imaging segmentation:
Encoder: downsample + extract features Bottleneck: lowest resolution Decoder: upsample + concatenate skip connections from encoder Output: pixel-wise class map
Common Computer Vision Tasks and Approaches
| Task | Approach |
|---|---|
| Classification | CNN / ViT backbone + linear head |
| Detection | YOLO / DETR |
| Segmentation | U-Net / SegFormer |
| Depth estimation | DPT / AdaBins |
| Face recognition | ArcFace / CosFace (metric learning) |
| Optical flow | RAFT / FlowFormer |
| Image generation | Diffusion models (DDPM, Stable Diffusion) |
| Video understanding | 3D-CNN / Video Transformers |