Data Preprocessing and Augmentation
Normalization, standardization, cropping, and augmentation techniques. When augmentation helps, when it introduces artifacts, and domain-specific considerations for medical imaging.
A model never sees the image you see on the PACS workstation. Before training, every image is converted, rescaled, resized and cropped into the grid of numbers the network takes as input. During training, most are also deliberately distorted, to make the dataset go further. The first step is called preprocessing and the second augmentation.
Both are usually considered details left to the engineers. This could be a mistake, because both preprocessing and augmentation involve decisions that need a clinician. An engineer can't know that the finding you care about is two pixels wide and will vanish when the image is shrunk, or that a left-right flip changes the diagnosis.
Preprocessing: from DICOM to numbers
Getting the pixels out
A DICOM file holds the pixel data and a header describing how to interpret it. This interpretation can be tricky and sometimes leads to errors. Stored pixel values usually have to be rescaled using a slope and intercept from the header to get real units. Some radiographs are stored inverted, with bone dark and air bright. Skip that field and part of your dataset is photographic negatives of the rest: if the inverted ones came mostly from one site, the model now has a shortcut.
The conclusion is pretty simple: have a look at the images after preprocessing, as the model will receive them, and not only at the originals.
Intensity
CT is the easy case. Its values are in Hounsfield units, a calibrated scale on which water is 0 and air is -1000, on every scanner. The variable factor is the intensity window. A CT covers a range of several thousand units and the network needs it squeezed into a smaller range, usually 256 values. Clip to a soft-tissue window and the lung detail is gone; clip to a lung window and the liver is a uniform grey. You can tell your engineer how to clip the window - you want it chosen with the same care you would give to setting the window yourself. Some pipelines feed several windows as separate channels.
MRI is a harder case. The intensities are in arbitrary units that differ between scanners, between sequences and between two sessions on the same machine. They have to be standardized per scan, commonly by subtracting the scan's mean and dividing by its standard deviation, called z-score normalization. MRI also has a slow drift in brightness across the field of view, which is usually removed first with a bias field correction such as the N4 algorithm.
Radiographs, ultrasound and photographs come with whatever processing the vendor applied, which differs by vendor and can't be undone. That is a generalization problem more than a preprocessing one (see 2.6 Multi-Site and Multi-Device Data).
Bit depth is a sneaky way to lose information. Medical images typically carry 12 or 16 bits per pixel, thousands of grey levels. Converting them to ordinary 8-bit image files along the way leaves 256 levels.
Size and spacing
Networks want inputs of a fixed size, but available memory limits what that size can be. A chest radiograph might be 3,000 pixels across, and many classification pipelines shrink it to a few hundred. For cardiomegaly that is harmless, but for a small nodule, a hairline fracture or a tiny pneumothorax, the finding may be shrunk out of existence, and the model is then being trained on labels for things that are no longer in the picture. Ask what resolution the model sees and look at your smallest positive cases at that resolution. If you can't see the finding, neither can the model.
The alternatives are to crop to the region of interest, or to work on patches at full resolution, which is standard for 3D volumes and for whole-slide pathology images, where one slide is far too large to process whole.
For CT and MRI there is also voxel spacing. One scan has 0.6 mm slices and the next 5 mm. Volumes are usually resampled to a common spacing so that a centimetre is the same number of voxels everywhere. Resampling thick slices to thin ones invents data by interpolation, so the choice of target spacing is a compromise.
Cropping and clean-up
Cropping to the anatomy removes irrelevant background and, usefully, some of the shortcuts that live at the edges: laterality markers, burned-in text, the edge of the detector. It also removes burned-in patient names, which you need gone anyway (see 2.7 De-Identification, Privacy, and Regulatory Compliance for Research Data).
Two rules
Any statistic used in preprocessing must be computed exclusively from the training set. For example if you normalize by the dataset's mean intensity, calculate that mean on the training set and apply it unchanged to the validation and test sets. Using the test set for these calculations is a kind of information leak, a mild form of the leakage described in 3.2 Training, Validation, and Test Sets.
That's because preprocessing is part of the model. The network was trained on images prepared one way, and it will only work on images prepared exactly that way. A model handed over without its preprocessing code is not reproducible. If the tool ever becomes a product, the preprocessing is inside the regulated device and gets verified along with the rest.
Augmentation: no free lunch, but maybe a snack?
Augmentation applies random changes to each training image every time the model sees it: a small rotation, a slight zoom or a brightness shift. The model never sees precisely the same image twice, which makes it harder to memorize the training set and teaches it that these changes don't alter the answer. On small datasets it is one of the most effective techniques there is, and it costs nothing but compute.
The common kinds of augmentation are:
- Geometric: rotation, shift, zoom, flip, and elastic deformation, which stretches the image locally as if it were printed on rubber. The original U-Net paper relied heavily on elastic deformation to train from its 30 images.
- Intensity: changes to brightness and contrast, added noise, blur.
- Simulated acquisition differences: MRI bias fields, motion and ghosting artifacts, lower resolution, and for pathology slides, variation in stain colour.
- Mixing: methods such as mixup and CutMix that blend two images or paste a patch of one into another, along with their labels.
The test for a safe augmentation
An augmentation is a kind of claim, that this change produces an image that could plausibly occur and would have the same label as the original image. If those are both true then the augmentation is very helpful, but if either is false you are training the model on wrong answers. Only someone who knows the clinical task can check the claim, so the list of augmentations is something that should be read and approved by a clinician.
There are lots of ways that an augmentation that is standard practice in other vision AI research can go wrong with medical data. For example, flips. Flipping a photograph of a cat left to right gives another cat, while flipping a chest radiograph left to right gives dextrocardia. If the task involves laterality, or the label says "left," a horizontal flip corrupts it. For a skin lesion photograph or a pathology patch, though, flips and rotations by any angle are fine.
Another example is intensity changes on calibrated data. In CT the Hounsfield value is diagnostic: it tells fat from fluid from blood. Random brightness shifts large enough to turn one into another destroys information. In pathology, colour carries meaning, yet stain colour also varies between labs for no clinical reason. Moderate stain augmentation teaches the model to ignore the lab. David Tellez and colleagues showed in 2019 that it was one of the most effective ways to make pathology models work on slides from other centers.
Most applications will have some augmentations that don't make sense in that context, which is why a clinician should always work with the engineer before implementing this strategy.
Rules for augmentation
Augment the training set only. The validation and test sets are left alone, since they are there to represent real images.
Split first, augment after. If augmented copies are generated before the data is divided, versions of the same image end up on both sides of the split and the test result is inflated. Augmenting on the fly during training, which is the norm, avoids this.
For segmentation and detection, the same geometric transform must be applied to the image and to its outline or boxes. Intensity transforms apply to the image only.
And look at the result. Have the engineers export a few dozen augmented training images with their labels, and review them as you would any other images. If you wouldn't accept one as a plausible study with that label, the setting is too strong.
What augmentation doesn't do
It makes variations on the patients you have. It adds no new patients, no new pathology and no new scanner. A model trained on one hospital's images, augmented heavily, still knows one hospital. Augmenting with simulated scanner differences narrows that gap a little but does not close it.
Augmentation also doesn't fix class imbalance on its own, though it makes oversampling the rare class safer by varying the repeated cases (see 2.4 Class Imbalance).
Key takeaways
- Preprocessing converts images into the numbers a model takes. Mistakes in reading the DICOM header, such as missed inversion or rescaling, corrupt the data without any error message.
- CT needs a deliberate choice of window. MRI intensities are arbitrary and must be standardized per scan.
- Downsizing can delete small findings. Check your smallest positives at the resolution the model sees.
- Compute normalization statistics on the training set only, and treat the preprocessing code as part of the model.
- Augmentation randomly varies training images and is very effective on small datasets.
- Each augmentation must produce a plausible image without changing the label. Flips, intensity shifts and deformations can all fail that test, depending on the task, and a clinician should review them.
- Augment only the training set, after the split, and apply geometric changes to image and annotation together.
- Augmentation adds variations, not patients. It does not replace data from other sites.
What to read next
- 2.6 Multi-Site and Multi-Device Data: The Generalization Challenge: the variation augmentation can't supply
- 2.8 Synthetic Data and Data Simulation: generating new images instead of varying old ones
- 3.5 Overfitting, Underfitting, and Regularization: the problem augmentation is a defence against
- 8.3 Interoperability: DICOM, HL7 FHIR, and Integration Standards: more on what is in a DICOM file
This article is part of the AI in Clinical Research Knowledge Base.