This repository contains a convolutional neural network (CNN) implemented in PyTorch to classify images of different dog breeds. The model is trained on a custom image dataset using a handcrafted CNN architecture, including data preprocessing, training loop, evaluation, and result visualization.
- Path:
archive_4dog
- The dataset should follow the ImageFolder structure:
archive_4dog/
βββ breed1/
β βββ img1.jpg
β βββ img2.jpg
βββ breed2/
β βββ img3.jpg
β βββ ...
- Only
.jpg
and.png
files are supported. - Corrupted images are automatically detected and removed before training.
The network includes:
- 5 convolutional layers (with increasing filters: 32 β 512)
- ReLU activations and MaxPooling
- Fully connected classifier with dropout
- Custom image size: 150Γ150
pip install torch torchvision matplotlib Pillow
Make sure the images are sorted into subfolders by class name (breed).
python train_cnn.py
This will:
- Train the model
- Save model as
pet_classifier_final.pth
- Save training graphs as
training_results.png
Parameter | Value |
---|---|
Batch Size | 32 |
Image Size | 150Γ150 |
Epochs | 30 |
Optimizer | Adam |
Learning Rate | 0.0001 |
Loss Function | CrossEntropy |
After training completes, the script generates:
pet_classifier_final.pth
: Final trained model weightstraining_results.png
: Training/validation loss and accuracy curves
Example output:
Epoch [30/30]
Train Loss: 0.1734 | Val Loss: 0.2156 | Accuracy: 93.87%
- Custom convolutional neural network (from scratch)
- Real-time training statistics
- Automatic removal of corrupted images
- Random rotation, flip, resize, crop augmentation