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.
- Project Overview
- Dataset Structure
- Requirements
- Installation
- Training the Model
- Testing on Single Images
- Running the Web Application
- Project Structure
- Acknowledgements
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.
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/trainunder folderscatsanddogs. - Images should be RGB format and of reasonable size.
- Dataset can be found and organized from Kaggle datasets.
- Python 3.7+
- PyTorch
- torchvision
- scikit-learn
- Flask (for web app)
- Pillow
- Other common Python libraries
Clone this repository:
git clone https://github.com/PritamTheCoder/Cats_vs_Dogs_CNN.git
cd Cats_vs_Dogs_CNNCreate a virtual environment and activate it:
python -m venv .venv
source .venv/bin/activate # Linux/Mac
.venv\Scripts\activate # WindowsInstall dependencies:
pip install -r requirements.txtRun the training script:
python train_cnn.pyThis 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.
To test a single image with the trained model:
python test_img.py path/to/your/image.jpgThis script loads the saved model, preprocesses the input image, runs inference, and prints the prediction with confidence.
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.pyOpen 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.
You can also try the live web application hosted at: [https://meoworwoof.onrender.com]
Upload an image of a cat or dog, and the model will classify it instantly. Below are some example screenshots from the web app:
Dog image correctly predicted as a dog.
Cat image correctly predicted as a cat.
.
βββ 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
- Uses PyTorch and torchvision libraries.
- Inspired by common Cats vs Dogs classification tutorials.
- Thanks to the Dogs vs Cats dataset on Kaggle.
