Spaces:
Sleeping
Sleeping
new app
Browse files
app.py
CHANGED
@@ -1,7 +1,43 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from torchvision import transforms
|
4 |
+
from architecture import ResNetLungCancer
|
5 |
import gradio as gr
|
6 |
|
7 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
8 |
|
9 |
+
model = ResNetLungCancer(num_classes=4)
|
10 |
+
model.load_state_dict(torch.load('Model/lung_cancer_detection_model.pth', map_location=device))
|
11 |
+
model = model.to(device)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
preprocess = transforms.Compose([
|
15 |
+
transforms.Resize(256),
|
16 |
+
transforms.CenterCrop(224),
|
17 |
+
transforms.ToTensor(),
|
18 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
19 |
+
])
|
20 |
+
|
21 |
+
class_names = ['Adenocarcinoma', 'Large Cell Carcinoma', 'Normal', 'Squamous Cell Carcinoma']
|
22 |
+
|
23 |
+
def predict(image):
|
24 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
25 |
+
input_tensor = preprocess(image).unsqueeze(0).to(device)
|
26 |
+
|
27 |
+
with torch.no_grad():
|
28 |
+
output = model(input_tensor)
|
29 |
+
|
30 |
+
predicted_class = torch.argmax(output, dim=1).item()
|
31 |
+
return class_names[predicted_class]
|
32 |
+
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=predict,
|
35 |
+
inputs=gr.Image(),
|
36 |
+
outputs=gr.Label(num_top_classes=1),
|
37 |
+
examples=[
|
38 |
+
["Data/test/large.cell.carcinoma/000108.png"],
|
39 |
+
["Data/test/normal/7 - Copy (3).png"]
|
40 |
+
]
|
41 |
+
)
|
42 |
+
|
43 |
+
iface.launch(share=True)
|