Using Pre-trained Models for Image Classification in AI for Beginners: A Practical Guide
You can leverage ImageNet-trained architectures like MobileNetV2 or ResNet-50 for custom image classification tasks by replacing the final classification layer and fine-tuning on your dataset, using the utility modules tfcv.py and pytorchcv.py from Microsoft's AI-For-Beginners repository.
The AI-For-Beginners curriculum provides a complete transfer learning pipeline that teaches beginners how to apply pre-trained models to new image classification tasks. This guide covers the repository's dual-stack approach, showing exactly how to implement transfer learning with both TensorFlow/Keras and PyTorch using the provided helper utilities.
Repository Structure and Utility Modules
The transfer learning lesson in lessons/4-ComputerVision/08-TransferLearning/ provides two complementary utility files designed to abstract away boilerplate code:
tfcv.py– TensorFlow/Keras utilities for dataset loading, JPEG validation, and visualizationpytorchcv.py– PyTorch equivalents exposing dataset loaders, training loops, and plotting helpers
Neither file defines the neural network architecture itself. This deliberate separation of concerns allows you to plug in any pre-trained model from tf.keras.applications or torchvision.models while the utilities handle data preparation and training logistics.
The Transfer Learning Workflow
The curriculum follows a six-step workflow that works identically across both frameworks.
1. Dataset Preparation
Both utilities provide a load_cats_dogs_dataset function that downloads the Kaggle Cats-vs-Dogs dataset, removes corrupted JPEG files, and returns framework-specific data objects:
- TensorFlow: Returns
(train_ds, val_ds)astf.data.Datasetobjects - PyTorch: Returns
(dataset, train_loader, test_loader)withtorch.utils.data.DataLoaderinstances
2. Image Pre-processing
Images are automatically resized to 224 × 224 pixels and normalized. The PyTorch implementation in pytorchcv.py uses common_transform() which applies standard ImageNet normalization:
- Mean:
[0.485, 0.456, 0.406] - Standard deviation:
[0.229, 0.224, 0.225]
3. Model Selection
Import any pre-trained architecture using the pretrained=True (PyTorch) or weights='imagenet' (TensorFlow) flag to load ImageNet-trained weights:
- TensorFlow:
tf.keras.applications.MobileNetV2,ResNet50, etc. - PyTorch:
torchvision.models.resnet50,mobilenet_v2, etc.
4. Head Replacement
Since Cats-vs-Dogs requires binary classification (2 classes) instead of ImageNet's 1000 classes, replace the final dense layer:
- TensorFlow: Set
include_top=Falseand addDense(2, activation='softmax') - PyTorch: Replace
model.fcwithnn.Linear(1000, 2)
5. Fine-tuning Strategy
Freeze the backbone to train only the new classification head initially:
- TensorFlow:
base_model.trainable = False - PyTorch:
for param in model.parameters(): param.requires_grad = False(then unfreezemodel.fc)
6. Training and Evaluation
Use the repository's training utilities to track loss and accuracy:
- TensorFlow: Standard
model.fit()withtfcv.plot_results(history) - PyTorch:
cv.train(model, train_loader, test_loader, epochs=5, lr=1e-3)withcv.plot_results(history)
TensorFlow/Keras Implementation
This example from lessons/4-ComputerVision/08-TransferLearning/tfcv.py demonstrates fine-tuning MobileNetV2:
import tensorflow as tf
from tensorflow import keras
from lessons.4-ComputerVision.08-TransferLearning import tfcv
# Load and preprocess dataset
train_ds, val_ds = tfcv.load_cats_dogs_dataset(batch_size=32)
# Load pre-trained MobileNetV2 without top layers
base_model = tf.keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False,
weights='imagenet'
)
base_model.trainable = False # Freeze backbone
# Add custom classification head
global_average = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
output = tf.keras.layers.Dense(2, activation='softmax')(global_average)
model = tf.keras.Model(inputs=base_model.input, outputs=output)
# Compile and train
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_ds, validation_data=val_ds, epochs=5)
# Visualize results using repository helper
tfcv.plot_results(history)
Key references:
- Dataset loader:
tfcv.load_cats_dogs_datasetinlessons/4-ComputerVision/08-TransferLearning/tfcv.py - Visualization:
tfcv.plot_resultsfor training curve plotting
PyTorch Implementation
This implementation uses lessons/4-ComputerVision/08-TransferLearning/pytorchcv.py with ResNet-50:
import torch
import torchvision
from lessons.4-ComputerVision.08-TransferLearning import pytorchcv as cv
# Load dataset with automatic transforms
dataset, train_loader, test_loader = cv.load_cats_dogs_dataset()
# Initialize pre-trained ResNet-50
model = torchvision.models.resnet50(pretrained=True)
# Replace final fully connected layer for binary classification
model.fc = torch.nn.Linear(model.fc.in_features, 2)
model = model.to(cv.default_device)
# Freeze backbone, unfreeze only the new head
for param in model.parameters():
param.requires_grad = False
for param in model.fc.parameters():
param.requires_grad = True
# Train using repository helper
history = cv.train(model, train_loader, test_loader, epochs=5, lr=1e-3)
# Plot learning curves
cv.plot_results(history)
Key references:
- Data pipeline:
cv.load_cats_dogs_datasethandlescommon_transform()with ImageNet normalization - Training loop:
cv.trainand validation inlessons/4-ComputerVision/08-TransferLearning/pytorchcv.py
Summary
- The AI-For-Beginners repository provides framework-agnostic utilities (
tfcv.pyandpytorchcv.py) that standardize dataset loading and visualization across TensorFlow and PyTorch. - Both implementations follow the same six-step transfer learning workflow: dataset preparation, ImageNet normalization (224×224), pre-trained model loading, binary head replacement, selective freezing, and fine-tuning.
- The separation of concerns design lets you swap between 30+ pre-trained architectures (MobileNetV2, ResNet-50, etc.) without changing the data pipeline code.
- Freezing the backbone initially (
trainable=Falseorrequires_grad=False) allows efficient training of the new classification head before optional full-model fine-tuning.
Frequently Asked Questions
What is transfer learning and why use pre-trained models for image classification?
Transfer learning allows you to use neural networks already trained on massive datasets (like ImageNet) and adapt them to your specific task. According to the AI-For-Beginners source code, this approach requires significantly less training data and computation than training from scratch, while achieving higher accuracy on tasks like the Cats-vs-Dogs binary classification example.
How do I choose between TensorFlow and PyTorch in this repository?
Both frameworks are fully supported with nearly identical APIs in tfcv.py and pytorchcv.py. The repository encourages comparing both ecosystems side-by-side. Choose TensorFlow if you prefer the Keras model.fit() workflow, or PyTorch if you want explicit control over the training loop via the cv.train() helper function.
Can I use a different dataset instead of Cats vs Dogs?
Yes. While load_cats_dogs_dataset is provided as the default example, you can modify the image paths in tfcv.py or create a custom torch.utils.data.Dataset for PyTorch. The utilities expect images organized in class folders and will apply the same 224×224 resizing and ImageNet normalization regardless of the specific dataset.
Why should I freeze the backbone layers during initial training?
Freezing the pre-trained backbone (setting trainable=False in Keras or requires_grad=False in PyTorch) prevents the ImageNet-trained weights from updating during the first training phase. This protects the learned feature extractors while allowing the new binary classification head to learn from your specific data, preventing overfitting when working with smaller datasets like Cats-vs-Dogs.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →