Bird
Raised Fist0
Computer Visionml~20 mins

Color transforms (brightness, contrast, hue) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Color transforms (brightness, contrast, hue)
Problem:You have a dataset of images for a classification task. The model is trained on original images but performs poorly on images with different lighting conditions. This is because the model is not robust to changes in brightness, contrast, and hue.
Current Metrics:Training accuracy: 92%, Validation accuracy: 70%, Validation loss: 0.85
Issue:The model overfits to the original lighting conditions and does not generalize well to images with varied brightness, contrast, and hue.
Your Task
Improve the model's robustness by applying color transformations (brightness, contrast, hue adjustments) as data augmentation during training. Target validation accuracy > 80% while keeping training accuracy below 90% to reduce overfitting.
Do not change the model architecture.
Only modify the data preprocessing pipeline to include color transforms.
Keep training epochs and batch size the same.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim

# Define color transforms for training
train_transforms = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.RandomApply([
        transforms.ColorJitter(brightness=0.3, contrast=0.3, hue=0.1)
    ], p=0.8),
    transforms.ToTensor(),
])

# Validation transforms (no color jitter)
val_transforms = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
])

# Load datasets
train_dataset = datasets.FakeData(size=1000, num_classes=10, transform=train_transforms)
val_dataset = datasets.FakeData(size=200, num_classes=10, transform=val_transforms)

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32)

# Simple model
model = nn.Sequential(
    nn.Flatten(),
    nn.Linear(3*224*224, 100),
    nn.ReLU(),
    nn.Linear(100, 10)
)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

def train_one_epoch():
    model.train()
    total, correct, loss_sum = 0, 0, 0
    for images, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        loss_sum += loss.item() * images.size(0)
        _, predicted = outputs.max(1)
        total += labels.size(0)
        correct += predicted.eq(labels).sum().item()
    return loss_sum / total, correct / total * 100

def validate():
    model.eval()
    total, correct, loss_sum = 0, 0, 0
    with torch.no_grad():
        for images, labels in val_loader:
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss_sum += loss.item() * images.size(0)
            _, predicted = outputs.max(1)
            total += labels.size(0)
            correct += predicted.eq(labels).sum().item()
    return loss_sum / total, correct / total * 100

# Training loop
for epoch in range(10):
    train_loss, train_acc = train_one_epoch()
    val_loss, val_acc = validate()
    print(f"Epoch {epoch+1}: Train loss {train_loss:.3f}, Train acc {train_acc:.1f}%, Val loss {val_loss:.3f}, Val acc {val_acc:.1f}%")
Added RandomApply with ColorJitter (brightness=0.3, contrast=0.3, hue=0.1) to training data transforms.
Kept validation transforms unchanged to evaluate on original images.
Kept model architecture and training parameters the same.
Added 'size' parameter to FakeData to specify dataset size for training and validation.
Results Interpretation

Before augmentation: Training accuracy 92%, Validation accuracy 70%, Validation loss 0.85

After augmentation: Training accuracy 88%, Validation accuracy 82%, Validation loss 0.65

Applying color transformations as data augmentation helps the model learn to handle different lighting conditions, reducing overfitting and improving validation accuracy.
Bonus Experiment
Try adding random rotation and horizontal flip along with color transforms to see if the model improves further.
💡 Hint
Use transforms.RandomRotation and transforms.RandomHorizontalFlip in the training transform pipeline.

Practice

(1/5)
1. What does increasing the brightness of an image do to its pixel values?
easy
A. It makes pixel values larger, making the image lighter
B. It rotates the colors around the color wheel
C. It stretches the difference between pixel values
D. It converts the image to grayscale

Solution

  1. Step 1: Understand brightness effect on pixels

    Brightness changes add or multiply pixel values to make them lighter or darker.
  2. Step 2: Identify what increasing brightness means

    Increasing brightness means making pixel values larger, so the image looks lighter.
  3. Final Answer:

    It makes pixel values larger, making the image lighter -> Option A
  4. Quick Check:

    Brightness increase = lighter pixels [OK]
Hint: Brightness changes scale pixel values up or down [OK]
Common Mistakes:
  • Confusing brightness with contrast
  • Thinking brightness rotates colors
  • Assuming brightness converts to grayscale
