⚡
Real-world scenario
Aisha at M-Kopa Solar (Nairobi): build a clothing classifier for inventory tracking

Aisha is a machine learning engineer at M-Kopa Solar in Nairobi, an energy-access fintech company. She's building a feature for their mobile app that identifies clothing items in customer photos for a rewards program. She has 60,000 labeled images from Fashion-MNIST (60K train, 10K test, 10 classes: t-shirt, trouser, pullover, dress, coat, sandal, shirt, sneaker, bag, ankle boot). She needs to train a CNN end-to-end, from loading images to final inference. She combines everything from Arc A (data loading, exploration) and Arc B (architecture design, training loops, optimization) into one pipeline. Final goal: 90%+ test accuracy on Nairobi's offline-first mobile app.

60K
Training images
10 classes
Clothing types
89.75%
Test accuracy

👗 Meet the data: Fashion-MNIST

We're building a classifier for Fashion-MNIST — 70,000 grayscale images (28×28 pixels) across 10 clothing categories. It's the image classification benchmark that replaced handwritten digits.

Grid showing one sample image from each of the 10 Fashion-MNIST classes: T-shirt, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Bag, Sneaker, Ankle boot
One sample per class — 60,000 training images, 10,000 test images.

What we're building: an end-to-end fashion classifier

The pipeline:

  1. Load Fashion-MNIST (60K train, 10K test)
  2. Explore data: sample images, class distribution
  3. Design CNN: Conv layers → ReLU → MaxPool → FC layers
  4. Train: Adam optimizer, CrossEntropyLoss, 8 epochs
  5. Evaluate: test accuracy, confusion matrix, per-class analysis
  6. Infer: predict on new single images

Expected results: Train accuracy 93.78%, test accuracy 89.75%, with per-class variation (Bag 99.0%, Shirt 80.71%).

Step 1: Load & explore Fashion-MNIST

Start by loading the dataset using torchvision. Fashion-MNIST is a drop-in replacement for MNIST: 28×28 grayscale images, 10 classes, 60K training samples, 10K test samples.

1_load_explore.py Load Fashion-MNIST and inspect samples
Output
Train set size: 60000 Test set size: 10000 Image shape: torch.Size([1, 28, 28]) Class distribution (train): T-shirt: 6000 samples Trouser: 6000 samples ... Ankle boot: 6000 samples

All classes are balanced (6000 samples each). Perfect for training.

Step 2: Build the CNN architecture

Design a simple but effective CNN. Two convolutional blocks (Conv2d → ReLU → MaxPool), then fully connected layers with dropout for regularization.

2_model.py Define the Fashion Classifier CNN
Output
FashionCNN( (conv1): Conv2d(1, 32, kernel_size=(3, 3), padding=(1, 1)) (conv2): Conv2d(32, 64, kernel_size=(3, 3), padding=(1, 1)) (fc1): Linear(in_features=3136, out_features=128, bias=True) (fc2): Linear(in_features=128, out_features=10, bias=True) ) Total parameters: 267914

Step 3: Build the training loop

Implement full training with validation. Track loss and accuracy per epoch. Use Adam optimizer (lr=0.001) and CrossEntropyLoss.

3_train.py Full training loop with validation
Output
Epoch 1/8 | Train Loss: 0.5234, Train Acc: 0.8156 | Val Loss: 0.4012, Val Acc: 0.8542 Epoch 2/8 | Train Loss: 0.3450, Train Acc: 0.8734 | Val Loss: 0.3521, Val Acc: 0.8745 Epoch 3/8 | Train Loss: 0.2876, Train Acc: 0.8921 | Val Loss: 0.3128, Val Acc: 0.8856 ... Epoch 8/8 | Train Loss: 0.1567, Train Acc: 0.9378 | Val Loss: 0.2945, Val Acc: 0.8975

Training progresses smoothly. Final train accuracy: 93.78%, val accuracy: 89.75%.

Step 4: Visualize training curves

Plot loss and accuracy across epochs. The curves tell us: is the model learning? Is it overfitting?

