e-colombo commited on
Commit
4f7b9da
·
1 Parent(s): fb54b5e

New app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from src.model import get_model, apply_weights, copy_weight
5
+ from src.transform import crop, pad, gpu_crop
6
+ from torchvision.transforms import Normalize, ToTensor
7
+ from pathlib import Path
8
+
9
+ vocab = [
10
+ "Actinic Keratosis",
11
+ "Basal Cell Carcinoma",
12
+ "Benign Keratosis",
13
+ "Dermatofibroma",
14
+ "Melanoma",
15
+ "Melanocytic Nevus",
16
+ "Vascular Lesion",
17
+ ]
18
+
19
+
20
+ model = get_model()
21
+ state = torch.load("exported_model.pth", map_location="cpu")
22
+ apply_weights(model, state, copy_weight)
23
+
24
+ to_tensor = ToTensor()
25
+ norm = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
26
+
27
+
28
+ def classify_image(inp):
29
+ inp = Image.fromarray(inp)
30
+ transformed_input = pad(crop(inp, (460, 460)), (460, 460))
31
+ transformed_input = to_tensor(transformed_input).unsqueeze(0)
32
+ transformed_input = gpu_crop(transformed_input, (224, 224))
33
+ transformed_input = norm(transformed_input)
34
+ model.eval()
35
+ with torch.no_grad():
36
+ pred = model(transformed_input)
37
+ prob = torch.softmax(pred[0], dim=0)
38
+ confidences = {vocab[i]: float(prob[i]) for i in range(7)}
39
+ return confidences
40
+
41
+
42
+ iface = gr.Interface(
43
+ fn=classify_image,
44
+ inputs="image",
45
+ outputs=gr.Label(),
46
+ examples=[
47
+ ["ISIC_0024634_00.jpg"],
48
+ ["ISIC_0032932_00.jpg"],
49
+ ],
50
+ title="Skin Lesion Recognition using fast.ai",
51
+ description="Adapted from https://domingomery.ing.puc.cl/",
52
+ article="<p style='text-align: center'><a href='https://evertoncolombo.github.io/blog/posts/skin-lesion/Skin%20Lesion%20Recognition%20using%20fastai.html'>More info | <a href='https://www.dropbox.com/s/nzrvuoos7sgl5dh/exp4val.zip' >Dataset</a> <center><img src='https://visitor-badge.glitch.me/badge?page_id=e_colombo_skin_lesion' alt='visitor badge'></center></p>",
53
+ allow_flagging="never",
54
+ ).launch()