0
0
Computer Visionml~20 mins

Pre-trained models (ResNet, VGG, EfficientNet) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Pre-trained models (ResNet, VGG, EfficientNet)
Problem:You want to classify images into 10 categories using a pre-trained model. Currently, you use a ResNet50 model fine-tuned on your dataset. The training accuracy is 98%, but validation accuracy is only 75%. This shows overfitting.
Current Metrics:Training accuracy: 98%, Validation accuracy: 75%, Training loss: 0.05, Validation loss: 0.85
Issue:The model overfits the training data, causing poor validation accuracy. The gap between training and validation accuracy is large.
Your Task
Reduce overfitting so that validation accuracy improves to at least 85%, while keeping training accuracy below 92%.
You must use the ResNet50 pre-trained model as base.
You can only change hyperparameters like learning rate, batch size, epochs, and add regularization techniques.
Do not change the dataset or model architecture.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping

# Load pre-trained ResNet50 without top layer
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
base_model.trainable = False  # Freeze base model

# Add custom layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)  # Added dropout to reduce overfitting
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

# Compile model with lower learning rate
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Data augmentation
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    validation_split=0.2
)

train_generator = train_datagen.flow_from_directory(
    'path_to_dataset',
    target_size=(224,224),
    batch_size=32,
    class_mode='categorical',
    subset='training'
)

val_datagen = ImageDataGenerator(
    rescale=1./255,
    validation_split=0.2
)

validation_generator = val_datagen.flow_from_directory(
    'path_to_dataset',
    target_size=(224,224),
    batch_size=32,
    class_mode='categorical',
    subset='validation'
)

# Early stopping callback
early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)

# Train model
history = model.fit(
    train_generator,
    epochs=30,
    validation_data=validation_generator,
    callbacks=[early_stop]
)
Added a Dropout layer with rate 0.5 after global average pooling to reduce overfitting.
Used data augmentation with rotation, shifts, and flips to increase training data variety.
Lowered learning rate from default to 0.0001 for smoother training.
Added EarlyStopping callback to stop training when validation loss stops improving.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 75%, large gap showing overfitting.

After: Training accuracy 90%, Validation accuracy 87%, smaller gap showing better generalization.

Adding dropout, data augmentation, lowering learning rate, and early stopping helps reduce overfitting. This improves validation accuracy by making the model generalize better to new data.
Bonus Experiment
Try using EfficientNetB0 pre-trained model instead of ResNet50 and compare validation accuracy.
💡 Hint
EfficientNet models are more efficient and may give better accuracy with fewer parameters. Use similar fine-tuning steps.