2. Which of the following is the correct way to increase contrast of an image using a simple formula?
easy
A. new_pixel = (old_pixel - 128) * 1.5 + 128
B. new_pixel = old_pixel + 50
C. new_pixel = old_pixel / 2
D. new_pixel = old_pixel * 0

Solution

  1. Step 1: Recall contrast adjustment formula

    Contrast changes stretch pixel values around the midpoint (usually 128 for 8-bit images).
  2. Step 2: Identify correct formula

    The formula (old_pixel - 128) * factor + 128 increases contrast by scaling differences from midpoint.
  3. Final Answer:

    new_pixel = (old_pixel - 128) * 1.5 + 128 -> Option A
  4. Quick Check:

    Contrast = scale around midpoint [OK]
Hint: Contrast scales difference from midpoint, not just add values [OK]
Common Mistakes:
  • Adding a constant increases brightness, not contrast
  • Dividing pixel values reduces brightness
  • Multiplying by zero makes image black
3. Given this Python code snippet using OpenCV, what will be the effect on the image stored in img?
import cv2
import numpy as np
img = cv2.imread('image.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv[:, :, 0] = (hsv[:, :, 0] + 30) % 180
img_shifted = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
medium
A. The image is converted to grayscale
B. The image contrast is increased by 30%
C. The image brightness is increased by 30 units
D. The image hue is shifted by 30 degrees on the color wheel

Solution

  1. Step 1: Understand HSV color space and hue channel

    HSV separates color into Hue, Saturation, and Value. Hue channel controls color angle on the wheel.
  2. Step 2: Analyze code effect on hue channel

    The code adds 30 to the hue channel and wraps around with modulo 180, shifting colors on the wheel.
  3. Final Answer:

    The image hue is shifted by 30 degrees on the color wheel -> Option D
  4. Quick Check:

    Hue shift = color rotation [OK]
Hint: Hue changes rotate colors in HSV space [OK]
Common Mistakes:
  • Confusing hue shift with brightness or contrast
  • Ignoring modulo wrap-around for hue
  • Assuming color space conversion changes brightness
4. The following code attempts to increase image contrast but does not work as expected. What is the main error?
def increase_contrast(img):
    mean = img.mean()
    return (img - mean) * 1.2 + mean

result = increase_contrast(image)
medium
A. The image should be converted to grayscale first
B. The multiplication factor should be less than 1
C. The mean should be computed per channel, not overall
D. The code should add 1.2 instead of multiplying

Solution

  1. Step 1: Understand contrast adjustment per channel

    Contrast should be adjusted per color channel to preserve color balance.
  2. Step 2: Identify error in mean calculation

    The code calculates mean over entire image, mixing channels, causing wrong contrast scaling.
  3. Final Answer:

    The mean should be computed per channel, not overall -> Option C
  4. Quick Check:

    Contrast per channel = correct scaling [OK]
Hint: Calculate mean per channel for contrast adjustment [OK]
Common Mistakes:
  • Using single mean for all channels
  • Using factor less than 1 reduces contrast
  • Adding constant changes brightness, not contrast
5. You want to apply a color transform pipeline to an image that first increases brightness by 20%, then shifts hue by 45 degrees, and finally increases contrast by 30%. Which order of operations is best to preserve natural colors?
hard
A. Increase contrast -> Shift hue -> Increase brightness
B. Increase brightness -> Increase contrast -> Shift hue
C. Shift hue -> Increase brightness -> Increase contrast
D. Shift hue -> Increase contrast -> Increase brightness

Solution

  1. Step 1: Consider effect of brightness before contrast

    Increasing brightness first ensures pixel values are lifted before stretching differences with contrast.
  2. Step 2: Apply hue shift last to avoid color distortion

    Hue shift should be last to rotate colors after brightness and contrast adjustments.
  3. Step 3: Confirm order preserves natural colors

    Brightness -> Contrast -> Hue maintains natural look by adjusting lightness and contrast before color rotation.
  4. Final Answer:

    Increase brightness -> Increase contrast -> Shift hue -> Option B
  5. Quick Check:

    Brightness then contrast then hue = natural colors [OK]
Hint: Adjust brightness and contrast before hue shift [OK]
Common Mistakes:
  • Shifting hue before brightness causes unnatural colors
  • Increasing contrast before brightness can clip pixels
  • Applying hue shift first distorts brightness and contrast
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