-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (46 loc) · 1.92 KB
/
app.py
File metadata and controls
59 lines (46 loc) · 1.92 KB
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
import gradio as gr
import torch
import torchvision
from torchvision import transforms
import matplotlib.pyplot as plt
from vit_class import ViT
from typing import List, Tuple
from PIL import Image
class_names = ['jerry', 'tom']
image_size = (224, 224)
model_save_path = "vit_model.pth"
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
vit = ViT(num_classes=len(class_names))
checkpoint = torch.load(model_save_path, map_location=device)
vit.load_state_dict(checkpoint["model_state_dict"])
vit.eval()
vit.to(device)
def predict(image):
# return "Hello", "Hello"
if image is None:
return "No image captured", "0.0"
image_transform = transforms.Compose(
[
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
with torch.inference_mode():
# Transform and add an extra dimension to image (model requires samples in [batch_size, color_channels, height, width])
transformed_image = image_transform(image).unsqueeze(dim=0)
# Make a prediction on image with an extra dimension and send it to the target device
target_image_pred = vit(transformed_image.to(device))
# Convert logits -> prediction probabilities (using torch.softmax() for multi-class classification)
target_image_pred_probs = torch.softmax(target_image_pred, dim=1)
# Convert prediction probabilities -> prediction labels
target_image_pred_label = torch.argmax(target_image_pred_probs, dim=1)
return class_names[target_image_pred_label], (target_image_pred_probs.max().item())
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Input Image"),
outputs=[gr.Textbox(label="Character", lines=1), gr.Textbox(label="Probability", lines=1)],
)
demo.launch(share=True)