-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
50 lines (40 loc) · 1.82 KB
/
inference.py
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
import torch
import json
from transformers import AutoModelForImageClassification, AutoImageProcessor
from peft import PeftModel
with open('human_action_labels.json', 'r') as f1, open('food_labels.json', 'r') as f2:
human_action_labels = json.load(f1)
food_labels = json.load(f2)
human_action_labels = {int(k):v for k, v in human_action_labels.items()}
food_labels = {int(k):v for k, v in food_labels.items()}
human_action_ids = {v:k for k, v in human_action_labels.items()}
food_ids = {v:k for k, v in food_labels.items()}
def inference_model(adaptor):
model = None
image_processor = AutoImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k', use_fast=True)
if adaptor == 'Food':
base_model = AutoModelForImageClassification.from_pretrained(
'google/vit-base-patch16-224-in21k',
label2id=food_ids,
id2label=food_labels,
ignore_mismatched_sizes = True
)
model = PeftModel.from_pretrained(base_model, 'lora-food')
# image_processor = AutoImageProcessor.from_pretrained('lora-food')
else:
base_model = AutoModelForImageClassification.from_pretrained(
'google/vit-base-patch16-224-in21k',
label2id=human_action_ids,
id2label=human_action_labels,
ignore_mismatched_sizes=True
)
model = PeftModel.from_pretrained(base_model, 'lora-human')
# image_processor = AutoImageProcessor.from_pretrained('lora-human')
return model, image_processor
def predict_image_class(image, model, image_processor):
encoding = image_processor(image.convert("RGB"), return_tensors="pt")
with torch.no_grad():
outputs = model(**encoding)
logits = outputs.logits
class_index = logits.argmax(-1).item()
return model.config.id2label[class_index]