DavidNguyen commited on
Commit
60c63c4
1 Parent(s): 6ac5fa2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import torch
3
+ import gradio as gr
4
+ import skimage
5
+ from model import FullModel
6
+ from albumentations.pytorch import ToTensorV2
7
+ from albumentations import *
8
+ PATH = r"C:\Users\huumi\Desktop\Subjects\HK8\DOAN_THUCTAP\BreastCancer\July_convnext_labelsmoothing=0.1-benign-0-224_labelsmoothing_300rsna_b16.pt"
9
+
10
+
11
+ transform = Compose([Resize(height=224,width=224,always_apply=True),
12
+ ToTensorV2(),
13
+ ])
14
+
15
+ def load_model(PATH):
16
+ model = FullModel()
17
+ PATH = PATH
18
+ model.load_state_dict(torch.load(PATH))
19
+ return model
20
+
21
+ model = load_model(PATH)
22
+
23
+ def read_img(img):
24
+ # print(path_img.shape)
25
+ # img = cv2.imread(path_img, 1)
26
+ img_trans = transform(image=img)['image']
27
+ img_trans = img_trans.unsqueeze(0).float()
28
+ return img_trans
29
+ # print(img_trans.unsqueeze(0).shape)
30
+ # print(model(img_trans.unsqueeze(0).float()))
31
+
32
+ def predict(img):
33
+ result = "Malignant"
34
+ images = read_img(img)
35
+ print(images.shape)
36
+ # images = images.reshape((-1, 3, 224, 224))
37
+ # images = torch.from_numpy(images)#.permute(0, 2, 3, 1)
38
+ outputs= model(images)
39
+ print(outputs)
40
+ _, predicted = torch.max(outputs, 1)
41
+ if(predicted.item() == 0):
42
+ result = "Normal/Benign"
43
+
44
+ return result
45
+
46
+
47
+ title = "Breast cancer detection with Deep Learning (ConvNext)"
48
+ description = "<p style='text-align: center'><b>As a radiologist or oncologist, it is crucial to know what is wrong with a breast x-ray image.<b><br><b>Upload the breast X-ray image to know what is wrong with a patients breast with or without inplant<b><p>"
49
+ article="<p style='text-align: center'>Web app is built and managed by IUH team></p>"
50
+ examples = [r'C:\Users\huumi\Desktop\Subjects\HK8\DOAN_THUCTAP\BreastCancer\D1-0001_1-1.png',
51
+ r'C:\Users\huumi\Desktop\Subjects\HK8\DOAN_THUCTAP\BreastCancer\D1-0005_1-2.png',
52
+ r'C:\Users\huumi\Desktop\Subjects\HK8\DOAN_THUCTAP\BreastCancer\mdb028.png']
53
+ enable_queue=True
54
+ #interpretation='default'
55
+
56
+ gr.Interface(fn=predict,
57
+ inputs=gr.Image(label="Image (png file)"),
58
+ outputs='text',
59
+ title=title,
60
+ description=description,
61
+ article=article,
62
+ examples=examples,
63
+ enable_queue=enable_queue
64
+ ).launch(share=True)