Individual Project 01
Machine Learning with Advanced Python
Infotech College of Business & IT
An advanced deep learning project that classifies animal images using Convolutional Neural Networks (CNNs) and transfer learning with PyTorch.
This repository demonstrates the full pipeline of building an end-to-end image classification system; from dataset preparation, transformations, model training, evaluation, and visualization of predictions.
- ✅ End-to-End Workflow: Data preprocessing, training, evaluation, and deployment-ready pipeline.
- ✅ Transfer Learning: Fine-tuning pre-trained CNNs for improved accuracy.
- ✅ GPU Acceleration: Optimized training with CUDA/cuDNN when available.
- ✅ Visualization: Model predictions with sample outputs for interpretability.
- ✅ Custom Dataset Support: Easily adaptable to new image datasets.
Project_01_Animal_Image_Classifier_Infotech/
├── 1_data/
│ ├── test/ # Images for final model testing
│ ├── train/ # Images for training (e.g., cats, dogs, snakes subfolders)
│ │ ├── cats/
│ │ ├── dogs/
│ │ └── snakes/
│ └── val/ # Images for validation during training (e.g., cats, dogs, snakes subfolders)
│ ├── cats/
│ ├── dogs/
│ └── snakes/
|
├── 2_models/ # Stores trained PyTorch model files (.pth)
│ ├── best_model.pth # Model with best validation accuracy
│ └── final_model.pth # Model after the last training epoch
|
├── 3_notebooks/ # Contains the Jupyter Notebook
│ └── your_project_notebook.ipynb
|
├── 4_results/ # Stores evaluation reports and confusion matrices
│ ├── val_classification_reports.txt
│ └── val_confusion_matrices.png
|
|── 5_outputs/ # Stores visual outputs
| |── Predictions/
| └── Data_Load_Validation.png
|
|── ReadMe.md # Official documentation
|── .gitignore
|── Cover_image.png
└── LICENSE.md # MIT License
- Language: Python
- Deep Learning Framework: PyTorch
- Libraries: Torchvision, Matplotlib, NumPy, PIL
- Environment: Jupyter Notebook
- IDE: Visual Studio Code
-
Clone the repository
git clone https://github.com/ThilinaPerera-DataAnalytics/ML_Projects_Animal_Images_Classifier.git
-
Install dependencies
Ensure you have all the required libraries by running: pip install -r requirements.txt (torch, numpy, torchvision, matplotlib, time, os, PIL, tempfile, scikitlearn, seaborn, pandas)
-
Prepare the Dataset
1. Download the dataset from the Kaggle link provided below and place the `train`, `val`, and `test` subdirectories into the `1_data` folder as per the folder structure. 2. Manual Splitting and Reorganization a. Train: 80% = 800 b. Validate: 20% = 200 c. Test: 3 (one from each)
-
Run the Jupyter Notebook
Open the `project_01_animal_image-classification.ipynb` located in the 3_notebooks/ directory using Jupyter Notebook or JupyterLab and execute the cells sequentially.
The notebook will
* Load and preprocess images.
* Initialize and train two models: a fine-tuned ResNet-18 model and a ResNet-18 feature extractor.
* Save the trained models in the `2_models` directory.
* Visualize model predictions.
* Generate and save classification reports and confusion matrices.
* Display training and validation graphs.
- Data Transformations – Normalize and augment datasets for robust training.
- Dataset Loading – Structured image dataset using
1_data/
folder. - Model Architecture – Pre-trained CNN models fine-tuned for classification.
- Training Loop – Custom training function with optimizer & learning rate scheduler.
- Evaluation – Accuracy and loss tracking per epoch.
- Visualization – Display predictions on validation and test images.
The project evaluates the performance of both the fine-tuned CNN model (best_model.pth
) and the transfer learning feature extraction model (final_model.pth
) on the validation dataset.
The evaluation includes:
- Loss and Accuracy per Epoch: Printed during the training process for both training and validation phases.
- Classification Report: Provides precision, recall, f1-score, and support for each class.
- Confusion Matrix: Visualizes the number of correct and incorrect predictions for each class.
Input Image | Predicted Class |
---|---|
🐶 Dog | ✅ Dog |
🐱 Cat | ✅ Cat |
🐍 Snake | ✅ Snake |
Classification Report:
precision recall f1-score support
cats 0.9845 0.9500 0.9669 200
dogs 0.9563 0.9850 0.9704 200
snakes 0.9950 1.0000 0.9975 200
accuracy 0.9783 600
macro avg 0.9786 0.9783 0.9783 600
weighted avg 0.9786 0.9783 0.9783 600
Confusion Matrix
Classification Report:
precision recall f1-score support
cats 0.9704 0.9850 0.9777 200
dogs 0.9848 0.9750 0.9799 200
snakes 1.0000 0.9950 0.9975 200
accuracy 0.9850 600
macro avg 0.9851 0.9850 0.9850 600
weighted avg 0.9851 0.9850 0.9850 600
Confusion Matrix
- 🔹 Experiment with ResNet, VGG, and EfficientNet backbones.
- 🔹 Deploy the trained model with Flask/FastAPI for inference.
- 🔹 Improve performance with hyperparameter tuning & data augmentation.
- 🔹 Add Grad-CAM visualization for interpretability.
- Lecturer Mr. Akshan Bandara for guidance & Infotech College staff for resources.
- PyTorch – Deep Learning Framework
- Torchvision – Image Utilities
- Matplotlib – Visualization
- Kaggle: Animal Image Classification Dataset - Dataset Source - Train and test
- Test images - Google images
Contributions are welcome! Please fork the repo and submit a pull request with improvements.
Thilina Perera/ Data with TP
📌 Data Science/ Data Analytics
📌 Machine Learning, Deep Learning, LLM/LMM, NLP, and Automated Data Pipelines Explorer
🔍 LinkedIn | 🔍 GitHub | 🔍 YouTube
This project is licensed under the MIT License – free to use and extend.
✨ If you like this project, don’t forget to star ⭐ the repo!