README.md
Browse files
README.md
CHANGED
|
@@ -11,6 +11,8 @@ tags:
|
|
| 11 |
- fine-tuning
|
| 12 |
- vision
|
| 13 |
- YouthAI
|
|
|
|
|
|
|
| 14 |
library_name: torchvision
|
| 15 |
metrics:
|
| 16 |
- accuracy
|
|
@@ -32,4 +34,138 @@ model-index:
|
|
| 32 |
value: 98.32
|
| 33 |
- type: f1
|
| 34 |
value: 0.98
|
| 35 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
- fine-tuning
|
| 12 |
- vision
|
| 13 |
- YouthAI
|
| 14 |
+
dataset:
|
| 15 |
+
- alessiocorrado99/animals10
|
| 16 |
library_name: torchvision
|
| 17 |
metrics:
|
| 18 |
- accuracy
|
|
|
|
| 34 |
value: 98.32
|
| 35 |
- type: f1
|
| 36 |
value: 0.98
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
# 🦁 ResNet50 Fine-Tuned on Animals-10 Dataset
|
| 40 |
+
|
| 41 |
+

|
| 42 |
+
|
| 43 |
+
> **Note:** This project was developed by **Group 4** as part of the **Youth AI Initiative**. It demonstrates how to achieve state-of-the-art performance (**>98% accuracy**) using modern Fine-Tuning techniques on standard architectures.
|
| 44 |
+
|
| 45 |
+
## 📝 Overview
|
| 46 |
+
This project implements a high-performance Image Classification model capable of identifying **10 different animal species** with near-perfect accuracy.
|
| 47 |
+
|
| 48 |
+
Unlike complex ensemble approaches that consume vast resources, we focused on **optimizing a single robust backbone (ResNet50)** using advanced training strategies like **OneCycleLR**, **Label Smoothing**, and **Mixed Precision Training**. This resulted in a lightweight yet extremely powerful model that outperforms standard baselines.
|
| 49 |
+
|
| 50 |
+
## 🎯 Objectives
|
| 51 |
+
- **High Accuracy:** Achieve >95% accuracy on the test set (Achieved: **98.32%**).
|
| 52 |
+
- **Robustness:** Prevent overfitting using regularization techniques (Label Smoothing, Weight Decay).
|
| 53 |
+
- **Efficiency:** Utilize GPU acceleration (AMP) for faster training.
|
| 54 |
+
- **Explainability:** Analyze errors using Confusion Matrices and Per-Class metrics.
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## 🏆 Performance Metrics
|
| 59 |
+
|
| 60 |
+
The model was evaluated on an independent test set (10% split) and achieved exceptional results across all metrics.
|
| 61 |
+
|
| 62 |
+
| Metric | Score | Description |
|
| 63 |
+
| :--- | :--- | :--- |
|
| 64 |
+
| **Test Accuracy** | **98.32%** | Overall correct predictions. |
|
| 65 |
+
| **F1-Score (Weighted)** | **0.98** | Harmonic mean of precision and recall. |
|
| 66 |
+
| **Precision** | **0.98** | Accuracy of positive predictions. |
|
| 67 |
+
| **Recall** | **0.98** | Ability to find all positive instances. |
|
| 68 |
+
|
| 69 |
+
### 📊 Confusion Matrix & Error Analysis
|
| 70 |
+
The confusion matrix below demonstrates the model's robustness. The dark diagonal line indicates near-perfect classification.
|
| 71 |
+
|
| 72 |
+

