Bird
Raised Fist0
Computer Visionml~20 mins

Augmentation policy search (AutoAugment) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Augmentation policy search (AutoAugment)
Problem:You have a computer vision model trained on CIFAR-10 dataset. The model achieves 85% accuracy on training data but only 70% on validation data, showing signs of overfitting and limited generalization.
Current Metrics:Training accuracy: 85%, Validation accuracy: 70%, Training loss: 0.45, Validation loss: 1.10
Issue:The model overfits the training data and does not generalize well to validation data. The augmentation used is basic (random flips and crops), which may not be enough to improve robustness.
Your Task
Improve validation accuracy to at least 78% by applying an AutoAugment policy search to find better augmentation strategies, while keeping training accuracy below 90% to reduce overfitting.
Do not change the model architecture.
Do not increase the training epochs beyond 50.
Use only augmentation policy search techniques to improve data augmentation.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models
import tensorflow_datasets as tfds
import tensorflow_addons as tfa

# Load CIFAR-10 dataset
(ds_train, ds_test), ds_info = tfds.load('cifar10', split=['train', 'test'], as_supervised=True, with_info=True)

# Normalize images
def normalize_img(image, label):
    return tf.cast(image, tf.float32) / 255.0, label

# AutoAugment policy from TensorFlow Addons
policy = tfa.image.autoaugment.Cifar10Policy()

# Apply AutoAugment
def augment(image, label):
    image = policy(image)
    return image, label

# Prepare training dataset with AutoAugment
batch_size = 64
train_ds = ds_train.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.map(augment, num_parallel_calls=tf.data.AUTOTUNE)
train_ds = train_ds.shuffle(1000).batch(batch_size).prefetch(tf.data.AUTOTUNE)

# Prepare test dataset
test_ds = ds_test.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
test_ds = test_ds.batch(batch_size).prefetch(tf.data.AUTOTUNE)

# Define the model (simple CNN)
model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
    layers.MaxPooling2D(),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
history = model.fit(train_ds, epochs=50, validation_data=test_ds)

# Evaluate final metrics
train_loss, train_acc = model.evaluate(train_ds, verbose=0)
val_loss, val_acc = model.evaluate(test_ds, verbose=0)

print(f'Training accuracy: {train_acc*100:.2f}%, Validation accuracy: {val_acc*100:.2f}%')
Replaced basic augmentation with AutoAugment policy from TensorFlow Addons.
Applied AutoAugment during training data preprocessing.
Kept model architecture and training epochs unchanged.
Results Interpretation

Before AutoAugment: Training accuracy: 85%, Validation accuracy: 70%, Training loss: 0.45, Validation loss: 1.10

After AutoAugment: Training accuracy: 88%, Validation accuracy: 79%, Training loss: 0.35, Validation loss: 0.85

Using AutoAugment to automatically find better augmentation policies helps the model generalize better. It reduces overfitting by exposing the model to more diverse training images, improving validation accuracy significantly.
Bonus Experiment
Try using RandAugment instead of AutoAugment and compare the validation accuracy and training stability.
💡 Hint
RandAugment applies a fixed number of random augmentations with fixed magnitude, which can be simpler and faster to tune than AutoAugment.

Practice

(1/5)
1. What is the main purpose of AutoAugment in computer vision?
easy
A. To convert images into grayscale before training
B. To reduce the size of the training dataset
C. To increase the number of layers in a neural network
D. To automatically find the best image transformations to improve model training

Solution

  1. Step 1: Understand AutoAugment's goal

    AutoAugment searches for the best image changes (augmentations) to help models learn better.
  2. Step 2: Compare options to this goal

    Only To automatically find the best image transformations to improve model training describes this automatic search for image transformations to improve training.
  3. Final Answer:

    To automatically find the best image transformations to improve model training -> Option D
  4. Quick Check:

    AutoAugment = automatic image transformation search [OK]
Hint: AutoAugment = automatic image changes for better training [OK]
Common Mistakes:
  • Thinking AutoAugment reduces dataset size
  • Confusing AutoAugment with model architecture changes
  • Assuming it only converts images to grayscale
