Skip to content

CatVsDog CNN β€” The ultimate fur-fight classifier! Upload a pic, and see if it’s a meow or a woof in seconds! Powered by PyTorch, Flask, and questionable caffeine levels. Test it live πŸ‘‡

Notifications You must be signed in to change notification settings

PritamTheCoder/Cats_vs_Dogs_CNN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cats vs Dogs CNN Classifier

This project implements a Convolutional Neural Network (CNN) to classify images of cats and dogs. It includes data loading, model training, evaluation, and a simple Flask web application for inference.


Table of Contents


Project Overview

This project builds a CNN to distinguish between cat and dog images. It uses PyTorch for deep learning and torchvision for dataset management. The model is trained on images organized in folders, evaluated on a test split, and saved for inference.

A Flask-based web app allows users to upload images and get real-time predictions.


Dataset Structure

Your image dataset should be organized as follows:

data/
└── train/
    β”œβ”€β”€ cats/
    β”‚   β”œβ”€β”€ cat1.jpg
    β”‚   β”œβ”€β”€ cat2.jpg
    β”‚   └── ...
    └── dogs/
        β”œβ”€β”€ dog1.jpg
        β”œβ”€β”€ dog2.jpg
        └── ...
  • Place all training images in data/train under folders cats and dogs.
  • Images should be RGB format and of reasonable size.
  • Dataset can be found and organized from Kaggle datasets.

Requirements

  • Python 3.7+
  • PyTorch
  • torchvision
  • scikit-learn
  • Flask (for web app)
  • Pillow
  • Other common Python libraries

Installation

Clone this repository:

git clone https://github.com/PritamTheCoder/Cats_vs_Dogs_CNN.git
cd Cats_vs_Dogs_CNN

Create a virtual environment and activate it:

python -m venv .venv
source .venv/bin/activate      # Linux/Mac
.venv\Scripts\activate         # Windows

Install dependencies:

pip install -r requirements.txt

Training the Model

Run the training script:

python train_cnn.py

This will:

  • Load and preprocess the dataset from data/train.
  • Train the CNN model for 8 epochs (modifiable in the script: train_cnn.py).
  • Evaluate accuracy on a held-out test set.
  • Save the trained model as cat_v_dog_cnn.pth.

Tips:

  • Ensure your dataset directory exists and is correctly structured.
  • Use GPU if available for faster training.

Testing on Single Images

To test a single image with the trained model:

python test_img.py path/to/your/image.jpg

This script loads the saved model, preprocesses the input image, runs inference, and prints the prediction with confidence.


Running the Web Application

The web app allows you to upload an image and get a prediction through a user-friendly interface.

Run the Flask app locally:

python app.py

Open your browser and navigate to:

http://127.0.0.1:5000

Upload an image of a cat or dog, and see the prediction displayed instantly.


Hosted Demo

You can also try the live web application hosted at: [https://meoworwoof.onrender.com]


Example Predictions

Upload an image of a cat or dog, and the model will classify it instantly. Below are some example screenshots from the web app:

Entry_point Enrty point of webapp.

Dog Test Dog image correctly predicted as a dog.

Cat Test Cat image correctly predicted as a cat.


Project Structure

.
β”œβ”€β”€ data/
β”‚   └── train/
β”‚       β”œβ”€β”€ cats/
β”‚       └── dogs/
β”‚
β”œβ”€β”€ webapp/
β”‚   β”œβ”€β”€ templates/
β”‚   β”‚   └── index.html           # HTML for app.py Flask web application
β”‚   β”‚
β”‚   β”œβ”€β”€ static/
β”‚   β”‚   └── style.css            # CSS for app.py Flask web application
β”‚   β”‚
β”‚   β”œβ”€β”€ cat_v_dog_cnn.pth        # Pretrained model weights (example, for quick testing)
β”‚   β”œβ”€β”€ app.py                   # Flask web application
β”‚   β”œβ”€β”€ network.py               # Minimal model architecture just for inference
β”‚
β”œβ”€β”€ train_cnn.py                 # Training script (uses root network.py)
β”œβ”€β”€ test_img.py                   # Single image inference script
β”œβ”€β”€ dataset_download.py           # Script to download & organize dataset from URL
β”œβ”€β”€ dataset_loader.py             # Custom dataset loader (optional)
β”œβ”€β”€ network.py                    # Full model architecture for training
β”œβ”€β”€ requirements.txt              # Virtual environment requirements
β”œβ”€β”€ .gitignore
└── README.md


Acknowledgements

  • Uses PyTorch and torchvision libraries.
  • Inspired by common Cats vs Dogs classification tutorials.
  • Thanks to the Dogs vs Cats dataset on Kaggle.

About

CatVsDog CNN β€” The ultimate fur-fight classifier! Upload a pic, and see if it’s a meow or a woof in seconds! Powered by PyTorch, Flask, and questionable caffeine levels. Test it live πŸ‘‡

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published