0
0
Computer Visionml~20 mins

Image augmentation transforms in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Image augmentation transforms
Problem:You have a small image dataset for a classification task. The current model trains well on the training images but performs poorly on new images, showing signs of overfitting.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Training loss: 0.15, Validation loss: 0.65
Issue:The model overfits the training data and does not generalize well to validation data due to limited and similar training images.
Your Task
Use image augmentation transforms to increase data variety and reduce overfitting, aiming to improve validation accuracy to at least 80% while keeping training accuracy below 92%.
Do not change the model architecture.
Only modify the data loading and preprocessing pipeline to add augmentation.
Use common augmentation techniques like rotation, flipping, and zoom.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models

# Load dataset
(train_images, train_labels), (val_images, val_labels) = tf.keras.datasets.cifar10.load_data()

# Normalize images
train_images = train_images / 255.0
val_images = val_images / 255.0

# Define image augmentation pipeline
data_augmentation = tf.keras.Sequential([
    layers.RandomFlip('horizontal'),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
    layers.RandomTranslation(0.1, 0.1)
])

# Build model (same architecture as before)
model = models.Sequential([
    layers.Input(shape=(32, 32, 3)),
    data_augmentation,  # Apply augmentation only during training
    layers.Conv2D(32, (3,3), activation='relu'),
    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 model
history = model.fit(train_images, train_labels, epochs=20, batch_size=64, validation_data=(val_images, val_labels))
Added a data augmentation pipeline with random horizontal flip, rotation, zoom, and translation.
Inserted the augmentation layer at the start of the model to augment training images on the fly.
Kept validation data unchanged to properly measure generalization.
Results Interpretation

Before augmentation: Training accuracy was 95%, validation accuracy was 70%. The model overfitted the training data.

After augmentation: Training accuracy decreased to 90%, validation accuracy improved to 82%. Loss values also show better generalization.

Image augmentation artificially increases data variety, helping the model learn more robust features and reducing overfitting. This improves validation accuracy by making the model better at handling new, unseen images.
Bonus Experiment
Try adding color jitter (random brightness and contrast changes) to the augmentation pipeline and observe if validation accuracy improves further.
💡 Hint
Use layers.RandomBrightness and layers.RandomContrast from TensorFlow or implement custom augmentation functions.