This project implements a Convolutional Neural Network (CNN) using PyTorch to classify images of cats and dogs. The model learns visual patterns from thousands of labeled images and predicts whether a given image contains a cat or a dog. A simple GUI built with Tkinter allows users to upload an image and receive real-time predictions.
The project uses a Convolutional Neural Network (CNN) implemented in PyTorch. The architecture contains:
- Convolution layers for feature extraction
- ReLU activation
- Max pooling layers
- Fully connected layers for classification
The model outputs two classes:
- Cat
- Dog
Training time on different systems:
Mac Mini M4 Pro
- ~30 minutes to train the dataset
Windows (Intel i5)
- ~2 hours training time
Dataset Size:
~800 MB
Number of Images:
- Dogs: ~12,000 images
- Cats: ~12,000 images
Total dataset size: ~24,000 images
Cats-Vs-Dogs-Classifier/
│
├── data/
│ ├── train/
│ │ ├── Cat/
│ │ └── Dog/
│
├── models/
│ └── cnn_model.py
│
├── images/
│ ├── gui_demo.png
│ ├── dog.jpg
│ └── cat.jpg
|__ clean_dataset.py
├── train.py
├── predict.py
├── predict_gui.py
├── requirements.txt
└── README.md
- Images are loaded from the dataset directory.
- Each image is resized and converted into tensors.
- The CNN extracts important visual features using convolution layers.
- Fully connected layers classify the extracted features.
- The model outputs the predicted label Cat or Dog.
Create virtual environment:
python3 -m venv .venv
Activate environment:
source .venv/bin/activate
Create virtual environment:
python -m venv .venv
Activate environment:
.venv\Scripts\activate
pip install -r requirements.txt
In this model , the dataset is download from Microsoft Kaggle Cats and Dogs Dataset
Run the training script:
python train.py
After training completes, the model will be saved as:
cat_dog_cnn.pth
As the dataset contains some corrupt/broken image so I have created a script to Delete these images.
run python clean_dataset.py
You can run prediction using the command line.
Edit the image path inside:
predict.py
Change the image path:
img = Image.open("path_to_image.jpg")
Then run:
python predict.py
Run the graphical interface:
python predict_gui.py
Steps:
- Click Upload Image
- Select a cat or dog image
- Click Predict
- The model will display the prediction
Main libraries used:
torch
torchvision
pillow
numpy
tkinter
Developed as a deep learning project using PyTorch for learning computer vision concepts and CNN-based image classification.