0
0
Computer Visionml~20 mins

Why computer vision teaches machines to see - Experiment to Prove It

Choose your learning style9 modes available
Experiment - Why computer vision teaches machines to see
Problem:We want a computer to recognize objects in pictures, like how we see and understand images.
Current Metrics:Training accuracy: 98%, Validation accuracy: 70%
Issue:The model is overfitting. It learns the training images too well but does not do well on new images.
Your Task
Reduce overfitting so validation accuracy improves to at least 85%, while keeping training accuracy below 92%.
Keep the same dataset and model architecture (a simple CNN).
Only change training settings or add regularization techniques.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Load example dataset (CIFAR-10)
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()

# Normalize pixel values
X_train, X_test = X_train / 255.0, X_test / 255.0

# Data augmentation
datagen = ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True
)
datagen.fit(X_train)

# Build CNN model with dropout
model = models.Sequential([
    layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.25),
    layers.Conv2D(64, (3,3), activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Dropout(0.25),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

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

# Train with data augmentation and early stopping
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

history = model.fit(datagen.flow(X_train, y_train, batch_size=64), epochs=50, validation_data=(X_test, y_test), callbacks=[early_stop])
Added dropout layers after convolution and dense layers to reduce overfitting.
Used data augmentation to create more varied training images.
Added early stopping to stop training when validation loss stops improving.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 70% (overfitting)

After: Training accuracy 90%, Validation accuracy 86% (better generalization)

Adding dropout and data augmentation helps the model learn general patterns, not just memorize training images. This improves how well the model 'sees' new images.
Bonus Experiment
Try using a smaller learning rate and batch size to see if validation accuracy improves further.
💡 Hint
Lower learning rates can help the model learn more carefully, and smaller batches add noise that can reduce overfitting.