liveBook · Manning

Data augmentation

As explained in Deep Learning with Python, Third Edition

A regularization approach that creates additional training examples by applying label-preserving random transformations to existing samples.

A ConvNet trained on only 2,000 images can keep improving on its training set while overfitting. Data augmentation tackles this by making random, believable variations of existing training images. The model sees changing inputs rather than the exact same picture each time, which can help it generalize. section 8.2.4 section 8.2.5

Vary an image without changing its label

A training image might be flipped horizontally, rotated, or zoomed while retaining its target label. In Keras, RandomFlip("horizontal") flips roughly half the images; RandomRotation(0.1) rotates within ±36 degrees; and RandomZoom(0.2) zooms within ±20%. Inspect several results before training to see what the model will receive. section 8.2.5 section 8.2.5

Listing 8.18 Defining a data augmentation stage
data_augmentation_layers = [                                           #1
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.2),
]

def data_augmentation(images, targets):                                #2
    for layer in data_augmentation_layers:
        images = layer(images)
    return images, targets

augmented_train_dataset = train_dataset.map(                           #3
    data_augmentation, num_parallel_calls=8                             #3
)                                                                       #3
augmented_train_dataset = augmented_train_dataset.prefetch(tf.data.AUTOTUNE)#4

The augmentation function applies the three Keras layers to images, returns targets unchanged, and maps over the training dataset. Prefetching prepares batches for training. section 8.2.5

Practical choices:

  • Apply augmentation in a Dataset map() call to run it on CPU cores in parallel. Placing the layers at the start of the model runs them on the GPU instead; that can sometimes help performance, but the data pipeline is usually the better option. section 8.2.5
  • Use augmentation alongside other regularization when needed. One worked setup combines augmented training images with dropout and monitors validation loss when saving the ConvNet. section 8.2.5

Augmentation does not create wholly independent examples: variations remain closely related to their originals. It helps cover anticipated changes in appearance, not situations for which the training data offers little or no basis. section 8.2.5 section 19.2.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through random transformations, pipeline placement, and training code; cautions that transformed samples remain correlated.
Examples
Shows flipped, rotated, and zoomed image variations, then trains a ConvNet on augmented batches.
What it assumes
Starts with an image Dataset and ConvNet, while showing how to map transformations over training images.
What you can do after
Apply it via Keras augmentation layers and a mapped training Dataset while checking the resulting images.
How it connects
Connects data augmentation to ConvNets, overfitting, dropout, and local generalization.

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 Python, Third Edition. The section links point to the parts of the book it draws on.