This project aims to classify images of butterflies and moths into 10 different species using a custom-built Convolutional Neural Network (CNN) trained from scratch using PyTorch. The project also features an interactive UI using Gradio where users can upload an image and get real-time predictions.
This repository includes the saved model weights:
butterfly_model.pth
You can reuse the trained model without retraining by loading it as follows:
model = SimpleCNN(num_classes=10) model.load_state_dict(torch.load("butterfly_model.pth", map_location="cpu")) model.eval()
- Python, PyTorch
- Torchvision
- Gradio (UI)
- Matplotlib & Sklearn (metrics)
- Google Colab (CPU environment)
The dataset was manually curated from a 100-class butterfly/moth dataset. We selected 10 unique species and structured the dataset as follows: my_10_class_dataset/ ├── TRAIN_BALANCED/ # Balanced training set ├── VALID/ # Validation set (5 images per class) └── TEST/ # Test set (5 images per class)
- Image size:
224 x 224 x 3
(RGB) - Total classes: 10
- Train samples: 1210
- Test/Validation samples: 50 each
- BANDED TIGER MOTH
- CHALK HILL BLUE
- EMPEROR GUM MOTH
- GLITTERING SAPPHIRE
- GREEN HAIRSTREAK
- MOURNING CLOAK
- POPINJAY
- PURPLISH COPPER
- RED CRACKER
- ROSY MAPLE MOTH
The original training dataset had imbalanced classes. To ensure fairness, we:
- Calculated the minimum class count
- Performed random undersampling of all classes to match the smallest one
- Stored the balanced data in a new folder:
TRAIN_BALANCED/
Applied using torchvision.transforms
:
Resize(224, 224)
to normalize image sizeRandomHorizontalFlip()
&RandomRotation(10)
for augmentationToTensor()
to convert image to PyTorch tensorNormalize([0.5], [0.5])
for input scaling
Built from scratch using PyTorch:
- 3 Convolutional blocks with ReLU, BatchNorm, and MaxPooling
Flatten → Dropout → Linear (128) → ReLU → Linear (10 classes)
- Regularization using Dropout (0.5) and BatchNorm
- Loss:
CrossEntropyLoss
- Optimizer:
Adam
- Batch size:
16
, Epochs:10
, Device:CPU
- Evaluation metrics on the test set:
- Accuracy: 92%
- Precision, Recall, F1-score per class using
classification_report()
- Confusion matrix (visualized using matplotlib)
After training:
- Saved model using
torch.save(model.state_dict(), 'butterfly_model.pth')
- Reloaded model for inference in Colab
Built a lightweight inference UI using gr.Interface()
:
- Users can upload any butterfly/moth image
- The trained model returns the predicted class
- Runs directly inside Google Colab
- Upload your image using the Gradio UI
- The model will return the butterfly/moth class name
- Behind the scenes:
- Image is preprocessed
- Forward pass through CNN
- Predicted label returned
- Use pre-trained models like ResNet for higher accuracy
- Handle class imbalance with SMOTE or weighted loss
- Deploy on HuggingFace Spaces or a Flask web server