File size: 2,268 Bytes
f6dabf7
 
 
 
 
652448b
f6dabf7
25f706b
 
f6dabf7
25f706b
f6dabf7
25f706b
f6dabf7
652448b
f6dabf7
652448b
f6dabf7
652448b
f6dabf7
25f706b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6dabf7
652448b
f6dabf7
652448b
25f706b
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
library_name: peft
base_model: google/vit-large-patch16-224
---

## LoRA Image Binary Classification LoRA adapter

Trained on APTOS 2019 Kaggle competition for identifying diabetic retinopathy. In this case I've modified the problem
to binary classifier (diagnosis=0 vs. all others; 50-50% distribution in training data)

Base Model: [google/vit-large-patch16-224](https://huggingface.co/google/vit-large-patch16-224)

Dataset: https://www.kaggle.com/c/aptos2019-blindness-detection - fundus images of the back of the eye, and diabetic retinopathy score

Training notebook: https://colab.research.google.com/drive/1TVsUyyou87E26Sz40CdBH3CzWoVckgtq?usp=sharing

On 10% held-out of training data: accuracy 98%

- PEFT 0.5.0

PEFT Image classifier inference / [Gradio app](https://huggingface.co/spaces/monsoon-nlp/eyegazer-demo/blob/main/app.py)

```python
from peft import PeftModel
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification

from torchvision.transforms import (
    CenterCrop,
    Compose,
    Normalize,
    RandomHorizontalFlip,
    RandomResizedCrop,
    Resize,
    ToTensor,
)

model_name = 'google/vit-large-patch16-224'
adapter = 'monsoon-nlp/eyegazer-vit-binary'

image_processor = AutoImageProcessor.from_pretrained(model_name)

normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
train_transforms = Compose(
    [
        RandomResizedCrop(image_processor.size["height"]),
        RandomHorizontalFlip(),
        ToTensor(),
        normalize,
    ]
)

val_transforms = Compose(
    [
        Resize(image_processor.size["height"]),
        CenterCrop(image_processor.size["height"]),
        ToTensor(),
        normalize,
    ]
)

model = AutoModelForImageClassification.from_pretrained(
    model_name,
    ignore_mismatched_sizes=True,
    num_labels=2,
)

lora_model = PeftModel.from_pretrained(model, adapter)

img = Image.open("sample.png")
pimg = val_transforms(img.convert("RGB"))
batch = pimg.unsqueeze(0)
op = lora_model(batch)
vals = op.logits.tolist()[0]

if vals[0] > vals[1]:
    return "Predicted unaffected"
else:
    return "Predicted affected to some degree"
```

## Future goals

- More documentation
- Modify loss for regression on 0-4 score