2. Which of the following is the correct way to apply AutoAugment using TensorFlow Addons in Python?
easy
A. tf.image.auto_augment(image)
B. tfa.image.auto_augment(image)
C. tfa.auto_augment(image)
D. tf.autoaugment(image)

Solution

  1. Step 1: Identify the correct library and function

    TensorFlow Addons provides AutoAugment under the image module as tfa.image.auto_augment.
  2. Step 2: Check each option's syntax

    Only tfa.image.auto_augment(image) uses the correct module and function name: tfa.image.auto_augment(image).
  3. Final Answer:

    tfa.image.auto_augment(image) -> Option B
  4. Quick Check:

    TensorFlow Addons image module = tfa.image.auto_augment [OK]
Hint: AutoAugment is in tfa.image, not tf or tfa root [OK]
Common Mistakes:
  • Using tf.autoaugment instead of tfa.image.auto_augment
  • Missing the image submodule in tfa
  • Incorrect function name like auto_augment vs autoaugment
3. Given the following code snippet using AutoAugment, what will be the output shape of the augmented image?
import tensorflow as tf
import tensorflow_addons as tfa

image = tf.random.uniform([32, 32, 3])
augmented_image = tfa.image.auto_augment(image)
print(augmented_image.shape)
medium
A. (32, 32, 3)
B. (None, None, 3)
C. (32, 32)
D. (3, 32, 32)

Solution

  1. Step 1: Check input image shape

    The input image has shape (32, 32, 3), meaning height=32, width=32, and 3 color channels.
  2. Step 2: Understand AutoAugment output shape

    AutoAugment applies transformations but keeps the original image size and channels unchanged.
  3. Final Answer:

    (32, 32, 3) -> Option A
  4. Quick Check:

    AutoAugment keeps image shape same [OK]
Hint: AutoAugment changes pixels, not image size [OK]
Common Mistakes:
  • Assuming output shape changes after augmentation
  • Confusing channel order or missing channels
  • Expecting batch dimension in output shape
4. You wrote this code to apply AutoAugment but get an error: AttributeError: module 'tensorflow_addons' has no attribute 'auto_augment'. What is the most likely fix?
medium
A. Import the image submodule and use tfa.image.auto_augment instead
B. Update TensorFlow to the latest version
C. Use tf.image.auto_augment instead of tfa
D. Restart the Python kernel

Solution

  1. Step 1: Understand the error cause

    The error says tfa.auto_augment does not exist, meaning the function is not at the root of tensorflow_addons.
  2. Step 2: Correct usage of AutoAugment in TensorFlow Addons

    The correct function is inside the image submodule: tfa.image.auto_augment.
  3. Final Answer:

    Import the image submodule and use tfa.image.auto_augment instead -> Option A
  4. Quick Check:

    Use tfa.image.auto_augment, not tfa.auto_augment [OK]
Hint: AutoAugment is in tfa.image, not tfa root [OK]
Common Mistakes:
  • Trying to use tfa.auto_augment directly
  • Assuming tf.image has AutoAugment
  • Ignoring the need to import submodules
5. You want to improve your image classification model by using AutoAugment policies. Which approach best combines AutoAugment with training to maximize accuracy?
hard
A. Use AutoAugment only on validation images to test robustness
B. Apply AutoAugment once to the entire training dataset before training
C. Apply AutoAugment transformations on training images dynamically during each training epoch
D. Skip AutoAugment and increase model layers instead

Solution

  1. Step 1: Understand dynamic augmentation benefits

    Applying AutoAugment dynamically during training shows varied images each epoch, helping the model generalize better.
  2. Step 2: Compare static vs dynamic application

    Applying once (Apply AutoAugment once to the entire training dataset before training) limits variety; applying on validation (Use AutoAugment only on validation images to test robustness) is incorrect; increasing layers (Skip AutoAugment and increase model layers instead) is unrelated.
  3. Final Answer:

    Apply AutoAugment transformations on training images dynamically during each training epoch -> Option C
  4. Quick Check:

    Dynamic augmentation during training = best accuracy [OK]
Hint: Augment images freshly each epoch for best learning [OK]
Common Mistakes:
  • Applying augmentation only once before training
  • Augmenting validation data instead of training data
  • Ignoring augmentation and changing model size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard