Data augmentation helps create more training images by changing existing ones. This makes models better at understanding new pictures.
Data augmentation for images in TensorFlow
import tensorflow as tf from tensorflow.keras import layers augmentation = tf.keras.Sequential([ layers.RandomFlip('horizontal'), layers.RandomRotation(0.1), layers.RandomZoom(0.1), ]) augmented_images = augmentation(original_images, training=True)
Use tf.keras.Sequential to stack augmentation layers.
Apply augmentation only on training data, not on validation or test data.
layers.RandomFlip('horizontal')layers.RandomRotation(0.2)layers.RandomZoom(height_factor=0.1, width_factor=0.1)
This code creates two random images and applies flipping, rotation, and zoom. It prints the shapes to show images are unchanged in size.
import tensorflow as tf from tensorflow.keras import layers import numpy as np # Create a batch of 2 dummy images (32x32 RGB) original_images = np.random.rand(2, 32, 32, 3).astype('float32') # Define augmentation pipeline augmentation = tf.keras.Sequential([ layers.RandomFlip('horizontal'), layers.RandomRotation(0.1), layers.RandomZoom(0.1), ]) # Apply augmentation augmented_images = augmentation(original_images, training=True) # Print shapes to confirm augmentation print('Original shape:', original_images.shape) print('Augmented shape:', augmented_images.shape)
Augmentation layers work only during training by default.
You can customize augmentation intensity by changing parameters.
Augmentation helps models generalize better to new images.
Data augmentation creates new images by changing existing ones.
Use TensorFlow augmentation layers like RandomFlip, RandomRotation, and RandomZoom.
Apply augmentation only on training data to improve model learning.
Practice
Solution
Step 1: Understand data augmentation concept
Data augmentation means making new images by changing existing images, like flipping or rotating.Step 2: Connect augmentation to model training
This helps the model see more variety and learn better, improving accuracy.Final Answer:
To create new images by modifying existing ones to improve model learning -> Option AQuick Check:
Data augmentation = create new images [OK]
- Thinking augmentation reduces dataset size
- Confusing augmentation with noise removal
- Believing augmentation converts images to text
Solution
Step 1: Identify the correct flip direction
To flip images horizontally, use 'horizontal' as the argument in RandomFlip.Step 2: Match the correct TensorFlow syntax
RandomFlip('horizontal') flips images left to right, which is the correct usage.Final Answer:
tf.keras.layers.RandomFlip('horizontal') -> Option AQuick Check:
Horizontal flip = RandomFlip('horizontal') [OK]
- Using 'vertical' instead of 'horizontal' for horizontal flip
- Confusing RandomFlip with RandomRotation
- Passing numeric values instead of strings to RandomFlip
augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip('horizontal'),
tf.keras.layers.RandomRotation(0.1)
])
augmented_images = augmentation(tf.random.uniform([32, 64, 64, 3]))
print(augmented_images.shape)Solution
Step 1: Understand input shape and augmentation effect
The input batch has shape (32, 64, 64, 3), meaning 32 images, each 64x64 pixels with 3 color channels.Step 2: Check if augmentation changes shape
RandomFlip and RandomRotation only modify image content, not size or batch count, so output shape stays the same.Final Answer:
(32, 64, 64, 3) -> Option CQuick Check:
Augmentation keeps shape same = (32, 64, 64, 3) [OK]
- Assuming rotation changes image size
- Confusing batch size with image dimensions
- Expecting channel count to change
augmentation = tf.keras.Sequential([ tf.keras.layers.RandomFlip(horizontal), tf.keras.layers.RandomRotation(0.2) ])
Solution
Step 1: Check RandomFlip argument type
RandomFlip expects a string such as 'horizontal' or 'vertical', but here horizontal is used without quotes, treated as a variable.Step 2: Confirm other parts are correct
RandomRotation accepts float values; Sequential can contain augmentation layers; input shape is optional here.Final Answer:
RandomFlip requires a string argument like 'horizontal', not a variable -> Option DQuick Check:
RandomFlip argument must be string [OK]
- Passing variable without quotes to RandomFlip
- Thinking RandomRotation disallows floats
- Believing Sequential can't hold augmentation layers
Solution
Step 1: Understand augmentation purpose
Augmentation is used to increase training data variety to help model learn better.Step 2: Decide where to apply augmentation
Applying augmentation only on training data pipeline ensures validation and test data remain original for fair evaluation.Final Answer:
Apply augmentation only on the training dataset pipeline before feeding to the model -> Option BQuick Check:
Augment training data only = Apply augmentation only on the training dataset pipeline before feeding to the model [OK]
- Augmenting validation or test data
- Applying augmentation after prediction
- Using augmentation layers inside model for all data
Ready to prove it?
Pass the test and this pattern moves unknown
