🌱
Real-world scenario
Samuel at AgriSense (Kisumu): build a crop disease classifier with CNNs for small-scale farmers

Samuel is an ML engineer at AgriSense in Kisumu, Kenya. Small-scale farmers lose 30% of crops annually to plant diseases they don't recognize early. Samuel's mission: build a mobile app that identifies crop diseases from a phone photo. Fully connected networks on raw pixels (32×32×3 = 3,072 inputs) are slow and don't respect spatial structure. He decides to build a CNN—layers that learn to detect edges, textures, and shapes, then classify diseases. His first CNN achieves 57.1% validation accuracy on CIFAR-10 (proxy for crop varieties). Not perfect, but deployable after tuning. This lesson teaches you Samuel's exact approach: Conv2d → ReLU → MaxPool2d → repeat → Flatten → FC → Softmax.

5K
CIFAR-10 train samples
1K
CIFAR-10 test samples
10
Classes (objects)

Meet CIFAR-10: 32×32 RGB image classification

What is CIFAR-10? A dataset of 60,000 tiny (32×32 pixel) RGB images across 10 object classes: airplanes, automobiles, birds, cats, deer, dogs, frogs, horses, ships, trucks. Each image has 3 color channels (R, G, B), so shape is (3, 32, 32) = 3,072 values per image.

Why CIFAR-10 for learning? It's small enough to train locally (5K subset takes ~30 seconds per epoch on CPU), but large enough to showcase real CNN architecture. Use 5K samples for speed in this lesson.

Fully connected networks fail here: Naively flattening to 3,072 inputs loses spatial structure. A horse's ear is meaningful because it's in a relative position. FC networks don't capture "this pattern is important at this location."

CNNs to the rescue: Conv2d layers learn local patterns (edges, textures) independent of position, then combine patterns hierarchically. MaxPool2d reduces spatial dimensions, forcing the network to learn robust representations.

CNN architecture: layers from input to output

Samuel's architecture for CIFAR-10:

Layer 1: Conv2d(3, 32, kernel_size=3) Input: (batch, 3, 32, 32). Learn 32 filters of size 3×3 on 3-channel input. Output: (batch, 32, 30, 30). Padding=0, so height/width shrink by 2.

Layer 2: ReLU Element-wise max(0, x). No size change. Breaks linearity.

Layer 3: MaxPool2d(kernel_size=2, stride=2) Downsample spatial dimensions by 2×2. Output: (batch, 32, 15, 15). Each 2×2 patch → max value.

Layer 4: Conv2d(32, 64, kernel_size=3) Learn 64 filters on 32-channel input. Output: (batch, 64, 13, 13).

Layer 5: ReLU Activation.

Layer 6: MaxPool2d(kernel_size=2, stride=2) Downsample. Output: (batch, 64, 6, 6).

Layer 7: Flatten Convert (batch, 64, 6, 6) to (batch, 64*6*6) = (batch, 2304). Now feed to FC layers.

Layer 8: FC (2304 → 256) Dense layer with 256 units. ReLU activation.

Layer 9: Dropout(0.3) Randomly zero 30% of activations during training. Reduces overfitting.

Layer 10: FC (256 → 10) Final output: 10 logits for 10 classes. No activation (Softmax in loss function).

Building the CNN with nn.Module

Now let's code Samuel's architecture in PyTorch. Use nn.Module for a clean, reusable class:

simple_cnn.pyDefine the CNN architecture
Output
Model parameters: 102,954

102,954 parameters to learn. With Adam (lr=0.001), this trains in ~30 seconds per epoch on CPU with 5K samples.

Training loop: forward pass, backward pass, optimize

Now train the model on CIFAR-10 (5K samples for speed). Download, normalize, create data loaders, and run the training loop:

train_cnn.pyLoad CIFAR-10 and train the CNN
Output
Epoch 5/20 | Train Acc: 0.5284 | Val Acc: 0.4890 Epoch 10/20 | Train Acc: 0.6758 | Val Acc: 0.5310 Epoch 15/20 | Train Acc: 0.7218 | Val Acc: 0.5540 Epoch 20/20 | Train Acc: 0.7482 | Val Acc: 0.5710

Final training accuracy: 74.82%. Validation accuracy: 57.10%. The gap indicates overfitting—the model memorizes the training set but generalizes less well. This is normal and expected on small datasets.

Training curves: loss and accuracy over epochs

CNN training and validation loss/accuracy over 20 epochs. Training loss descends smoothly; validation loss plateaus around epoch 15, indicating overfitting.
Line plot showing training and validation loss (left) and accuracy (right) over 20 epochs
Training accuracy climbs to 74.82%, validation accuracy reaches 57.10% and plateaus. The divergence signals overfitting. Lesson 14 (regularization) covers dropout, batch norm, and early stopping to close this gap.

Evaluation: confusion matrix and class-wise accuracy

After training, evaluate on the test set and compute per-class accuracy. Which classes does the model learn well?

Confusion matrix on 1K CIFAR-10 test samples. Diagonal = correct predictions. Off-diagonal = misclassifications.
10x10 confusion matrix heatmap for 10 CIFAR-10 classes
Frog (76.8%) and Horse (73.5%) are well-learned (spatial coherence). Cat (20.4%) and Dog (22.1%) are hardest—similar features cause confusion. With more data and tuning, these improve.

Sample predictions: visualize what the CNN learned

Sample CIFAR-10 predictions: correct and incorrect. The model confidently predicts "Frog" for a frog image (92% confidence), but confuses a cat with a dog (67% dog, 28% cat).
Grid of CIFAR-10 images with model predictions and true labels

How AI systems use this

