Pretrained models and transfer learning - iffatAGheyas/computer-vision-handbook GitHub Wiki

๐Ÿ”„ Pretrained Models & Transfer Learning

๐Ÿง  What Is a Pretrained Model?

A pretrained model is a neural network that has already been trained on a large, generic dataset (e.g., ImageNet or COCO). It has learned:

  • What edges, shapes, and textures look like
  • What common objects (e.g., person, dog, car) look like

You can reuse this knowledge for your own dataset, even with very little training data.


๐Ÿ”„ What Is Transfer Learning?

Transfer learning means:

Start with a pretrained model โ†’ fine-tune it for your own task.

You donโ€™t retrain from scratch. Instead:

  1. Keep the pretrained layers (theyโ€™re already useful!)
  2. Replace the last few layers (e.g., the classifier head) with your custom task
  3. Fine-tune only the final layers, or optionally the entire model

๐Ÿ’ก Why Use Transfer Learning?

Benefit Description
๐Ÿฃ Less data needed Can train with a small dataset (100s, not 1000s)
โฑ๏ธ Faster training Training takes hours, not days
๐ŸŽฏ High accuracy Leverages powerful pretrained features
๐Ÿ’ฐ Low cost Great for projects without massive compute

๐Ÿ› ๏ธ Code: Transfer Learning with TensorFlow/Keras (MobileNetV2)

Hereโ€™s a complete end-to-end example to classify Baby vs Doll.
Train on 40 images (trainingset/ with subfolders Baby/ and Doll/), then evaluate on 10 test images (testset/).

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
from tensorflow.keras.models import Model
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os

# 1) Directories
train_dir = 'trainingset'  # contains subfolders 'Baby' and 'Doll'
test_dir  = 'testset'      # contains subfolders 'Baby' and 'Doll'

# 2) Hyperparameters
IMG_SIZE   = (224, 224)
BATCH_SIZE = 8
EPOCHS     = 10
SEED       = 42
THRESHOLD  = 0.3  # for test-time filtering

# 3) Data generators
datagen = ImageDataGenerator(
    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,
    horizontal_flip=True,
    rotation_range=20,
    validation_split=0.2
)
train_gen = datagen.flow_from_directory(
    train_dir,
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='categorical',
    subset='training',
    seed=SEED
)
val_gen = datagen.flow_from_directory(
    train_dir,
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='categorical',
    subset='validation',
    seed=SEED
)

# 4) Build model
base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(*IMG_SIZE, 3))
base.trainable = False

x = base.output
x = GlobalAveragePooling2D()(x)
output = Dense(2, activation='softmax')(x)
model = Model(base.input, output)

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

# 5) Train
model.fit(
    train_gen,
    epochs=EPOCHS,
    validation_data=val_gen
)

# 6) Prepare test generator (no shuffle!)
test_datagen = ImageDataGenerator(
    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input
)
test_gen = test_datagen.flow_from_directory(
    test_dir,
    target_size=IMG_SIZE,
    batch_size=1,
    class_mode='categorical',
    shuffle=False
)

# 7) Predict on test set
preds        = model.predict(test_gen, steps=test_gen.samples)
pred_classes = np.argmax(preds, axis=1)
true_classes = test_gen.classes  # 0 = Baby, 1 = Doll
idx2label    = {v:k for k,v in test_gen.class_indices.items()}

# 8) Display results
fig, axes = plt.subplots(2, 5, figsize=(15, 6))
axes = axes.flatten()

for i, ax in enumerate(axes):
    if i >= test_gen.samples: break
    img_path = test_gen.filepaths[i]
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    actual    = idx2label[true_classes[i]]
    predicted = idx2label[pred_classes[i]]
    ax.imshow(img)
    ax.set_title(f"Actual: {actual}\nPredicted: {predicted}")
    ax.axis('off')

plt.tight_layout()
plt.show()

๐Ÿ“ŠSample Output

image

๐Ÿ”ง How It Works

  • Data setup
    flow_from_directory reads Baby/ and Doll/ subfolders automatically as labels.

  • Transfer learning
    Freeze the MobileNetV2 base and add a global pooling layer followed by a dense classification head.

  • Training
    Use an 80/20 train/validation split on 40 images and train for 10 epochs (adjust as needed).

  • Testing
    Load 10 test images (5 Baby + 5 Doll), run predictions, and display each image with Actual vs Predicted labels.


โœ… Summary Table

Concept Description
Pretrained Model Already trained on large datasets
Transfer Learning Fine-tuning the model for a new task
Benefit Fast, accurate, low data requirement
Frameworks PyTorch, TensorFlow, Ultralytics

๐Ÿ•’ When to Use Pretrained Models?

Scenario Use Pretrained + Transfer Learning?
Small dataset โœ… Absolutely
Want fast results โœ… Yes
Building mobile or embedded models โœ… Especially helpful
Domain very different (e.g. X-rays) โŒ May need more fine-tuning or training from scratch