Data augmentation helps the model learn better by showing it many different versions of the same data. This stops the model from just memorizing and makes it better at guessing new data.
Data augmentation as regularization in TensorFlow
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip('horizontal'),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.1),
])
augmented_images = data_augmentation(original_images)Use tf.keras.Sequential to stack augmentation layers.
Apply augmentation only during training, not during testing or validation.
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip('horizontal_and_vertical'),
tf.keras.layers.RandomRotation(0.2),
])data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomContrast(0.2),
tf.keras.layers.RandomZoom(0.15),
])This code creates two random images and applies data augmentation layers to them. It prints the average pixel values before and after augmentation to show the images changed.
import tensorflow as tf import numpy as np # Create dummy image data: 2 images, 64x64 pixels, 3 color channels original_images = tf.random.uniform(shape=(2, 64, 64, 3)) # Define data augmentation layers data_augmentation = tf.keras.Sequential([ tf.keras.layers.RandomFlip('horizontal'), tf.keras.layers.RandomRotation(0.1), tf.keras.layers.RandomZoom(0.1), ]) # Apply augmentation augmented_images = data_augmentation(original_images) # Show difference between original and augmented images (mean pixel values) print(f"Original images mean pixel value: {tf.reduce_mean(original_images).numpy():.4f}") print(f"Augmented images mean pixel value: {tf.reduce_mean(augmented_images).numpy():.4f}")
Data augmentation works best when applied only during training, not during validation or testing.
Augmentation can slow down training because it changes data on the fly.
Try different augmentation types to see what helps your model most.
Data augmentation creates new training data by changing existing data slightly.
This helps the model learn better and avoid memorizing the training data.
Use TensorFlow's built-in layers like RandomFlip and RandomRotation to add augmentation easily.