Skip to content

Repository files navigation

Typing SVG

Python TensorFlow Keras MobileNetV2 License


A complete deep learning pipeline — from raw images to fine-tuned predictions —
built with TensorFlow, MobileNetV2, and a GitHub-dark aesthetic throughout.


🗂️ Table of Contents


🔭 Overview

This project implements a binary image classifier (Cat 🐱 vs Dog 🐶) using transfer learning on top of Google's MobileNetV2 pretrained on ImageNet.

The notebook walks through two training phases:

Phase Strategy Learning Rate
Feature Extraction MobileNetV2 frozen, only custom head trained 1e-3
Fine-Tuning Top 30 layers of MobileNetV2 unfrozen 1e-5

The result is a lightweight, highly accurate classifier that trains in minutes on a free GPU (Google Colab).


🎬 Demo

Sample Grid Augmentation
Sample images from the dataset Augmented examples
Training Curves Confusion Matrix
Feature extraction curves Confusion matrix

📁 Project Structure

📦 animal-classifier/
├── 📓 Animal_Classifier_MobileNetV2.ipynb   ← Main notebook (10 tasks)
├── 🖼️  sample_grid.png                       ← Dataset preview
├── 🔄  augmentation.png                      ← Augmentation examples
├── 📊  feature_extraction_model_curves.png   ← Phase 1 training curves
├── 📊  fine-tuned_model_curves.png           ← Phase 2 training curves
├── 🔲  confusion_matrix.png                  ← Evaluation heatmap
├── 🎯  predictions.png                       ← Test image predictions
└── 📄  README.md

🔄 Pipeline

Raw Images (Cats vs Dogs — 25,000 images)
        │
        ▼
┌──────────────────────────────────────┐
│   Preprocessing & Augmentation       │
│   • Resize → 224×224                 │
│   • Random Flip / Rotate / Zoom      │
│   • MobileNetV2 normalization        │
│   • tf.data  cache → shuffle → batch │
└──────────────────────────────────────┘
        │
        ▼
┌──────────────────────────────────────┐
│   MobileNetV2  (ImageNet weights)    │  ← Frozen
│   + GlobalAveragePooling2D           │
│   + Dense(256, relu)                 │
│   + Dropout(0.3)                     │
│   + Dense(1, sigmoid)                │  ← Trainable
└──────────────────────────────────────┘
        │
        ▼
  Phase 1: Feature Extraction  (lr = 1e-3)
        │
        ▼
  Phase 2: Fine-Tuning          (lr = 1e-5, top 30 layers unfrozen)
        │
        ▼
  Evaluation → Confusion Matrix + Classification Report
        │
        ▼
  Visualised Predictions on Test Set

📊 Results

Metric Feature Extraction Fine-Tuned
Train Accuracy ~87% ~93%
Val Accuracy ~85% ~90%
Test Accuracy TBD (run notebook) TBD (run notebook)

Results will vary slightly due to random seed and hardware differences.


🚀 Setup & Run

▶️ Run on Google Colab (Recommended — free GPU)

Open In Colab

Replace YOUR_USERNAME with your GitHub username after uploading.

💻 Run Locally

# 1. Clone the repo
git clone https://github.com/YOUR_USERNAME/animal-classifier.git
cd animal-classifier

# 2. Create a virtual environment
python -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate

# 3. Install dependencies
pip install tensorflow tensorflow-datasets seaborn scikit-learn matplotlib jupyter

# 4. Launch the notebook
jupyter notebook Animal_Classifier_MobileNetV2.ipynb

📦 Requirements

tensorflow>=2.10
tensorflow-datasets>=4.9
matplotlib>=3.6
seaborn>=0.12
scikit-learn>=1.2
numpy>=1.23

🛠️ Tech Stack

Tool Purpose
TensorFlow 2.x Deep learning framework
Keras High-level model API
MobileNetV2 Pretrained backbone (ImageNet)
TensorFlow Datasets cats_vs_dogs dataset (25k images)
Matplotlib Training curves & image grids
Seaborn Confusion matrix heatmap
scikit-learn Classification report & metrics

🧠 How It Works

1. Transfer Learning

Instead of training from scratch, we load MobileNetV2 pretrained on 1.2M ImageNet images. Its convolutional layers already know how to detect edges, textures, and shapes — all transferable to our cat/dog task.

2. Feature Extraction (Phase 1)

The entire MobileNetV2 base is frozen (weights locked). Only the new classification head — Dense(256) → Dropout → Dense(1, sigmoid) — learns. This is fast and avoids destroying the pretrained features.

3. Fine-Tuning (Phase 2)

After Phase 1 converges, the top 30 layers of MobileNetV2 are unfrozen and retrained at a very small learning rate (1e-5). This lets the higher-level features adapt specifically to cats and dogs, squeezing out extra accuracy.

4. Data Augmentation

Random horizontal flips, rotations (±15°), and zooms (±10%) are applied on-the-fly during training, effectively multiplying the dataset size and improving generalisation.


📄 License

This project is licensed under the MIT License — feel free to use, modify, and distribute.


Made with ❤️ and TensorFlow

⭐ Star this repo if you found it useful!

About

Cat vs Dog vs human image classifier using MobileNetV2 transfer learning | TensorFlow

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages