Bird
Raised Fist0
TensorFlowml~20 mins

Data augmentation for images in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Augmentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output shape after applying random flip augmentation
Consider the following TensorFlow code that applies a random horizontal flip to a batch of images. What is the shape of the output tensor?
TensorFlow
import tensorflow as tf

images = tf.random.uniform([16, 64, 64, 3])  # batch of 16 images, 64x64 RGB
augmented_images = tf.image.random_flip_left_right(images)
output_shape = augmented_images.shape
print(output_shape)
A(16, 64, 64, 3)
B(64, 64, 3)
C(16, 128, 128, 3)
D(16, 64, 64)
Attempts:
2 left
💡 Hint
The random flip changes pixel order but not the shape.
❓ Model Choice
intermediate
2:00remaining
Choosing augmentation for small dataset
You have a small image dataset and want to reduce overfitting by augmenting data during training. Which augmentation technique is best suited to increase data variety without changing image content drastically?
ARandom rotation by 90 degrees increments only
BRandom cropping with resizing back to original size
CRandom color inversion (negative colors)
DRandom Gaussian noise addition
Attempts:
2 left
💡 Hint
Think about preserving main image features while increasing variety.
❓ Metrics
advanced
2:00remaining
Effect of augmentation on validation accuracy
A model is trained twice: once with data augmentation and once without. The validation accuracy with augmentation is 85%, without augmentation is 78%. What does this difference most likely indicate?
AAugmentation reduced model capacity
BAugmentation caused overfitting on training data
CAugmentation improved model generalization to unseen data
DAugmentation caused data leakage
Attempts:
2 left
💡 Hint
Higher validation accuracy usually means better generalization.
🔧 Debug
advanced
2:00remaining
Debugging augmentation pipeline error
You run this TensorFlow code but get a runtime error. What is the cause? ```python import tensorflow as tf images = tf.random.uniform([8, 32, 32, 3]) augmented = tf.image.random_brightness(images, max_delta=0.5) augmented = tf.image.random_flip_left_right(augmented) augmented = tf.image.random_crop(augmented, size=[8, 28, 28, 3]) ```
Arandom_crop cannot be applied after random_flip_left_right
Brandom_brightness requires images to be uint8, not float32
Crandom_flip_left_right only works on grayscale images
Drandom_crop size must be smaller than input size but batch dimension cannot be included
Attempts:
2 left
💡 Hint
Check the shape argument for random_crop carefully.
🧠 Conceptual
expert
2:00remaining
Why use augmentation only on training data?
Why is data augmentation typically applied only to training images and not to validation or test images?
ATo simulate more diverse training data and fairly evaluate model on original data
BBecause augmentation algorithms are too slow for validation and test sets
CBecause validation and test images are already augmented by default
DTo prevent the model from memorizing augmented images in validation and test sets
Attempts:
2 left
💡 Hint
Think about the purpose of validation and test sets.

Practice

(1/5)
1. What is the main purpose of data augmentation in image training with TensorFlow?
easy
A. To create new images by modifying existing ones to improve model learning
B. To reduce the size of the training dataset
C. To convert images into text format
D. To remove noise from images before training

Solution

  1. Step 1: Understand data augmentation concept

    Data augmentation means making new images by changing existing images, like flipping or rotating.
  2. Step 2: Connect augmentation to model training

    This helps the model see more variety and learn better, improving accuracy.
  3. Final Answer:

    To create new images by modifying existing ones to improve model learning -> Option A
  4. Quick Check:

    Data augmentation = create new images [OK]
Hint: Augmentation means making new images from old ones [OK]
Common Mistakes:
  • Thinking augmentation reduces dataset size
  • Confusing augmentation with noise removal
  • Believing augmentation converts images to text
2. Which TensorFlow layer is correctly used to flip images horizontally during training?
easy
A. tf.keras.layers.RandomFlip('horizontal')
B. tf.keras.layers.RandomFlip('vertical')
C. tf.keras.layers.RandomRotation(0.5)
D. tf.keras.layers.RandomZoom(0.2)

Solution

  1. Step 1: Identify the correct flip direction

    To flip images horizontally, use 'horizontal' as the argument in RandomFlip.
  2. Step 2: Match the correct TensorFlow syntax

    RandomFlip('horizontal') flips images left to right, which is the correct usage.
  3. Final Answer:

    tf.keras.layers.RandomFlip('horizontal') -> Option A
  4. Quick Check:

    Horizontal flip = RandomFlip('horizontal') [OK]
Hint: Use 'horizontal' string inside RandomFlip for left-right flip [OK]
Common Mistakes:
  • Using 'vertical' instead of 'horizontal' for horizontal flip
  • Confusing RandomFlip with RandomRotation
  • Passing numeric values instead of strings to RandomFlip
3. What will be the output shape after applying this augmentation layer to a batch of 32 images of size 64x64x3?
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)
medium
A. (32, 128, 128, 3)
B. (64, 64, 3)
C. (32, 64, 64, 3)
D. (32, 64, 64)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

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

    Augmentation keeps shape same = (32, 64, 64, 3) [OK]
Hint: Augmentation changes pixels, not image shape [OK]
Common Mistakes:
  • Assuming rotation changes image size
  • Confusing batch size with image dimensions
  • Expecting channel count to change
4. Identify the error in this TensorFlow augmentation code snippet:
augmentation = tf.keras.Sequential([
  tf.keras.layers.RandomFlip(horizontal),
  tf.keras.layers.RandomRotation(0.2)
])
medium
A. Missing input shape in Sequential
B. RandomRotation cannot take float values
C. Sequential cannot contain augmentation layers
D. RandomFlip requires a string argument like 'horizontal', not a variable

Solution

  1. 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.
  2. Step 2: Confirm other parts are correct

    RandomRotation accepts float values; Sequential can contain augmentation layers; input shape is optional here.
  3. Final Answer:

    RandomFlip requires a string argument like 'horizontal', not a variable -> Option D
  4. Quick Check:

    RandomFlip argument must be string [OK]
Hint: Put flip direction in quotes: 'horizontal' [OK]
Common Mistakes:
  • Passing variable without quotes to RandomFlip
  • Thinking RandomRotation disallows floats
  • Believing Sequential can't hold augmentation layers
5. You want to augment only the training images but not validation or test images. Which is the best way to apply augmentation in TensorFlow?
hard
A. Apply augmentation layers inside the model and use them for all data
B. Apply augmentation only on the training dataset pipeline before feeding to the model
C. Augment validation and test data to increase evaluation accuracy
D. Apply augmentation after model prediction to improve results

Solution

  1. Step 1: Understand augmentation purpose

    Augmentation is used to increase training data variety to help model learn better.
  2. Step 2: Decide where to apply augmentation

    Applying augmentation only on training data pipeline ensures validation and test data remain original for fair evaluation.
  3. Final Answer:

    Apply augmentation only on the training dataset pipeline before feeding to the model -> Option B
  4. Quick Check:

    Augment training data only = Apply augmentation only on the training dataset pipeline before feeding to the model [OK]
Hint: Augment only training data, not validation/test [OK]
Common Mistakes:
  • Augmenting validation or test data
  • Applying augmentation after prediction
  • Using augmentation layers inside model for all data
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