Spaces:
Runtime error
Runtime error
init
Browse files- app.py +58 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import timm
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
from torchvision import datasets, transforms
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
from torch.utils.mobile_optimizer import optimize_for_mobile
|
9 |
+
|
10 |
+
model = timm.create_model('vit_base_patch16_224', pretrained=True)
|
11 |
+
model.head = torch.nn.Linear(in_features=model.head.in_features, out_features=5)
|
12 |
+
|
13 |
+
path = "opt_model.pt"
|
14 |
+
|
15 |
+
model = model.jit.load(path)
|
16 |
+
|
17 |
+
model.eval()
|
18 |
+
|
19 |
+
def transform_image(img_sample):
|
20 |
+
transform = transforms.Compose([
|
21 |
+
transforms.Resize((224, 224)), # Resize to 224x224
|
22 |
+
transforms.ToTensor(), # Convert PIL image to tensor
|
23 |
+
transforms.ColorJitter(contrast=0.5), # Contrast
|
24 |
+
transforms.RandomAdjustSharpness(sharpness_factor=0.5),
|
25 |
+
transforms.RandomSolarize(threshold=0.75),
|
26 |
+
transforms.RandomAutocontrast(p=1),
|
27 |
+
])
|
28 |
+
img = Image.open(img_sample)
|
29 |
+
transformed_img = transform(img)
|
30 |
+
return transformed_img
|
31 |
+
|
32 |
+
def predict(Image):
|
33 |
+
model.eval()
|
34 |
+
tranformed_img = transform_image(Image)
|
35 |
+
img = torch.from_numpy(tranformed_img)
|
36 |
+
|
37 |
+
with torch.no_grad():
|
38 |
+
grade = torch.softmax(model(img.float()), dim=1)[0]
|
39 |
+
category = ["None", "Mild", "Moderate", "Severe", "Proliferative"]
|
40 |
+
output_dict = {}
|
41 |
+
for cat, value in zip(category, grade):
|
42 |
+
output_dict[cat] = value.item()
|
43 |
+
return output_dict
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
image = gr.Image(shape=(224, 224), image_mode="RGB")
|
48 |
+
label = gr.Label(label="Grade")
|
49 |
+
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn=predict,
|
52 |
+
inputs=image,
|
53 |
+
outputs=label,
|
54 |
+
examples=["examples/0.png", "examples/1.png", "examples/2.png", "examples/3.png", "examples/4.png"]
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
omegaconf
|
2 |
+
timm
|
3 |
+
torch
|