Update README.md
Browse files
README.md
CHANGED
|
@@ -6,87 +6,270 @@ tags:
|
|
| 6 |
- flowers
|
| 7 |
- computer-vision
|
| 8 |
pipeline_tag: image-classification
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
# 🌸 102-Flower Image Classifier — EfficientNet-B0
|
| 12 |
|
| 13 |
-
PyTorch
|
| 14 |
|
| 15 |
-
##
|
| 16 |
|
| 17 |
-
|
| 18 |
-
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
## Files
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
```python
|
|
|
|
| 37 |
import torch
|
| 38 |
import torch.nn as nn
|
| 39 |
from torchvision import models, transforms
|
| 40 |
from PIL import Image
|
| 41 |
|
| 42 |
-
checkpoint = torch.load(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
model = models.efficientnet_b0(weights=None)
|
| 45 |
-
model.classifier[1] = nn.Linear(model.classifier[1].in_features, 102)
|
| 46 |
|
| 47 |
-
model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
model.eval()
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
idx_to_class = {
|
| 51 |
-
int(k): v
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
transform = transforms.Compose([
|
| 55 |
transforms.Resize(256),
|
| 56 |
transforms.CenterCrop(224),
|
| 57 |
transforms.ToTensor(),
|
| 58 |
-
transforms.Normalize(
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
])
|
| 61 |
|
| 62 |
-
image = Image.open(
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
idx = prediction.item()
|
| 70 |
-
|
| 71 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
```
|
| 73 |
|
| 74 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
- `epoch`
|
| 78 |
-
- `model_state_dict`
|
| 79 |
-
- `optimizer_state_dict`
|
| 80 |
-
- `class_to_idx`
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
|
|
|
| 6 |
- flowers
|
| 7 |
- computer-vision
|
| 8 |
pipeline_tag: image-classification
|
| 9 |
+
language:
|
| 10 |
+
- en
|
| 11 |
---
|
| 12 |
|
| 13 |
# 🌸 102-Flower Image Classifier — EfficientNet-B0
|
| 14 |
|
| 15 |
+
A PyTorch EfficientNet-B0 image classification model trained to recognize **102 flower categories** from the Oxford 102 Category Flower Dataset.
|
| 16 |
|
| 17 |
+
## Model Performance
|
| 18 |
|
| 19 |
+
| Metric | Result |
|
| 20 |
+
| ------------------------ | --------------- |
|
| 21 |
+
| Architecture | EfficientNet-B0 |
|
| 22 |
+
| Number of classes | 102 |
|
| 23 |
+
| Input size | 224 × 224 |
|
| 24 |
+
| Best validation accuracy | **94.38%** |
|
| 25 |
+
| Training epochs | 3 |
|
| 26 |
+
| Optimizer | AdamW |
|
| 27 |
+
| Learning rate | 0.001 |
|
| 28 |
+
|
| 29 |
+
The model was trained using transfer learning with an ImageNet-pretrained EfficientNet-B0 backbone.
|
| 30 |
+
|
| 31 |
+
## Dataset
|
| 32 |
+
|
| 33 |
+
This model was trained using the **Oxford 102 Category Flower Dataset**, created by **Maria-Elena Nilsback and Andrew Zisserman**.
|
| 34 |
+
|
| 35 |
+
The dataset contains 102 flower categories with variations in scale, pose, lighting, and appearance.
|
| 36 |
+
|
| 37 |
+
Official dataset page:
|
| 38 |
+
|
| 39 |
+
https://www.robots.ox.ac.uk/~vgg/data/flowers/102/
|
| 40 |
+
|
| 41 |
+
Please review the original dataset documentation and terms before using or redistributing dataset-derived material.
|
| 42 |
|
| 43 |
## Files
|
| 44 |
|
| 45 |
+
* `checkpoint.pth` — trained PyTorch checkpoint
|
| 46 |
+
* `model_config.json` — model architecture information
|
| 47 |
+
* `training_config.json` — training configuration
|
| 48 |
+
* `class_config.json` — exact class/index mappings
|
| 49 |
+
* `labels.txt` — flower labels
|
| 50 |
+
* `requirements.txt` — Python dependencies
|
| 51 |
+
|
| 52 |
+
## Checkpoint Contents
|
| 53 |
|
| 54 |
+
The `checkpoint.pth` file contains:
|
| 55 |
+
|
| 56 |
+
* `epoch`
|
| 57 |
+
* `model_state_dict`
|
| 58 |
+
* `optimizer_state_dict`
|
| 59 |
+
* `class_to_idx`
|
| 60 |
+
|
| 61 |
+
## Use the Model
|
| 62 |
+
|
| 63 |
+
Install the dependencies:
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
pip install torch torchvision pillow
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
Load the model:
|
| 70 |
|
| 71 |
```python
|
| 72 |
+
import json
|
| 73 |
import torch
|
| 74 |
import torch.nn as nn
|
| 75 |
from torchvision import models, transforms
|
| 76 |
from PIL import Image
|
| 77 |
|
| 78 |
+
checkpoint = torch.load(
|
| 79 |
+
"checkpoint.pth",
|
| 80 |
+
map_location="cpu",
|
| 81 |
+
weights_only=False
|
| 82 |
+
)
|
| 83 |
|
| 84 |
model = models.efficientnet_b0(weights=None)
|
|
|
|
| 85 |
|
| 86 |
+
model.classifier[1] = nn.Linear(
|
| 87 |
+
model.classifier[1].in_features,
|
| 88 |
+
102
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
model.load_state_dict(
|
| 92 |
+
checkpoint["model_state_dict"]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
model.eval()
|
| 96 |
|
| 97 |
+
with open(
|
| 98 |
+
"class_config.json",
|
| 99 |
+
"r",
|
| 100 |
+
encoding="utf-8"
|
| 101 |
+
) as f:
|
| 102 |
+
class_config = json.load(f)
|
| 103 |
+
|
| 104 |
idx_to_class = {
|
| 105 |
+
int(k): v
|
| 106 |
+
for k, v in class_config["idx_to_class"].items()
|
| 107 |
}
|
| 108 |
|
| 109 |
transform = transforms.Compose([
|
| 110 |
transforms.Resize(256),
|
| 111 |
transforms.CenterCrop(224),
|
| 112 |
transforms.ToTensor(),
|
| 113 |
+
transforms.Normalize(
|
| 114 |
+
[0.485, 0.456, 0.406],
|
| 115 |
+
[0.229, 0.224, 0.225]
|
| 116 |
+
)
|
| 117 |
])
|
| 118 |
|
| 119 |
+
image = Image.open(
|
| 120 |
+
"flower.jpg"
|
| 121 |
+
).convert("RGB")
|
| 122 |
+
|
| 123 |
+
x = transform(
|
| 124 |
+
image
|
| 125 |
+
).unsqueeze(0)
|
| 126 |
+
|
| 127 |
+
with torch.inference_mode():
|
| 128 |
+
probabilities = torch.softmax(
|
| 129 |
+
model(x),
|
| 130 |
+
dim=1
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
confidence, prediction = probabilities.max(
|
| 134 |
+
dim=1
|
| 135 |
+
)
|
| 136 |
|
| 137 |
idx = prediction.item()
|
| 138 |
+
|
| 139 |
+
print(
|
| 140 |
+
"Prediction:",
|
| 141 |
+
idx_to_class[idx]
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
print(
|
| 145 |
+
"Confidence:",
|
| 146 |
+
f"{confidence.item() * 100:.2f}%"
|
| 147 |
+
)
|
| 148 |
```
|
| 149 |
|
| 150 |
+
## Top-5 Predictions
|
| 151 |
+
|
| 152 |
+
You can also get the five most likely flower categories:
|
| 153 |
+
|
| 154 |
+
```python
|
| 155 |
+
with torch.inference_mode():
|
| 156 |
+
probabilities = torch.softmax(
|
| 157 |
+
model(x),
|
| 158 |
+
dim=1
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
values, indices = torch.topk(
|
| 162 |
+
probabilities,
|
| 163 |
+
k=5
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
for probability, index in zip(
|
| 167 |
+
values[0],
|
| 168 |
+
indices[0]
|
| 169 |
+
):
|
| 170 |
+
flower = idx_to_class[index.item()]
|
| 171 |
+
confidence = probability.item() * 100
|
| 172 |
+
|
| 173 |
+
print(
|
| 174 |
+
f"{flower}: {confidence:.2f}%"
|
| 175 |
+
)
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## Training Configuration
|
| 179 |
+
|
| 180 |
+
The model was trained using transfer learning.
|
| 181 |
+
|
| 182 |
+
* Architecture: EfficientNet-B0
|
| 183 |
+
* Classes: 102
|
| 184 |
+
* Image size: 224 × 224
|
| 185 |
+
* Batch size: 32
|
| 186 |
+
* Epochs: 3
|
| 187 |
+
* Optimizer: AdamW
|
| 188 |
+
* Learning rate: 0.001
|
| 189 |
+
* Loss: CrossEntropyLoss
|
| 190 |
+
* Scheduler: StepLR
|
| 191 |
+
* Mixed precision: CUDA when available
|
| 192 |
+
|
| 193 |
+
### Training Augmentation
|
| 194 |
+
|
| 195 |
+
* Random resized crop
|
| 196 |
+
* Random horizontal flip
|
| 197 |
+
* Color jitter
|
| 198 |
+
* ImageNet normalization
|
| 199 |
+
|
| 200 |
+
### Validation Preprocessing
|
| 201 |
+
|
| 202 |
+
* Resize to 256
|
| 203 |
+
* Center crop to 224
|
| 204 |
+
* ImageNet normalization
|
| 205 |
+
|
| 206 |
+
## Evaluation
|
| 207 |
+
|
| 208 |
+
The best validation accuracy achieved during training was:
|
| 209 |
+
|
| 210 |
+
**94.38%**
|
| 211 |
+
|
| 212 |
+
This result corresponds to the validation split used during training.
|
| 213 |
+
|
| 214 |
+
Performance may vary on images that differ substantially from the training data.
|
| 215 |
+
|
| 216 |
+
## Interactive Demo
|
| 217 |
+
|
| 218 |
+
An interactive Gradio application can be deployed using this model so that users can upload flower images directly through a web browser.
|
| 219 |
+
|
| 220 |
+
The demo can provide:
|
| 221 |
+
|
| 222 |
+
* Image upload
|
| 223 |
+
* Flower prediction
|
| 224 |
+
* Confidence score
|
| 225 |
+
* Top-5 predictions
|
| 226 |
+
|
| 227 |
+
## Limitations
|
| 228 |
+
|
| 229 |
+
This model is designed to classify images into the 102 flower categories represented in the training dataset.
|
| 230 |
+
|
| 231 |
+
Predictions may be less reliable when:
|
| 232 |
+
|
| 233 |
+
* The image does not contain a supported flower category.
|
| 234 |
+
* The flower is heavily obscured.
|
| 235 |
+
* The image is blurry or poorly illuminated.
|
| 236 |
+
* Multiple flowers appear in the image.
|
| 237 |
+
* The image differs substantially from the training distribution.
|
| 238 |
+
|
| 239 |
+
This model should be considered an image-classification research/demo model and not a definitive botanical identification system.
|
| 240 |
+
|
| 241 |
+
## Citation
|
| 242 |
+
|
| 243 |
+
If you use this model or the underlying dataset, please provide attribution to the original dataset authors.
|
| 244 |
+
|
| 245 |
+
**Maria-Elena Nilsback and Andrew Zisserman**
|
| 246 |
+
|
| 247 |
+
*"Automated Flower Classification over a Large Number of Classes."*
|
| 248 |
+
|
| 249 |
+
Proceedings of the Indian Conference on Computer Vision, Graphics and Image Processing (ICVGIP), 2008.
|
| 250 |
+
|
| 251 |
+
## Dataset Reference
|
| 252 |
+
|
| 253 |
+
Oxford 102 Category Flower Dataset:
|
| 254 |
+
|
| 255 |
+
https://www.robots.ox.ac.uk/~vgg/data/flowers/102/
|
| 256 |
+
|
| 257 |
+
## Author
|
| 258 |
+
|
| 259 |
+
**Naila Rais**
|
| 260 |
+
|
| 261 |
+
Hugging Face:
|
| 262 |
|
| 263 |
+
`nailarais1`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
Model:
|
| 266 |
|
| 267 |
+
`nailarais1/image-classifier-efficientnet`
|
| 268 |
|
| 269 |
+
Architecture:
|
| 270 |
|
| 271 |
+
**EfficientNet-B0**
|
| 272 |
|
| 273 |
+
Best validation accuracy:
|
| 274 |
|
| 275 |
+
**94.38%**
|