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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model: torchvision/vit_b_16 (IMAGENET1K_V1)
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
library_name: pytorch
|
| 5 |
+
pipeline_tag: image-classification
|
| 6 |
+
tags:
|
| 7 |
+
- image-classification
|
| 8 |
+
- flowers
|
| 9 |
+
- oxford-102
|
| 10 |
+
- torchvision
|
| 11 |
+
- vit
|
| 12 |
+
- transfer-learning
|
| 13 |
+
metrics:
|
| 14 |
+
- accuracy
|
| 15 |
+
- f1
|
| 16 |
+
model-index:
|
| 17 |
+
- name: ViT-B/16 Flower Classifier
|
| 18 |
+
results:
|
| 19 |
+
- task:
|
| 20 |
+
type: image-classification
|
| 21 |
+
dataset:
|
| 22 |
+
name: Oxford-102 Flowers
|
| 23 |
+
type: oxford-102-flowers
|
| 24 |
+
split: validation
|
| 25 |
+
metrics:
|
| 26 |
+
- type: accuracy
|
| 27 |
+
value: 1.0
|
| 28 |
+
- type: f1
|
| 29 |
+
value: 1.0
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
# ViT-B/16 Flower Classifier
|
| 33 |
+
|
| 34 |
+
Fine-tuned [`torchvision.models.vit_b_16`](https://docs.pytorch.org/vision/main/models/vision_transformer.html) (ImageNet-1K pretrained) for 102-class flower classification on the Oxford-102 Flowers dataset, with the full backbone unfrozen during fine-tuning. Achieves **1.0 accuracy / 1.0 F1** on the validation split.
|
| 35 |
+
|
| 36 |
+
**Recommended when** accuracy is the only priority and the extra size/latency budget is acceptable β e.g. offline batch labeling, research baselines, or any deployment where a ~344MB model and ~87ms mean inference isn't a constraint. For latency- or memory-constrained serving, see [EfficientNetV2-S Flower Classifier](efficientnetv2-s-readme.md) instead.
|
| 37 |
+
|
| 38 |
+
## Usage
|
| 39 |
+
|
| 40 |
+
```python
|
| 41 |
+
import torch
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
from torchvision import models
|
| 44 |
+
|
| 45 |
+
weights_path = hf_hub_download(repo_id="bengid/flower-classifier", filename="ft_ViT-B16.pth")
|
| 46 |
+
|
| 47 |
+
model = models.vit_b_16(weights=None)
|
| 48 |
+
model.heads[-1] = torch.nn.Linear(model.heads[-1].in_features, 102)
|
| 49 |
+
model.load_state_dict(torch.load(weights_path, map_location="cpu", weights_only=True))
|
| 50 |
+
model.eval()
|
| 51 |
+
|
| 52 |
+
# preprocessing: resize(256) -> center-crop(224) -> normalize with dataset mean/std
|
| 53 |
+
# see src/utils.py:get_transforms() in the training repo for the exact pipeline
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
## Training Data
|
| 57 |
+
|
| 58 |
+
[Oxford-102 Flowers](https://www.robots.ox.ac.uk/~vgg/data/flowers/102/) β 8,189 images across 102 flower species, downloaded via `torchvision.datasets.Flowers102`. Class-weighted `CrossEntropyLoss` was used to correct for the dataset's uneven per-class image counts.
|
| 59 |
+
|
| 60 |
+
## Training Procedure
|
| 61 |
+
|
| 62 |
+
Single-stage fine-tune with a **two-phase backbone unfreeze** callback (`BackboneFinetuning`): the ViT backbone starts frozen (only the classification head trains), then unfreezes at a fixed epoch with its own, lower learning rate and a separate parameter group β unlike this project's original (v1) EfficientNet-B0 model, which only ever unfroze its *last 3 backbone blocks*.
|
| 63 |
+
|
| 64 |
+
### Hyperparameters
|
| 65 |
+
|
| 66 |
+
| Parameter | Value |
|
| 67 |
+
|---|---|
|
| 68 |
+
| Optimizer | AdamW |
|
| 69 |
+
| LR scheduler | Cosine annealing (T_max=50, eta_min=1e-06) |
|
| 70 |
+
| Head LR (before unfreeze) | 1e-3 |
|
| 71 |
+
| Head LR (after unfreeze) | 1e-3 |
|
| 72 |
+
| Backbone LR (after unfreeze) | 1e-5 |
|
| 73 |
+
| Unfreeze epoch | 5 |
|
| 74 |
+
| Max epochs | 50 |
|
| 75 |
+
| Batch size | 64 |
|
| 76 |
+
| Effective batch size | 256 |
|
| 77 |
+
| Gradient accumulation | 4 |
|
| 78 |
+
| Precision | 16-mixed |
|
| 79 |
+
| Weight decay | 0.01 |
|
| 80 |
+
| Early stopping patience | 5 |
|
| 81 |
+
|
| 82 |
+
## Evaluation
|
| 83 |
+
|
| 84 |
+
| Metric | Value |
|
| 85 |
+
|---|---|
|
| 86 |
+
| **Accuracy** | **1.0** |
|
| 87 |
+
| **F1** | **1.0** |
|
| 88 |
+
| Parameters | 85,877,094 |
|
| 89 |
+
| Model size | 343.5 MB |
|
| 90 |
+
| Checkpoint size | 1030.7 MB |
|
| 91 |
+
| Mean latency | 86.5 ms |
|
| 92 |
+
| p95 latency | 104.0 ms |
|
| 93 |
+
|
| 94 |
+
Latency measured on {} at batch size 1.
|
| 95 |
+
|
| 96 |
+
## Strengths & Weaknesses
|
| 97 |
+
|
| 98 |
+
**Strengths:**
|
| 99 |
+
- Best accuracy/F1 of every architecture evaluated for this project (see comparison below) β attention-based global context handles flowers that differ mainly in overall shape/arrangement rather than local texture.
|
| 100 |
+
- Full-backbone fine-tuning lets every ViT layer adapt to the flower domain, avoiding the ceiling that partial-unfreeze approaches hit.
|
| 101 |
+
|
| 102 |
+
**Weaknesses:**
|
| 103 |
+
- Largest and slowest model in the lineup by a wide margin β ~4x the parameters and ~3x the mean latency of EfficientNetV2-S for a negligible (~0.05pp) accuracy gain.
|
| 104 |
+
- ViTs are comparatively data-hungry and were historically harder to fine-tune from limited data before full-backbone unfreezing + a long enough schedule (see "Why the earlier models underperformed" below) β this model only reaches its ceiling because both were used.
|
| 105 |
+
- Not a good fit for edge/mobile or high-throughput serving given its size and latency.
|
| 106 |
+
|
| 107 |
+
## Limitations
|
| 108 |
+
|
| 109 |
+
- **Closed-set, single-label**: trained on exactly 102 Oxford flower species; will confidently misclassify any other flower species, non-flower image, or multi-flower image into one of the 102 known classes β there is no out-of-distribution rejection.
|
| 110 |
+
- **Fixed input pipeline**: expects a 224Γ224 center-cropped, normalized input (resize-then-crop). Unusual aspect ratios or off-center subjects can crop the flower out of frame.
|
| 111 |
+
- **No adversarial robustness or calibration guarantees** β confidence scores are not calibrated probabilities.
|
| 112 |
+
- Reported metrics are on the Oxford-102 validation split; real-world images (different lighting, backgrounds, camera quality) may perform worse.
|
| 113 |
+
|
| 114 |
+
## Intended Use
|
| 115 |
+
|
| 116 |
+
**Intended uses:**
|
| 117 |
+
- Flower species identification within the 102 Oxford-102 classes (gardening/botany apps, educational tools, dataset labeling).
|
| 118 |
+
- Backend model for this project's v2 `/classify` API endpoint when accuracy is prioritized over latency.
|
| 119 |
+
|
| 120 |
+
**Out-of-scope uses:**
|
| 121 |
+
- General-purpose plant, object, or scene classification outside the 102 trained species.
|
| 122 |
+
- Medical, toxicity, or safety-related plant identification.
|
| 123 |
+
- Any use where a wrong classification has safety or financial consequences without human review.
|
| 124 |
+
|
| 125 |
+
## Model Comparison
|
| 126 |
+
|
| 127 |
+
This project trained four models in total, in this order:
|
| 128 |
+
|
| 129 |
+
| Model | Val Acc | Val F1 | Params | Size (MB) | Best For |
|
| 130 |
+
|---|---|---|---|---|---|
|
| 131 |
+
| [SimpleCNN (scratch)](https://huggingface.co/bengid/flower-classifier/blob/main/flower_model_weights.pth) | ~0.63 | {} | {} | {} | historical baseline only |
|
| 132 |
+
| [EfficientNet-B0 (v1, partial unfreeze)](https://huggingface.co/bengid/flower-classifier/blob/main/ft_EfficientNet-B0.pth) | >0.93 | {} | {} | {} | historical baseline only |
|
| 133 |
+
| [EfficientNetV2-S](https://huggingface.co/bengid/flower-classifier/blob/main/ft_EfficientNetV2-S.pth) | 0.9997 | 0.9995 | 20,308,150 | 81.8 | efficient production serving |
|
| 134 |
+
| **ViT-B/16 (this model)** | **1.0** | **1.0** | 85,877,094 | 343.5 | maximum accuracy |
|
| 135 |
+
|
| 136 |
+
### Why the earlier models underperformed
|
| 137 |
+
|
| 138 |
+
- **SimpleCNN (scratch)** was trained from randomly initialized weights with no ImageNet pretraining, on a 6-block custom CNN β too little capacity and too little prior visual knowledge to learn 102 fine-grained flower classes from ~8k images alone.
|
| 139 |
+
- **EfficientNet-B0 (v1)** started from ImageNet-pretrained weights but only ever unfroze its *last 3 backbone blocks* during fine-tuning (see this project's root `README.md`, "Fine-Tuning EfficientNet-B0" section, for the original two-stage recipe) β the earlier backbone layers, tuned for general ImageNet features, never adapted to flower-specific low/mid-level features, capping accuracy well below the fully-unfrozen v2 models.
|
| 140 |
+
- Both **EfficientNetV2-S** and **ViT-B/16** (this model) unfreeze the *entire* backbone during fine-tuning, which is the main driver of the jump from ~93% to ~99.97-100% accuracy.
|
| 141 |
+
|
| 142 |
+
## License
|
| 143 |
+
|
| 144 |
+
Apache 2.0, consistent with this project's license.
|
| 145 |
+
|
| 146 |
+
## Citation
|
| 147 |
+
|
| 148 |
+
**Base model (Vision Transformer):**
|
| 149 |
+
```bibtex
|
| 150 |
+
@article{dosovitskiy2020vit,
|
| 151 |
+
title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
|
| 152 |
+
author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
|
| 153 |
+
journal={arXiv preprint arXiv:2010.11929},
|
| 154 |
+
year={2020}
|
| 155 |
+
}
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
**Training dataset:**
|
| 159 |
+
```bibtex
|
| 160 |
+
@inproceedings{nilsback2008automated,
|
| 161 |
+
title={Automated flower classification over a large number of classes},
|
| 162 |
+
author={Nilsback, Maria-Elena and Zisserman, Andrew},
|
| 163 |
+
booktitle={2008 Sixth Indian Conference on Computer Vision, Graphics \& Image Processing},
|
| 164 |
+
pages={722--729},
|
| 165 |
+
year={2008},
|
| 166 |
+
organization={IEEE}
|
| 167 |
+
}
|
| 168 |
+
```
|