A deep learning CNN model for binary image classification using TensorFlow and Keras
Features β’ Installation β’ Usage β’ Results β’ Architecture
- Overview
- Features
- Model Architecture
- Installation
- Dataset Structure
- Usage
- Results
- Technical Details
- Contributing
- License
This project implements a Convolutional Neural Network (CNN) for classifying images of cats and dogs with high accuracy. The model leverages both Transfer Learning with MobileNetV2 and a custom CNN architecture, featuring advanced data augmentation techniques and robust training strategies.
- Dual Architecture Support: Transfer Learning (MobileNetV2) + Custom CNN
- Advanced Data Augmentation for improved generalization
- Smart Training Strategies: Early stopping, learning rate reduction
- Production Ready: Complete preprocessing and prediction pipeline
- Comprehensive Visualization of training metrics
| Feature | Description |
|---|---|
| π§ Transfer Learning | Pre-trained MobileNetV2 for superior performance |
| π Data Augmentation | Horizontal flip, zoom, width/height shifts |
| π Smart Callbacks | Early stopping and adaptive learning rate |
| π¨ Visualization | Training history plots and performance metrics |
| β‘ Fast Inference | Optimized for quick predictions |
| π‘οΈ Regularization | Dropout layers prevent overfitting |
MobileNetV2 (ImageNet weights, frozen)
β
GlobalAveragePooling2D
β
Dropout(0.3)
β
Dense(128, ReLU)
β
Dropout(0.5)
β
Dense(1, Sigmoid)
Input(160Γ160Γ3)
β
Conv2D(16) + BatchNorm + MaxPool + Dropout(0.25)
β
Conv2D(32) + BatchNorm + MaxPool + Dropout(0.25)
β
Conv2D(64) + BatchNorm + MaxPool + Dropout(0.25)
β
GlobalAveragePooling2D
β
Dense(32, ReLU) + Dropout(0.5)
β
Dense(1, Sigmoid)
- Python 3.8+
- CUDA-compatible GPU (recommended)
# Clone the repository
git clone https://github.com/Wayn-Git/CatvsDog.git
cd CatvsDog
# Install dependencies
pip install tensorflow matplotlib seaborn numpytensorflow>=2.8.0
matplotlib>=3.5.0
seaborn>=0.11.0
numpy>=1.21.0CatvsDog/
βββ Data/
β βββ train/
β β βββ cats/ # Training cat images
β β βββ dogs/ # Training dog images
β βββ test/
β βββ cats/ # Test cat images
β βββ dogs/ # Test dog images
βββ Model/
β βββ cat_dog_model.keras
βββ Notebook/
β βββ CatvsDog/
β βββ (Backup).ipynb
β βββ catDog(Col...).ipynb
βββ PythonScript/
β βββ catdog.py # Main training script
β βββ catDog.ipynb
β βββ README.md
β βββ requirements.txt
βββ README.md
# Set transfer learning mode
TRANSFER_LEARNING = True # Use MobileNetV2 (recommended)
# TRANSFER_LEARNING = False # Use custom CNN
# Run training
python PythonScript/catdog.pyfrom tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
import numpy as np
# Load trained model
model = load_model('Model/cat_dog_model.keras')
# Load and preprocess image
img_path = "your_image.jpg"
img = image.load_img(img_path, target_size=(160, 160))
img_array = image.img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
prediction = model.predict(img_array)
result = "Dog πΆ" if prediction[0][0] > 0.5 else "Cat π±"
print(f"Predicted: {result}")
print(f"Confidence: {prediction[0][0]:.4f}")| Architecture | Test Accuracy | Training Time | Model Size |
|---|---|---|---|
| Transfer Learning (MobileNetV2) | ~95%+ | ~15-20 epochs | ~9MB |
| Custom CNN | ~85-90% | ~25-30 epochs | ~3MB |
- Early Stopping: Prevents overfitting with patience=5
- Learning Rate Reduction: Adaptive LR with factor=0.2, patience=3
- Data Augmentation: Improves generalization significantly
- Batch Processing: Efficient training with batch_size=64
# Training augmentation
trainGen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=True,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
# Test preprocessing
testGen = ImageDataGenerator(rescale=1./255)| Parameter | Value |
|---|---|
| Input Size | 160Γ160Γ3 |
| Batch Size | 64 |
| Optimizer | Adam |
| Loss Function | Binary Crossentropy |
| Max Epochs | 30 |
| Early Stopping Patience | 5 |
| LR Reduction Factor | 0.2 |
- Robust Architecture: Both transfer learning and custom CNN options
- Smart Callbacks: Early stopping and learning rate scheduling
- Data Augmentation: Comprehensive image transformations
- Regularization: Strategic dropout placement
- Efficient Training: Optimized batch processing
The model automatically generates training visualizations:
- Accuracy Curves: Training vs Validation accuracy over epochs
- Loss Curves: Training vs Validation loss progression
- Performance Metrics: Detailed evaluation results
Contributions are welcome! Please feel free to:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
- Model architecture improvements
- Additional data augmentation techniques
- Web interface development
- Mobile deployment optimization
- Documentation enhancements
This project is licensed under the MIT License - see the LICENSE file for details.
- TensorFlow/Keras team for the excellent deep learning framework
- MobileNetV2 architecture for efficient transfer learning
- ImageNet dataset for pre-trained weights
- Open source community for inspiration and support
β If you found this project helpful, please give it a star! β
Made with β€οΈ and Deep Learning
Report Bug β’ Request Feature β’ Documentation