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








