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.
👗 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.
What we're building: an end-to-end fashion classifier
The pipeline:
- Load Fashion-MNIST (60K train, 10K test)
- Explore data: sample images, class distribution
- Design CNN: Conv layers → ReLU → MaxPool → FC layers
- Train: Adam optimizer, CrossEntropyLoss, 8 epochs
- Evaluate: test accuracy, confusion matrix, per-class analysis
- 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.
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.
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.
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?
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.
Step 6: Per-class accuracy analysis
Which classes are easy? Which are hard? Analyze per-class recall (% of true samples correctly predicted).
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.
Complete end-to-end pipeline (all steps combined)
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.
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
Quiz: 5 questions to test your knowledge
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
transforms.Normalize((0.5,), (0.5,)) to shift to [-1, 1].train=True and train=False flags, never mix samples. Touching val data during training invalidates results.model.eval() and wrap in torch.no_grad() to disable gradients and freeze batch stats.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.
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 →