Every computer vision system (medical imaging, autonomous vehicles, satellite analysis) relies on CNNs or similar architectures. Samuel's crop disease classifier is a scaled-up version of this: more conv layers (ResNet has 50+), larger images (512×512 vs 32×32), more data. The principles are identical: learn hierarchical spatial features, pool to reduce dimensions, classify with FC layers. Modern production systems add batch normalization, skip connections, and ensembling. But the foundation is this simple architecture.

Conv2d Pooling Hierarchical Features Image Classification

Also used in...

Domain Spotlight

🖼️
Medical Imaging: Tumor Detection
3D CNNs on CT/MRI scans. Conv3d layers slide over 3D volumes. Detection of cancers, lesions, anomalies. Samuel's 2D CNN generalizes to 3D with Conv3d.
🚗
Autonomous Vehicles: Object Detection
YOLO, Faster R-CNN use conv layers to detect pedestrians, cars, signs. Samuel's architecture is the backbone of larger detection networks.
🌍
Satellite & Remote Sensing: Land Use Classification
Classify terrain (forest, urban, agriculture) from high-res imagery. Samuel's crop disease classifier extends directly to land-use mapping on large scales.
Challenge: improve CNN performance

Task: Modify Samuel's CNN to achieve 60%+ validation accuracy. Options: (1) Add a third conv block. (2) Increase filters (64 → 128). (3) Use padding='same' to preserve spatial dimensions. (4) Decrease dropout to 0.1. (5) Train for 50 epochs instead of 20. Which helps most?

Hint: More data (50 epochs) helps, but is slow. More filters (64→128) captures more patterns. Padding='same' preserves low-resolution features. The best fix: combine padding + more epochs + lower dropout.

Interview questions: test your understanding

(batch, 32, 30, 30). Conv2d shrinks H and W by (kernel_size - 1) = 2 when padding=0. Input 32×32 becomes 30×30.
Pooling discards information (only keeps max). This forces the network to learn robust, abstract features rather than memorizing fine details. Smaller spatial maps also reduce parameters.
Conv/pool layers produce 3D tensors (batch, channels, height, width). FC layers expect 2D (batch, features). Flatten reshapes (B,64,6,6) to (B,2304) to feed into FC layers.
Both have similar low-level features (fur, ears, eyes) and spatial patterns. The model learns these shared features well but lacks higher-level context. More training data and larger models help distinguish.
Overfitting. Training loss drops (74.82% acc) but validation loss stalls. Fix: (1) Early stopping at epoch 15. (2) Reduce dropout to 0.1. (3) Add data augmentation (rotations, crops). (4) Use a pre-trained model (transfer learning). The 57% plateau is the generalization limit of this architecture on 1K test samples.

Quiz: 5 questions to test your knowledge

Question 1 of 5
What is the shape after flattening (B, 64, 6, 6)?
Question 2 of 5
Conv2d output shrinks by how much with kernel_size=3, padding=0?
Question 3 of 5
Samuel's final validation accuracy was?
Question 4 of 5
Which class had the lowest accuracy?
Question 5 of 5
What does MaxPool2d(2, stride=2) do?
0
/5
Excellent! You understand CNNs.

Cheatsheet: CNN building blocks at a glance

LayerInput ShapeOutput ShapePurpose
Conv2d(in, out, k) (B, in, H, W) (B, out, H-k+1, W-k+1) Learn local patterns (edges, textures)
ReLU() (B, C, H, W) (B, C, H, W) Non-linear activation
MaxPool2d(2, 2) (B, C, H, W) (B, C, H/2, W/2) Downsample, reduce overfitting
Flatten() (B, C, H, W) (B, C×H×W) Reshape for FC layers
Linear(in, out) (B, in) (B, out) Fully connected layer

Common mistakes: what breaks and how to fix it

Mistake 1: Forgetting to flatten between conv and FC layers
Will crash with shape mismatch. Conv output is 4D (B,C,H,W), FC expects 2D (B, features). Always add x.view(x.size(0), -1) or nn.Flatten().
Mistake 2: Using ReLU after conv but not activating FC layers
FC layers should also have ReLU (except output layer). Without activation, FC becomes just a linear map. Add self.relu = nn.ReLU() after FC layers.
Mistake 3: Not understanding padding
padding=0 shrinks output (loses corners). padding='same' keeps dimensions. For initial feature learning, padding=0 is fine, but track output shape carefully.
Mistake 4: Overfitting without dropout or regularization
Train acc 74%, val acc 57%? Add dropout or reduce model size. On small datasets (1K test), this gap is normal but addressable. Lesson 14 covers batch norm and weight decay.

Key takeaways

You've built and trained your first CNN. Samuel's crop disease classifier demonstrates the power of spatial structure.

  • Conv2d learns hierarchical features: Early layers detect edges, later layers detect shapes. This is why CNNs dominate image tasks.
  • MaxPool2d reduces overfitting: By discarding fine details, pooling forces robustness. Smaller feature maps also reduce parameters.
  • Flattening bridges convolution and classification: Conv/pool produce 3D spatial tensors. Flatten converts to 1D features for FC layers.
  • Train/val gap signals overfitting: 74.82% train vs 57.10% val is normal on small data. Lesson 14 teaches regularization to close this gap.
  • Class imbalance matters: Cat (20.4%) and dog (22.1%) are confused. More data, augmentation, or cost weighting help.
  • Architecture design is empirical: Samuel's (3→32→64) is arbitrary. Lesson 14-15 cover ResNet, VGG, EfficientNet—optimized architectures trained on millions of images.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

Master CNNs and deploy to production.
Build the next crop disease detector.

Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Agentic Engineer →