🐱🐢 Dogs vs Cats Classifier

A binary image classification model that distinguishes between cats and dogs using Support Vector Machine (SVM) combined with VGG16 transfer learning. The model is trained on two combined datasets and saved in modern .keras format.

Model Description

This model implements a hybrid approach combining classical machine learning with deep learning:

  • Feature Extraction: VGG16 (pre-trained on ImageNet) with Global Average Pooling β†’ 512 features
  • Dimensionality Reduction: PCA reduces to 256 components (~95% variance)
  • Classification: SVM with RBF kernel performs binary classification

Model Architecture

Input Image (224Γ—224Γ—3)
    ↓
VGG16 Feature Extractor (frozen)
    ↓
Global Average Pooling β†’ 512 features
    ↓
StandardScaler (normalization)
    ↓
PCA (512 β†’ 256 components)
    ↓
SVM (RBF kernel)
    ↓
Binary Output (Cat=0, Dog=1)

Model Files

  • cats-vs-dogs-components.keras (~58 MB): VGG16 + Global Average Pooling feature extractor
  • cats-vs-dogs-components.keras (~3-5 MB): PCA, StandardScaler, and SVM components stored in HDF5 format

Quick Start

Option 1: Use Pre-trained Model (2 minutes)

Install dependencies:

uv pip install tensorflow scikit-learn h5py huggingface-hub pillow

Run inference:

uv run python inference.py test/cat1.jpeg

Expected output:

🐱🐢 Dogs vs Cats Classifier - Inference (.keras format)
Image: test/cat1.jpeg

βœ“ Using local model files

Loading model components...
βœ“ Feature extractor loaded
βœ“ PCA, Scaler, and SVM loaded

Making prediction...

Prediction: Cat 🐱
Confidence: 92.34%

Option 2: Train Your Own Model (30-60 minutes)

Install dependencies:

uv pip install tensorflow scikit-learn h5py kagglehub pillow jupyter matplotlib seaborn tqdm

Start Jupyter Notebook:

jupyter notebook train_model.ipynb

Training process:

  1. Cell 1: Install packages
  2. Cell 2: Import libraries
  3. Cell 3: Download Kaggle dataset to ./kaggle_data/
  4. Cell 4: Configure training (combines Kaggle + local datasets)
  5. Cell 5: Load images from both datasets
  6. Cells 6-12: Train VGG16 β†’ PCA β†’ SVM pipeline
  7. Cell 13: Save as .keras format
  8. Cell 14: Display summary

Files generated:

  • cats-vs-dogs-components.keras - Feature extractor
  • cats-vs-dogs-components.keras - PCA/Scaler/SVM components

Test the model:

uv run python inference.py test/dog1.jpeg

Option 3: Upload to HuggingFace (5 minutes)

Create .env file:

HF_TOKEN_CD=hf_your_token_here

Get your token from: https://huggingface.co/settings/tokens

Run upload script:

uv run python upload_to_huggingface.py

View your model: Visit https://huggingface.co/YOUR_USERNAME/dogs-vs-cats-svm


Training Data

Datasets Used

1. Kaggle Dataset: dog-and-cat-classification-dataset

  • Auto-downloaded to ./kaggle_data/
  • 5,000 samples (2,500 cats, 2,500 dogs)

2. Local Dataset: dogs-vs-cats/train/

  • 5,000 samples (2,500 cats, 2,500 dogs)

Total Combined: 10,000 images from diverse sources

Configuration

Edit train_model.ipynb Cell 4 for different configurations:

Config Samples Time Expected Accuracy
Fast 2K (1K each) 5-10 min ~75%
Balanced 10K (5K each) 30-45 min ~87%
Maximum 25K (12.5K each) 60-90 min ~95%
N_SAMPLES_PER_DATASET = 5000  # Adjust
PCA_COMPONENTS = 256           # 128 (fast), 256 (balanced), 512 (max)

Performance Metrics

Validation Set Performance

