0
0
TensorFlowml~20 mins

Feature extraction approach in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Feature extraction approach
Problem:We want to classify images of cats and dogs using a neural network. Currently, the model is trained from scratch on a small dataset.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Training loss: 0.15, Validation loss: 0.65
Issue:The model is overfitting: training accuracy is high but validation accuracy is much lower.
Your Task
Reduce overfitting by using a feature extraction approach with a pre-trained model, aiming for validation accuracy above 85% while keeping training accuracy below 90%.
Use TensorFlow and Keras only.
Do not increase the dataset size.
Do not train the entire pre-trained model, only the top layers.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing.image import ImageDataGenerator

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

# Add classification head
model = models.Sequential([
    base_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(1, activation='sigmoid')
])

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

# Data generators with 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(
    'cats_and_dogs/train', target_size=(160, 160), batch_size=32, class_mode='binary', subset='training')

validation_generator = train_datagen.flow_from_directory(
    'cats_and_dogs/train', target_size=(160, 160), batch_size=32, class_mode='binary', subset='validation')

# Train only the top layers
history = model.fit(train_generator, epochs=10, validation_data=validation_generator)
Replaced training from scratch with MobileNetV2 pre-trained model as feature extractor.
Froze the base model layers to prevent overfitting.
Added a small classifier on top with dropout for regularization.
Used data augmentation to improve generalization.
Results Interpretation

Before: Training accuracy 95%, Validation accuracy 70%, showing overfitting.

After: Training accuracy 88%, Validation accuracy 87%, showing better generalization.

Using a pre-trained model as a fixed feature extractor helps reduce overfitting and improves validation accuracy by leveraging learned features from a large dataset.
Bonus Experiment
Try fine-tuning the last few layers of the pre-trained model after initial training to see if validation accuracy improves further.
💡 Hint
Unfreeze the last 10 layers of MobileNetV2 and train with a very low learning rate.