|
| 73 |
+
|
| 74 |
+
### 📈 Per-Class Performance
|
| 75 |
+
The model maintains high performance (>95%) even on difficult classes.
|
| 76 |
+
|
| 77 |
+
| Class (IT/EN) | Precision | Recall | F1-Score |
|
| 78 |
+
| :--- | :--- | :--- | :--- |
|
| 79 |
+
| **Cane (Dog)** | 0.99 | 0.98 | **0.99** |
|
| 80 |
+
| **Cavallo (Horse)** | 0.99 | 0.99 | **0.99** |
|
| 81 |
+
| **Elefante (Elephant)** | 0.98 | 0.99 | **0.99** |
|
| 82 |
+
| **Farfalla (Butterfly)** | 0.99 | 0.98 | **0.99** |
|
| 83 |
+
| **Gallina (Chicken)** | 0.97 | 0.98 | **0.98** |
|
| 84 |
+
| **Gatto (Cat)** | 0.96 | 0.97 | **0.97** |
|
| 85 |
+
| **Mucca (Cow)** | 0.97 | 0.96 | **0.97** |
|
| 86 |
+
| **Pecora (Sheep)** | 0.98 | 0.98 | **0.98** |
|
| 87 |
+
| **Ragno (Spider)** | 0.99 | 0.99 | **0.99** |
|
| 88 |
+
| **Scoiattolo (Squirrel)** | 0.98 | 0.97 | **0.98** |
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
## ⚙️ Methodology & Training Techniques
|
| 93 |
+
|
| 94 |
+
To achieve **98.32% accuracy** while maintaining a healthy **Bias-Variance Tradeoff**, we employed the following advanced techniques:
|
| 95 |
+
|
| 96 |
+
| Technique | Purpose in this Project |
|
| 97 |
+
| :--- | :--- |
|
| 98 |
+
| **Model Architecture** | **ResNet50** backbone for powerful feature extraction. |
|
| 99 |
+
| **Optimization** | **AdamW** optimizer for better weight decay and regularization. |
|
| 100 |
+
| **Learning Rate Schedule** | **OneCycleLR** policy for faster and more stable convergence. |
|
| 101 |
+
| **Regularization** | **Label Smoothing (0.1)** to prevent overfitting and overconfidence. |
|
| 102 |
+
| **Data Augmentation** | `RandomErasing`, `ColorJitter`, and `Rotation` to force feature learning. |
|
| 103 |
+
| **Mixed Precision** | **Native AMP (fp16)** for efficient VRAM usage and speed. |
|
| 104 |
+
| **Training Strategy** | **Fine-Tuning** (Frozen early layers, trainable Layer 4 + FC). |
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## 🛠️ Installation & Requirements
|
| 109 |
+
|
| 110 |
+
To run this model, you need to install the following dependencies. We recommend using a GPU for faster inference.
|
| 111 |
+
|
| 112 |
+
```bash
|
| 113 |
+
pip install torch torchvision torchaudio pillow
|
| 114 |
+
````
|
| 115 |
+
|
| 116 |
+
## 💻 Usage Code (GPU Supported)
|
| 117 |
+
|
| 118 |
+
You can use this model directly with PyTorch. The code below automatically detects if you have a GPU (CUDA).
|
| 119 |
+
|
| 120 |
+
```python
|
| 121 |
+
import torch
|
| 122 |
+
import torch.nn as nn
|
| 123 |
+
from torchvision import models, transforms
|
| 124 |
+
from PIL import Image
|
| 125 |
+
|
| 126 |
+
# 1. Device Configuration
|
| 127 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 128 |
+
|
| 129 |
+
# 2. Define Architecture
|
| 130 |
+
model = models.resnet50(weights=None)
|
| 131 |
+
model.fc = nn.Linear(model.fc.in_features, 10)
|
| 132 |
+
|
| 133 |
+
# 3. Load Weights
|
| 134 |
+
# Ensure 'best_resnet50_animals.pt' is in your directory
|
| 135 |
+
model.load_state_dict(torch.load("best_resnet50_animals.pt", map_location=device))
|
| 136 |
+
model = model.to(device)
|
| 137 |
+
model.eval()
|
| 138 |
+
|
| 139 |
+
# 4. Preprocess Image
|
| 140 |
+
transform = transforms.Compose([
|
| 141 |
+
transforms.Resize((256, 256)),
|
| 142 |
+
transforms.CenterCrop(224),
|
| 143 |
+
transforms.ToTensor(),
|
| 144 |
+
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
| 145 |
+
])
|
| 146 |
+
|
| 147 |
+
# 5. Predict
|
| 148 |
+
img_path = "test_image.jpg"
|
| 149 |
+
try:
|
| 150 |
+
img = Image.open(img_path).convert("RGB")
|
| 151 |
+
input_tensor = transform(img).unsqueeze(0).to(device)
|
| 152 |
+
|
| 153 |
+
with torch.no_grad():
|
| 154 |
+
output = model(input_tensor)
|
| 155 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 156 |
+
confidence, pred = torch.max(probabilities, 0)
|
| 157 |
+
|
| 158 |
+
classes = ['cane', 'cavallo', 'elefante', 'farfalla', 'gallina',
|
| 159 |
+
'gatto', 'mucca', 'pecora', 'ragno', 'scoiattolo']
|
| 160 |
+
|
| 161 |
+
print(f"Prediction: {classes[pred.item()].upper()} ({confidence.item():.2%})")
|
| 162 |
+
|
| 163 |
+
except FileNotFoundError:
|
| 164 |
+
print("Image not found.")
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
## 👥 Team Members (Group 4)
|
| 168 |
+
|
| 169 |
+
* [Kuzey KAYA, Maruf Salih ATALA, Umut ÇATAK, Kuzey ÇALIŞKAN, Mücahit YETER, Yusuf BATMACA, Göktüğ...]
|
| 170 |
+
<!-- end list -->
|
| 171 |
+
```
|