Skip to content

notoctavio/Cats-and-Dogs-Image-Classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฑ๐Ÿถ CNN Image Classifier: Cats vs Dogs

Python TensorFlow Keras License

A powerful Convolutional Neural Network that distinguishes between cats and dogs with 90%+ training accuracy!

Features โ€ข Architecture โ€ข Installation โ€ข Dataset โ€ข Usage โ€ข Results


๐ŸŽฏ Project Overview

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.

๐ŸŒŸ Key Highlights

  • 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

๐Ÿš€ Features

๐Ÿ”ง Technical Features

  • 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

๐Ÿ“Š Model Capabilities

  • Binary classification (Cat vs Dog)
  • 64x64 pixel image processing
  • RGB color image support
  • Robust feature extraction

๐Ÿ—๏ธ Architecture

Model Structure

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)

๐Ÿ”ฌ Technical Specifications

  • Input Shape: (64, 64, 3) - RGB images
  • Activation Functions: ReLU (hidden layers), Sigmoid (output)
  • Optimizer: Adam
  • Loss Function: Binary Crossentropy
  • Metrics: Accuracy

๐Ÿ“ฆ Installation

Prerequisites

Python 3.10+
TensorFlow 2.16.2
NumPy
Pandas

Setup Instructions

  1. Clone the Repository

    git clone https://github.com/yourusername/cnn-cats-dogs-classifier.git
    cd cnn-cats-dogs-classifier
  2. Create Virtual Environment

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install Dependencies

    pip install tensorflow numpy pandas matplotlib

๐Ÿ“ Dataset

Download the Dataset

The cats and dogs dataset is available for download:

๐Ÿ”— Download Dataset

Dataset Structure

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

Dataset Information

  • 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

๐ŸŽฎ Usage

Training the Model

# Import required libraries
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Initialize and train the CNN
python CNN.ipynb

Making Predictions

# 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}")

๐Ÿ“ˆ Results

Training Performance

  • Final Training Accuracy: ~90%
  • Validation Accuracy: ~80%
  • Training Epochs: 25
  • Training Time: ~19 minutes

Model Metrics

Metric Value
Training Accuracy 90.3%
Validation Accuracy 77.6%
Training Loss 0.23
Validation Loss 0.57

๐Ÿ“Š Training Progress

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%)

๐Ÿ” Data Preprocessing

Training Data Augmentation

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 Data Processing

test_datagen = ImageDataGenerator(rescale=1./255)

๐Ÿ› ๏ธ Code Structure

๐Ÿ“ 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

๐Ÿง  Key Learning Concepts

Convolutional Layers

  • Feature Detection: Identifies edges, patterns, and textures
  • Parameter Sharing: Reduces overfitting and computational cost
  • Translation Invariance: Recognizes features regardless of position

Pooling Layers

  • Dimensionality Reduction: Reduces computational complexity
  • Feature Robustness: Makes model less sensitive to small translations
  • Max Pooling: Retains strongest activations from each region

Data Augmentation

  • Prevents Overfitting: Increases effective dataset size
  • Improves Generalization: Model learns from varied perspectives
  • Real-world Robustness: Handles different lighting and orientations

๐ŸŽฏ Future Improvements

  • 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

๐Ÿค Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

  • 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

๐ŸŒŸ If you found this project helpful, please give it a star! โญ

Built with โค๏ธ and lots of โ˜•

โฌ† Back to Top

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors