Instructions to use turhancan97/vit-tiny-lora-food101 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use turhancan97/vit-tiny-lora-food101 with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,199 +1,167 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
-
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
-
|
| 36 |
-
## Uses
|
| 37 |
-
|
| 38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
-
|
| 40 |
-
### Direct Use
|
| 41 |
-
|
| 42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
-
|
| 44 |
-
[More Information Needed]
|
| 45 |
-
|
| 46 |
-
### Downstream Use [optional]
|
| 47 |
-
|
| 48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
-
|
| 50 |
-
[More Information Needed]
|
| 51 |
-
|
| 52 |
-
### Out-of-Scope Use
|
| 53 |
-
|
| 54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
-
|
| 56 |
-
[More Information Needed]
|
| 57 |
-
|
| 58 |
-
## Bias, Risks, and Limitations
|
| 59 |
-
|
| 60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
-
|
| 62 |
-
[More Information Needed]
|
| 63 |
-
|
| 64 |
-
### Recommendations
|
| 65 |
-
|
| 66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
-
|
| 68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
-
|
| 70 |
-
## How to Get Started with the Model
|
| 71 |
-
|
| 72 |
-
Use the code below to get started with the model.
|
| 73 |
-
|
| 74 |
-
[More Information Needed]
|
| 75 |
-
|
| 76 |
-
## Training Details
|
| 77 |
-
|
| 78 |
-
### Training Data
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
-
|
| 84 |
-
### Training Procedure
|
| 85 |
-
|
| 86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
-
|
| 88 |
-
#### Preprocessing [optional]
|
| 89 |
-
|
| 90 |
-
[More Information Needed]
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#### Training Hyperparameters
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
|
| 103 |
## Evaluation
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
-
|
| 129 |
-
[More Information Needed]
|
| 130 |
-
|
| 131 |
-
#### Summary
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
## Model Examination [optional]
|
| 136 |
-
|
| 137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
-
|
| 139 |
-
[More Information Needed]
|
| 140 |
-
|
| 141 |
-
## Environmental Impact
|
| 142 |
-
|
| 143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
-
|
| 145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
-
|
| 147 |
-
- **Hardware Type:** [More Information Needed]
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
##
|
| 156 |
|
| 157 |
-
|
| 158 |
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
|
|
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
|
| 187 |
-
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
##
|
| 190 |
|
| 191 |
-
[
|
| 192 |
|
| 193 |
-
##
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: peft
|
| 4 |
+
base_model: WinKawaks/vit-tiny-patch16-224
|
| 5 |
+
tags:
|
| 6 |
+
- lora
|
| 7 |
+
- peft
|
| 8 |
+
- image-classification
|
| 9 |
+
- vit
|
| 10 |
+
- food101
|
| 11 |
+
datasets:
|
| 12 |
+
- food101
|
| 13 |
+
pipeline_tag: image-classification
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# ViT-tiny LoRA adapter on Food-101
|
| 19 |
|
| 20 |
+
A LoRA adapter that teaches [`WinKawaks/vit-tiny-patch16-224`](https://huggingface.co/WinKawaks/vit-tiny-patch16-224) to classify images from the [Food-101](https://huggingface.co/datasets/food101) dataset (101 food categories) while leaving the original pretrained weights mathematically untouched.
|
| 21 |
|
| 22 |
+
- **Base model:** `WinKawaks/vit-tiny-patch16-224` (~5.7M params)
|
| 23 |
+
- **Dataset:** [Food-101](https://huggingface.co/datasets/food101) (75,750 train / 25,250 test, 101 classes)
|
| 24 |
+
- **Method:** LoRA on attention `query` + `value` projections + a fresh 101-way classification head
|
| 25 |
+
- **Demo Space:** [`turhancan97/vit-tiny-imagenet-demo`](https://huggingface.co/spaces/turhancan97/vit-tiny-imagenet-demo)
|
| 26 |
|
| 27 |
+
## How it works
|
| 28 |
|
| 29 |
+
The backbone is never fine-tuned. Instead a low-rank update $\Delta W = BA$ (with rank $r = 8$) is added to each attention projection, and a separate 101-class linear head is trained on top of the pooled CLS features. The full artifact is tiny (~1–2 MB) and additive — disabling the adapter at inference time recovers the exact original ImageNet-1k model.
|
| 30 |
|
| 31 |
+
```text
|
| 32 |
+
adapter_config.json # PEFT LoRA config
|
| 33 |
+
adapter_model.safetensors # LoRA weights (B, A matrices)
|
| 34 |
+
classifier.pt # 101-way Linear head (state_dict)
|
| 35 |
+
labels.json # {"0": "apple_pie", "1": "baby_back_ribs", ...}
|
| 36 |
+
preprocessor_config.json # image processor (224x224, standard ImageNet norm)
|
| 37 |
+
```
|
| 38 |
|
| 39 |
+
## Training
|
| 40 |
|
| 41 |
+
Trained with the script at [`turhancan97/vit-tiny-imagenet-demo/train_lora.py`](https://huggingface.co/spaces/turhancan97/vit-tiny-imagenet-demo/blob/main/train_lora.py):
|
| 42 |
|
| 43 |
+
```bash
|
| 44 |
+
python train_lora.py \
|
| 45 |
+
--rank 8 --alpha 16 --dropout 0.1 \
|
| 46 |
+
--target-modules query value \
|
| 47 |
+
--epochs 5 --batch-size 64 --lr 5e-4 \
|
| 48 |
+
--warmup-ratio 0.03 --weight-decay 0.0 \
|
| 49 |
+
--push-to-hub turhancan97/vit-tiny-lora-food101
|
| 50 |
+
```
|
| 51 |
|
| 52 |
+
**Hyperparameters**
|
| 53 |
|
| 54 |
+
| Setting | Value |
|
| 55 |
+
|---|---|
|
| 56 |
+
| LoRA rank | 8 |
|
| 57 |
+
| LoRA alpha | 16 |
|
| 58 |
+
| LoRA dropout | 0.1 |
|
| 59 |
+
| Target modules | `query`, `value` |
|
| 60 |
+
| Optimizer | AdamW (HF `Trainer` default) |
|
| 61 |
+
| Learning rate | 5e-4 |
|
| 62 |
+
| Batch size | 64 |
|
| 63 |
+
| Epochs | 5 |
|
| 64 |
+
| Warmup ratio | 0.03 |
|
| 65 |
+
| Weight decay | 0.0 |
|
| 66 |
+
| Precision | FP16 |
|
| 67 |
+
| Augmentation | RandomResizedCrop(0.8–1.0), RandomHorizontalFlip |
|
| 68 |
|
| 69 |
+
**Trainable parameters:** ~93k of ~5.6M total (**~1.7%**).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
## Evaluation
|
| 72 |
|
| 73 |
+
Evaluated on the Food-101 test split (25,250 images).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
| Metric | Value |
|
| 76 |
+
|---|---|
|
| 77 |
+
| Top-1 accuracy | _fill in from `eval_metrics.json`_ |
|
| 78 |
+
| Top-5 accuracy | _fill in from `eval_metrics.json`_ |
|
| 79 |
|
| 80 |
+
## Usage
|
| 81 |
|
| 82 |
+
The adapter uses the standard PEFT format plus a sidecar `classifier.pt` and `labels.json`. Minimal loader:
|
| 83 |
|
| 84 |
+
```python
|
| 85 |
+
import json
|
| 86 |
+
import torch
|
| 87 |
+
from huggingface_hub import hf_hub_download
|
| 88 |
+
from peft import PeftModel
|
| 89 |
+
from torch import nn
|
| 90 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 91 |
|
| 92 |
+
BASE = "WinKawaks/vit-tiny-patch16-224"
|
| 93 |
+
ADAPTER = "turhancan97/vit-tiny-lora-food101"
|
| 94 |
|
| 95 |
+
processor = AutoImageProcessor.from_pretrained(BASE, use_fast=True)
|
| 96 |
+
base = AutoModelForImageClassification.from_pretrained(BASE)
|
| 97 |
+
model = PeftModel.from_pretrained(base, ADAPTER)
|
| 98 |
|
| 99 |
+
id2label = {int(k): v for k, v in json.loads(
|
| 100 |
+
open(hf_hub_download(ADAPTER, "labels.json")).read()
|
| 101 |
+
).items()}
|
| 102 |
|
| 103 |
+
head_state = torch.load(
|
| 104 |
+
hf_hub_download(ADAPTER, "classifier.pt"), map_location="cpu", weights_only=True
|
| 105 |
+
)
|
| 106 |
+
head = nn.Linear(base.config.hidden_size, len(id2label))
|
| 107 |
+
head.load_state_dict(head_state)
|
| 108 |
+
model.base_model.model.classifier = head
|
| 109 |
|
| 110 |
+
model.eval()
|
| 111 |
+
```
|
| 112 |
|
| 113 |
+
**Inference:**
|
| 114 |
|
| 115 |
+
```python
|
| 116 |
+
from PIL import Image
|
| 117 |
|
| 118 |
+
image = Image.open("my_food.jpg").convert("RGB")
|
| 119 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 120 |
+
with torch.inference_mode():
|
| 121 |
+
logits = model(**inputs).logits[0]
|
| 122 |
+
topk = logits.softmax(-1).topk(5)
|
| 123 |
+
for score, idx in zip(topk.values, topk.indices):
|
| 124 |
+
print(f"{id2label[idx.item()]:30s} {score.item():.3f}")
|
| 125 |
+
```
|
| 126 |
|
| 127 |
+
**Switching back to the base model** (ImageNet-1k, 1000 classes) without unloading:
|
| 128 |
|
| 129 |
+
```python
|
| 130 |
+
with model.disable_adapter():
|
| 131 |
+
logits = base(**inputs).logits # uses the pristine pretrained weights
|
| 132 |
+
```
|
| 133 |
|
| 134 |
+
## Intended use
|
| 135 |
|
| 136 |
+
- Educational / demo use for showing how LoRA adds new capabilities to a frozen backbone.
|
| 137 |
+
- Classifying photos of prepared food into the Food-101 taxonomy.
|
| 138 |
|
| 139 |
+
## Limitations
|
| 140 |
|
| 141 |
+
- Only 101 food categories; anything outside the taxonomy will be misclassified.
|
| 142 |
+
- Trained on Food-101 which is mostly western/restaurant-style dishes, with label noise in the original data.
|
| 143 |
+
- ViT-tiny is a low-capacity backbone; a larger base model would likely get higher accuracy with the same adapter recipe.
|
| 144 |
|
| 145 |
+
## License
|
| 146 |
|
| 147 |
+
Apache-2.0, matching the base model and the [Food-101 dataset license](https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/).
|
| 148 |
|
| 149 |
+
## Citation
|
| 150 |
|
| 151 |
+
If you use this adapter, please cite the underlying works:
|
| 152 |
|
| 153 |
+
```bibtex
|
| 154 |
+
@inproceedings{hu2022lora,
|
| 155 |
+
title={{LoRA}: Low-Rank Adaptation of Large Language Models},
|
| 156 |
+
author={Hu, Edward J. and Shen, Yelong and Wallis, Phillip and Allen-Zhu, Zeyuan and Li, Yuanzhi and Wang, Shean and Wang, Lu and Chen, Weizhu},
|
| 157 |
+
booktitle={ICLR},
|
| 158 |
+
year={2022}
|
| 159 |
+
}
|
| 160 |
|
| 161 |
+
@inproceedings{bossard2014food101,
|
| 162 |
+
title={Food-101 -- Mining Discriminative Components with Random Forests},
|
| 163 |
+
author={Bossard, Lukas and Guillaumin, Matthieu and Van Gool, Luc},
|
| 164 |
+
booktitle={ECCV},
|
| 165 |
+
year={2014}
|
| 166 |
+
}
|
| 167 |
+
```
|