π±πΆ 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
.kerasformat - Cell 14: Display summary
Files generated:
cats-vs-dogs-components.keras- Feature extractorcats-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
- 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:
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
- GitHub: https://github.com/990aa/SCT_ML_3
- HuggingFace: https://huggingface.co/a-01a/dogs-vs-cats-svm
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