A powerful Convolutional Neural Network that distinguishes between cats and dogs with 90%+ training accuracy!
Features โข Architecture โข Installation โข Dataset โข Usage โข Results
This project implements a Convolutional Neural Network (CNN) using TensorFlow/Keras to classify images of cats and dogs. The model achieves impressive performance through careful architecture design and data augmentation techniques.
- 90%+ Training Accuracy achieved in 25 epochs
- Advanced Data Augmentation to prevent overfitting
- Real-time Prediction capability on new images
- Clean, Well-documented Code with detailed explanations
- Deep Learning Architecture: Multi-layer CNN with optimized parameters
- Data Augmentation: Rotation, shearing, zooming, and flipping
- Batch Processing: Efficient training with batch size optimization
- Real-time Inference: Single image prediction capability
- Binary classification (Cat vs Dog)
- 64x64 pixel image processing
- RGB color image support
- Robust feature extraction
Input Layer (64x64x3)
โ
Convolutional Layer (32 filters, 3x3, ReLU)
โ
Max Pooling (2x2, stride=2)
โ
Convolutional Layer (32 filters, 3x3, ReLU)
โ
Max Pooling (2x2, stride=2)
โ
Flatten Layer
โ
Dense Layer (128 neurons, ReLU)
โ
Output Layer (1 neuron, Sigmoid)
- Input Shape: (64, 64, 3) - RGB images
- Activation Functions: ReLU (hidden layers), Sigmoid (output)
- Optimizer: Adam
- Loss Function: Binary Crossentropy
- Metrics: Accuracy
Python 3.10+
TensorFlow 2.16.2
NumPy
Pandas-
Clone the Repository
git clone https://github.com/yourusername/cnn-cats-dogs-classifier.git cd cnn-cats-dogs-classifier -
Create Virtual Environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies
pip install tensorflow numpy pandas matplotlib
The cats and dogs dataset is available for download:
๐ Download Dataset
After downloading and extracting the dataset, organize it as follows:
dataset/
โโโ training_set/
โ โโโ cats/ # Training images of cats
โ โโโ dogs/ # Training images of dogs
โโโ test_set/
โโโ cats/ # Test images of cats
โโโ dogs/ # Test images of dogs
- Training Set: 8,000 images (4,000 cats + 4,000 dogs)
- Test Set: 2,000 images (1,000 cats + 1,000 dogs)
- Image Format: JPEG
- Image Size: Variable (resized to 64x64 during preprocessing)
- Total Size: ~540 MB
# Import required libraries
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Initialize and train the CNN
python CNN.ipynb# Load and predict on a single image
from tensorflow.keras.preprocessing import image
import numpy as np
# Load your trained model
model = tf.keras.models.load_model('cnn_model.h5')
# Make prediction
test_image = image.load_img('path/to/image.jpg', target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
prediction = 'dog' if result[0][0] == 1 else 'cat'
print(f"Prediction: {prediction}")- Final Training Accuracy: ~90%
- Validation Accuracy: ~80%
- Training Epochs: 25
- Training Time: ~19 minutes
| Metric | Value |
|---|---|
| Training Accuracy | 90.3% |
| Validation Accuracy | 77.6% |
| Training Loss | 0.23 |
| Validation Loss | 0.57 |
The model shows steady improvement over 25 epochs with clear learning progression:
- Early Epochs (1-5): Rapid initial learning (53% โ 73%)
- Mid Training (6-15): Steady improvement (73% โ 84%)
- Late Training (16-25): Fine-tuning and optimization (84% โ 90%)
train_datagen = ImageDataGenerator(
rescale=1./255, # Normalize pixels to [0,1]
shear_range=0.2, # Shear transformation
zoom_range=0.2, # Zoom transformation
horizontal_flip=True # Random horizontal flip
)test_datagen = ImageDataGenerator(rescale=1./255)๐ CNN-Cats-Dogs-Classifier/
โโโ ๐ CNN.ipynb # Main Jupyter notebook
โโโ ๐ README.md # This file
โโโ ๐ dataset/ # Dataset folder
โ โโโ ๐ training_set/ # Training images
โ โโโ ๐ test_set/ # Test images
โโโ ๐ requirements.txt # Dependencies
- Feature Detection: Identifies edges, patterns, and textures
- Parameter Sharing: Reduces overfitting and computational cost
- Translation Invariance: Recognizes features regardless of position
- Dimensionality Reduction: Reduces computational complexity
- Feature Robustness: Makes model less sensitive to small translations
- Max Pooling: Retains strongest activations from each region
- Prevents Overfitting: Increases effective dataset size
- Improves Generalization: Model learns from varied perspectives
- Real-world Robustness: Handles different lighting and orientations
- Transfer Learning: Implement pre-trained models (VGG16, ResNet)
- Model Ensemble: Combine multiple models for better accuracy
- Advanced Augmentation: Add color jittering and cutout
- Mobile Deployment: Convert to TensorFlow Lite
- Web Interface: Create Flask/Streamlit app for easy use
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Machine Learning A-Z Course for the comprehensive deep learning curriculum
- TensorFlow Team for the amazing deep learning framework
- Kaggle for providing excellent datasets for machine learning projects
Built with โค๏ธ and lots of โ