0
0
TensorFlowml~5 mins

Data augmentation in pipeline in TensorFlow

Choose your learning style9 modes available
Introduction

Data augmentation helps create more training examples by changing images slightly. This makes the model better at understanding new images.

When you have a small number of images to train your model.
When you want your model to recognize objects from different angles or lighting.
When you want to reduce overfitting by showing varied data during training.
When you want to improve model accuracy without collecting more data.
Syntax
TensorFlow
import tensorflow as tf

# Create a data augmentation pipeline
augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip('horizontal'),
    tf.keras.layers.RandomRotation(0.1),
    tf.keras.layers.RandomZoom(0.1),
])

# Use it inside your model or on your dataset
augmented_images = augmentation(images)

The pipeline is a sequence of layers that randomly change images.

You can add many types of augmentations like flip, rotate, zoom, and more.

Examples
This pipeline flips images both horizontally and vertically and rotates them up to 20%.
TensorFlow
augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip('horizontal_and_vertical'),
    tf.keras.layers.RandomRotation(0.2),
])
This pipeline changes the contrast and brightness of images randomly.
TensorFlow
augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomContrast(0.2),
    tf.keras.layers.RandomBrightness(0.1),
])
Sample Model

This code creates two random images, applies a data augmentation pipeline, and prints the shapes and average pixel change to show augmentation effect.

TensorFlow
import tensorflow as tf
import numpy as np

# Create a batch of 2 dummy images (64x64 RGB)
images = tf.random.uniform(shape=(2, 64, 64, 3), minval=0, maxval=1)

# Define augmentation pipeline
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 = augmentation(images)

# Check shapes and print a summary
print('Original images shape:', images.shape)
print('Augmented images shape:', augmented_images.shape)

# Show mean pixel value difference to confirm changes
mean_diff = tf.reduce_mean(tf.abs(augmented_images - images))
print(f'Mean pixel difference after augmentation: {mean_diff.numpy():.4f}')
OutputSuccess
Important Notes

Data augmentation layers work only during training by default.

You can include augmentation inside your model or apply it on datasets before training.

Augmentation helps models generalize better by seeing varied data.

Summary

Data augmentation creates new training images by randomly changing originals.

Use TensorFlow's Sequential model with augmentation layers to build a pipeline.

Applying augmentation improves model accuracy and reduces overfitting.