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 2: Data Transformation & Augmentation

Learning objectives: - Understand why data transformations are necessary before training - Apply data augmentation to artificially expand your training set - Build PyTorch DataLoaders that feed data to models in batches

Clinical context: Think of transforms as standardizing your imaging protocol - just as you ensure consistent window/level settings before reading a CT, we ensure consistent data format before feeding it to a model.

Why do we need transforms?

Raw medical images come in different formats, sizes, and intensity ranges. Before a deep learning model can process them, we need to standardize them:

  1. LoadImage - reads the image file from disk into memory
  2. EnsureChannelFirst - rearranges dimensions to (Channel, Height, Width). Deep learning frameworks expect the channel dimension first, but image files store it last
  3. ScaleIntensity - normalizes pixel values from [0, 255] to [0, 1]. This helps the model train more efficiently, just as standardized units help clinicians compare measurements

What is data augmentation?

Data augmentation applies random transformations (rotations, flips, zooms) to training images. Each epoch, the model sees slightly different versions of the same image - this prevents overfitting (memorizing the training data instead of learning generalizable patterns).

Think of it as showing the same X-ray to a trainee rotated slightly, zoomed in a bit, or flipped - the pathology is the same, but the model learns to recognize it regardless of minor variations in positioning.

Why augmentation matters in clinical AI

Clinical datasets are often small and expensive to collect - labeling medical images requires expert radiologists, and data sharing is restricted by privacy regulations.

Data augmentation effectively multiplies your dataset without collecting new images. A dataset of 200 labeled images can behave like a dataset of 1,000+ images when each image is randomly transformed during training.

Important caveat: Augmentations must be clinically plausible. For example: - ✅ Rotation and zoom - patients are positioned slightly differently each scan - ✅ Gaussian noise - simulates scanner noise variation - ⚠️ Horizontal flip - appropriate for some modalities (e.g., histopathology patches) but not for chest X-rays where heart laterality (left-sided) is diagnostically meaningful - ❌ Color jitter - inappropriate for grayscale medical images

Always think: "Would a radiologist still make the same diagnosis on this augmented image?"

Datasets and DataLoaders

In PyTorch, data flows through two components:

  1. Dataset - a container that holds your data and knows how to load a single item (image + label). We define a custom MedNISTDataset class that applies our transforms when loading each image.

  2. DataLoader - wraps the Dataset and handles:

  3. Batching: groups images into batches (e.g., 300 at a time) for efficient GPU processing
  4. Shuffling: randomizes the order each epoch so the model doesn't learn from the sequence
  5. Parallel loading: uses multiple CPU workers to load batches while the GPU is training

Analogy: The Dataset is like a filing cabinet of patient cases. The DataLoader is the assistant who pulls cases in random batches of 300 and hands them to you for review.