Skip to content

Read it, then run it

This page is the read-only chapter intro. To actually execute the cells (load the dataset, train a model, render GradCAM heatmaps on your own images), open the Colab notebook in a free T4 runtime. The curriculum is designed to be executed, not read.

Chapter 10: Build Your Own CNN (Optional)

Learning objective: Understand CNN architecture design from the ground up by building, training, and evaluating a custom network.

This chapter is optional. It's for participants who want to understand what's "under the hood" of the pretrained architectures we've been using. If you're short on time, you can skip to the wrap-up at the end.

CNN Building Blocks: Why These Choices?

Component What it does Why we use it
Conv2d(3×3 kernel) Extracts local features with a small filter 3×3 is the most common choice - small enough to capture fine details, large enough to learn meaningful patterns
BatchNorm2d Normalizes activations within each batch Stabilizes training, allows higher learning rates, acts as mild regularization
ReLU Sets negative values to zero Introduces non-linearity (without it, stacking layers would be equivalent to one linear layer)
MaxPool2d(2×2) Keeps the maximum value in each 2×2 region Reduces spatial dimensions by half, making the network progressively more abstract
Linear (FC layer) Connects all features to output classes Makes the final classification decision based on all extracted features

Design philosophy: Start simple, add complexity only if needed. A simple network that works is better than a complex one that overfits.

Exercise: Modify the Architecture

Try these modifications and observe the effect:

  1. Add a Dropout layer: Insert nn.Dropout(0.3) before the final nn.Linear in the classifier. Does it reduce overfitting?

  2. Add another conv block: Insert a layer6 with 128 filters between layer5 and the classifier. You'll need to recalculate the FC input size.

  3. Try a different activation: Replace ReLU with nn.LeakyReLU(0.1) or nn.GELU(). Does it change convergence?

Think about: Does adding complexity improve performance? Or does it lead to overfitting with only 4 epochs of training? This is the fundamental trade-off in architecture design.

Hint: With more layers and parameters, you typically need more data or more regularization (dropout, weight decay) to prevent overfitting.

Tutorial Complete!

Key Takeaways

  1. Medical images are matrices of numbers - all the "intelligence" comes from finding mathematical patterns that correlate with clinical outcomes

  2. Start with pretrained models - transfer learning from ImageNet or foundation models dramatically reduces the data and compute you need

  3. Vision Transformers and CNNs are complementary - ViTs excel with large datasets and global context; CNNs are efficient and effective with smaller datasets

  4. Metrics alone aren't enough - calibration, confidence intervals, subgroup analysis, and explainability are all essential for clinical trust

  5. Explainability is hypothesis-generating, not proof - GradCAM shows where the model is sensitive, not what it "understands"

Next Steps for Your Research

  • Apply to your own data: Start with a pretrained model, use the freeze → fine-tune workflow
  • Read the literature: Search for foundation models specific to your imaging modality
  • Validate rigorously: External test sets, confidence intervals, subgroup analysis
  • Engage with regulations: Understand FDA pathways before designing your study

Further Reading


Clinical AI 2026 - Computer Vision for Radiologists