🐱🐶 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:
Cell 1: Install packages
Cell 2: Import libraries
Cell 3: Download Kaggle dataset to ./kaggle_data/
Cell 4: Configure training (combines Kaggle + local datasets)
Cell 5: Load images from both datasets
Cells 6-12: Train VGG16 → PCA → SVM pipeline
Cell 13: Save as .keras format
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
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%
1 N_SAMPLES_PER_DATASET = 5000 # Adjust
2 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
Download : Kaggle dataset to ./kaggle_data/
Load : Combine images from both datasets
Resize : All images to 224×224 pixels
Normalize : VGG16 preprocessing (ImageNet mean subtraction)
Extract : VGG16 forward pass → 512-d feature vectors
Transform : PCA dimensionality reduction to 256 components
Scale : StandardScaler for zero mean, unit variance
Model Training
Train/Validation Split : 80/20 stratified split
Hyperparameter Tuning : GridSearchCV with 3-fold CV
Parameter Grid :
C: [1, 10, 100]
Gamma: ['scale', 0.001, 0.01]
Kernel: ['rbf']
Optimization : Best parameters selected automatically
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
Binary Classification Only : Cannot distinguish breeds or other animals
Image Quality Dependent : Performance degrades with low-quality/occluded images
Dataset Biases : May inherit biases from ImageNet pre-training
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:
1 N_SAMPLES_PER_DATASET = 2000 # Smaller dataset
2 BATCH_SIZE = 8 # Smaller batches
Low Confidence
Train with more samples:
1 N_SAMPLES_PER_DATASET = 10000 # More data
2 PCA_COMPONENTS = 512 # Keep more features
Additional Information
Repository
License
This model is released under the MIT License.
Citation
1 @misc{dogs_cats_svm_vgg16_2025,
2 author = {{Abdul Ahad}},
3 title = {Dogs vs Cats Classification using SVM and VGG16 Transfer Learning},
4 year = {2025},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 howpublished = {\url{https://github.com/990aa/SCT_ML_3}}
8 }
Author : Abdul Ahad (@990aa)
Last Updated : November 2025
Model Format : Keras (.keras)