Metric Score
Accuracy ~87-95%
Precision >0.85
Recall >0.85
F1-Score >0.85

Model Characteristics

  • Balanced Performance: Equal accuracy for cats and dogs
  • High Confidence: Most predictions >85% confidence
  • Robust: Trained on diverse datasets for better generalization
  • Fast Inference: <1 second per image on CPU

Project Structure

cats_dogs_svm/
β”œβ”€β”€ train_model.ipynb             # Training notebook (multi-dataset)
β”œβ”€β”€ inference.py                  # Inference script (.keras format)
β”œβ”€β”€ upload_to_huggingface.py      # HuggingFace upload script
β”‚
β”œβ”€β”€ cats-vs-dogs-components.keras            # VGG16 feature extractor (~58 MB)
β”œβ”€β”€ cats-vs-dogs-components.keras # PCA/Scaler/SVM (~3-5 MB)
β”‚
β”œβ”€β”€ kaggle_data/                  # Auto-downloaded Kaggle dataset
β”‚   └── dog-and-cat-classification-dataset/
β”‚       └── train/
β”‚
β”œβ”€β”€ dogs-vs-cats/                 # Local dataset
β”‚   └── train/
β”‚
β”œβ”€β”€ test/                         # Test images
β”‚   β”œβ”€β”€ cat1.jpeg
β”‚   β”œβ”€β”€ dog1.jpeg
β”‚   └── ...
β”‚
└── model_cache/                  # HuggingFace model cache

Training Procedure

Preprocessing Pipeline

  1. Download: Kaggle dataset to ./kaggle_data/
  2. Load: Combine images from both datasets
  3. Resize: All images to 224Γ—224 pixels
  4. Normalize: VGG16 preprocessing (ImageNet mean subtraction)
  5. Extract: VGG16 forward pass β†’ 512-d feature vectors
  6. Transform: PCA dimensionality reduction to 256 components
  7. Scale: StandardScaler for zero mean, unit variance

Model Training

  1. Train/Validation Split: 80/20 stratified split
  2. Hyperparameter Tuning: GridSearchCV with 3-fold CV
  3. Parameter Grid:
    • C: [1, 10, 100]
    • Gamma: ['scale', 0.001, 0.01]
    • Kernel: ['rbf']
  4. Optimization: Best parameters selected automatically
  5. Save: Models in .keras format (VGG16 + components)

Primary Use Cases

βœ… Binary classification of cat and dog images
βœ… Educational demonstration of transfer learning
βœ… Baseline model for image classification tasks
βœ… Feature extraction pipeline for similar datasets


Limitations

Known Limitations

  1. Binary Classification Only: Cannot distinguish breeds or other animals
  2. Image Quality Dependent: Performance degrades with low-quality/occluded images
  3. Dataset Biases: May inherit biases from ImageNet pre-training
  4. Computational Requirements: Requires TensorFlow for feature extraction

May Struggle With

  • Cartoons or artistic renderings
  • Animals in unusual poses or clothing
  • Mixed images containing both cats and dogs
  • Very young animals (kittens/puppies)
  • Rare or unusual breeds

Troubleshooting

Import Errors

uv pip install --upgrade tensorflow scikit-learn h5py

Out of Memory

Reduce samples in train_model.ipynb Cell 4:

N_SAMPLES_PER_DATASET = 2000  # Smaller dataset
BATCH_SIZE = 8                 # Smaller batches

Low Confidence

Train with more samples:

N_SAMPLES_PER_DATASET = 10000  # More data
PCA_COMPONENTS = 512           # Keep more features

Additional Information

Repository

License

This model is released under the MIT License.

Citation

@misc{dogs_cats_svm_vgg16_2025,
  author = {{Abdul Ahad}},
  title = {Dogs vs Cats Classification using SVM and VGG16 Transfer Learning},
  year = {2025},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/990aa/SCT_ML_3}}
}

Author: Abdul Ahad (@990aa)
Last Updated: November 2025
Model Format: Keras (.keras)

Downloads last month
24
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support