liveBook · Manning

Data augmentation

As explained in Deep Learning with PyTorch, Second Edition

A training technique that creates modified versions of examples to increase effective dataset diversity while preserving task-relevant meaning.

Data augmentation makes new training examples by changing existing ones. The useful changes preserve what the model must recognize while making the result harder to memorize. This increases a dataset’s effective size without collecting more samples—a practical way to encourage generalization when data is limited. section 14.6 section 5.5.3

The change must matter to the task. For a face-to-age model, changing a few corner pixels creates many nominally different images, but the model can simply ignore those pixels. A left-to-right mirror is more useful: it changes the face while preserving its age-related features. An upside-down face, by contrast, may be too unrealistic. Choose transformations with domain knowledge, not just a desire to produce more images. section 14.6 section 14.6.1

For a 3D CT nodule candidate, the available alterations are:

Shift a CT candidate during training

Start with an identity transform, then assign a random offset on each of its three axes: transform_t = torch.eye(4) for i in range(3): random_float = random.random() * 2 - 1 transform_t[i, 3] = augmentation_dict['offset'] * random_float An affine grid and `grid_sample` then resample the CT tensor using that transform. The same pipeline can incorporate flips, scaling, and rotation; noise is added to the resampled chunk. Cache the original chunk before augmentation, or the cache will preserve just one altered version instead of allowing fresh variations. section 14.6.1 section 14.6.1

Augmentation can help a model see an object in different positions, but it does not erase architectural limitations. A fully connected image classifier can require substantial capacity to learn translated copies of the same airplane; convolutional models address that mismatch through their structure. Augmentation is therefore a training choice, not a substitute for choosing a suitable model. section 7.2.6

Check whether the chosen changes help rather than assuming they will. Train an unaugmented baseline, separate runs for each alteration, and a run combining them; then compare validation loss, F1 score, precision, and recall in TensorBoard. The combined CT augmentation run differs from the individual runs, so the validation metrics—not the apparent number of new samples—should guide the choice. Further experiments can vary the strength of noise or offsets and test whether noise helps at all. section 14.6.2 section 14.8

Listing 14.12 Applying affine transformation
transform_t = torch.eye(4)
# ...                        #1
# ... line 195
affine_t = F.affine_grid(
    transform_t[:3].unsqueeze(0).to(torch.float32),
    ct_t.size(),
    align_corners=False,
  )

augmented_chunk = F.grid_sample(
    ct_t,
    affine_t,
    padding_mode='border',
    align_corners=False,
  ).to('cpu')
# ... line 214
return augmented_chunk[0], center_irc

The affine-grid and sampling listing shows how a transformed CT candidate is produced from the transformation matrix. section 14.6.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through five CT augmentation types, affine resampling, caching order, and comparative training runs; leaves transformation strength open to experimentation.
Examples
Uses mirrored face images, translated airplanes, transformed CT nodule candidates, and TensorBoard comparisons of augmentation runs.
What it assumes
Assumes a training dataset and a CT candidate pipeline; introduces the augmentation choices and resampling steps in context.
What you can do after
Build it from scratch for CT candidates with PyTorch transforms, then evaluate individual and combined changes against validation metrics.
How it connects
Connects overfitting and generalization to model capacity, translation invariance, class balancing, and validation metrics.

Also explained in

Compare how 9 books explain data augmentation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.