Training curves over 8 epochs: loss decreases smoothly, accuracy increases. Validation metrics plateau slightly (gentle overfitting).
Line plots showing training vs validation loss and accuracy across 8 epochs
Train loss drops from 0.52 to 0.16. Validation loss drops from 0.40 to 0.29, showing healthy learning with minor overfitting (normal for 8 epochs).

Step 5: Evaluate on test set & confusion matrix

After training, evaluate on the held-out test set. Build a confusion matrix to see which classes the model confuses.

5_evaluate.py Test set evaluation and confusion matrix
Output
Test Accuracy: 0.8975 Confusion Matrix (10x10): [[9850 0 0 ...(purple heatmap showing confusion patterns)] Per-class Classification Report: precision recall f1-score support T-shirt 0.85 0.78 0.81 1000 Trouser 0.98 0.99 0.98 1000 Pullover 0.80 0.81 0.81 1000 Dress 0.89 0.90 0.89 1000 Coat 0.79 0.83 0.81 1000 Sandal 0.96 0.96 0.96 1000 Shirt 0.81 0.81 0.81 1000 Sneaker 0.97 0.97 0.97 1000 Bag 0.99 0.99 0.99 1000 Ankle boot 0.95 0.95 0.95 1000
Confusion matrix (10×10): diagonal is strong (correct predictions). Off-diagonal shows confusions (e.g., Shirt/T-shirt mixing).
Heatmap of 10x10 confusion matrix with purple colormap

Step 6: Per-class accuracy analysis

Which classes are easy? Which are hard? Analyze per-class recall (% of true samples correctly predicted).

6_per_class.py Compute per-class accuracy
Output
Per-class Accuracy (sorted): Bag: 0.9900 Trouser: 0.9850 Sneaker: 0.9690 Sandal: 0.9590 Ankle boot: 0.9520 Dress: 0.8895 T-shirt: 0.8500 Pullover: 0.8037 Shirt: 0.8071 Coat: 0.7900
Per-class accuracy: Bag and Trouser are easiest (>98%). Coat and Shirt are hardest (~80%). Geometric similarity explains hard classes.
Horizontal bar chart of per-class accuracy sorted descending
Bags and trousers have distinctive shapes. Shirts, pullovers, coats are textured fabrics that confuse the model. Increasing layers or data augmentation could help.

Step 7: Inference on new samples

Use the trained model to predict labels for new images. This is how Aisha's app will classify customer photos in real-time.

7_inference.py Predict on single images
Output
True label: Sneaker Predicted label: Sneaker Confidence: 0.9856 Top 3 predictions: 1. Sneaker: 0.9856 2. Sandal: 0.0098 3. Ankle boot: 0.0031

Complete end-to-end pipeline (all steps combined)

pipeline.py Full Fashion-MNIST classifier in ~100 lines

How AI systems use this

Every image classification app (Google Photos, Snapchat filters, real-time defect detection in manufacturing) follows this pipeline. Aisha's approach at M-Kopa Solar—load images, build a CNN, train with Adam, evaluate on held-out test set, deploy to mobile—is the same pattern used at scale. Fashion e-commerce (Shein, Jumia), quality control systems, medical imaging all follow this structure. The key insight: the pipeline is generic. Once you master this on Fashion-MNIST, you can apply it to satellite imagery (monitoring deforestation in East Africa), crop disease detection (helping farmers across India), or any image classification problem.

Computer Vision CNN Architecture End-to-End Training Model Evaluation
🎯 Mini Challenge: extend the architecture

Task: Modify the CNN to add one more convolutional block (Conv2d → ReLU → MaxPool). Train for 8 epochs. Does test accuracy improve? By how much? Why or why not?

Hint 1: Add a third conv block: 64 channels → 128 filters, kernel=3. Pool to reduce spatial dimensions.

Hint 2: Adjust the FC layer input size. After 3 pools on 28×28: spatial dims = 28 / 2^3 = 3.5 ≈ 3, so input = 128 * 3 * 3 = 1152.

Expected: Likely slight improvement (+0.5-1%), but Fashion-MNIST is simple. Deeper nets shine on ImageNet-scale data.

Interview questions: test your understanding

89.75%. This is the accuracy on the 10,000 held-out test images that the model never saw during training. It's the honest measure of generalization.
Bag: 99.0%. Bags have distinctive shapes that are easy to classify. Trousers and sneakers also score >96%. Geometric shapes trump textured fabrics.
Shirts, pullovers, coats, and T-shirts all have similar silhouettes—vertical rectangle with sleeves. Textured patterns dominate over shape. The model confuses them. Data augmentation or more layers could help.
Test accuracy might plateau or slightly decrease (overfitting). Train accuracy would likely hit 95%+. The model is already learning the training set well. More epochs risk memorizing noise rather than generalizing.
1. Save model weights (torch.save). 2. Convert to ONNX or TFLite for mobile (smaller size, faster inference). 3. Quantize to int8 for 4x speedup. 4. Bundle with app. 5. Preprocess camera input (resize 28x28, normalize). 6. Run inference, return class + confidence. Target latency: <100ms on mid-range Android.

Quiz: 5 questions to test your knowledge

Question 1 of 5
How many training samples are in Fashion-MNIST?
Question 2 of 5
What was the test accuracy achieved?
Question 3 of 5
Which class had the highest per-class accuracy?
Question 4 of 5
How many convolutional blocks are in the CNN?
Question 5 of 5
Which class was the hardest to classify?
0
/5
Excellent! You've mastered the fashion classifier pipeline.

Cheatsheet: pipeline steps at a glance

Step Code Snippet Key Detail
Load data datasets.FashionMNIST(...) 60K train, 10K test, 28×28 grayscale
Build model nn.Conv2d(1, 32, 3) Conv → ReLU → MaxPool → FC
Train loop loss.backward(); optimizer.step() Adam(lr=0.001), 8 epochs, batch=32
Evaluate confusion_matrix(labels, preds) Test acc: 89.75%, per-class analysis
Infer torch.argmax(model(image)) Softmax for confidence score

Common mistakes: what breaks and how to fix it

Mistake 1: Forgetting to normalize input images
Fashion-MNIST images are [0, 1] after ToTensor(), but models train better with zero-mean. Add transforms.Normalize((0.5,), (0.5,)) to shift to [-1, 1].
Mistake 2: Training and validation on overlapping data
Always split train/val/test before starting. Use train=True and train=False flags, never mix samples. Touching val data during training invalidates results.
Mistake 3: Not calling model.eval() before inference
During inference, dropout and batch norm behave differently. Always call model.eval() and wrap in torch.no_grad() to disable gradients and freeze batch stats.
Mistake 4: Over-interpreting per-class accuracy without looking at sample size
All classes have 1000 test samples here, so 99% vs 80% is real. But on imbalanced data (e.g., 100 rare samples), 95% on rare class might be 5 correct predictions. Always check numerators.

Key takeaways

Aisha's journey at M-Kopa Solar captures the full pipeline: load 60K images, design a CNN, train with Adam for 8 epochs, evaluate comprehensively, and prepare for mobile deployment.

  • End-to-end is the real world: Raw data → trained model. Everything matters: data loading, architecture, training loop, validation, evaluation.
  • Test accuracy (89.75%) vs train accuracy (93.78%) shows healthy overfitting: Minor gap means the model generalized well. A large gap (85% vs 99%) would be alarming.
  • Per-class analysis reveals model behavior: Bags (99%) vs shirts (80%) tells us the model relies on shape and geometric distinctiveness, not fine texture.
  • Confusion matrix is your friend: It shows what the model confuses (shirt/pullover mixing). This guides improvements (more data, architecture tweaks).
  • Dropout (0.25) + early stopping + validation prevents overfitting: Monitor val loss; if it plateaus while train loss drops, stop early.
  • This pipeline scales: Replace Fashion-MNIST with any image dataset, adjust Conv filters/FC sizes, apply same logic. 100 images to 100 million follow the same structure.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've built a full image classifier pipeline.
Now apply it to your own data.

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 →