Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,62 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
3 |
from PIL import Image
|
4 |
-
|
5 |
|
6 |
# Set the device
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
|
|
|
|
|
|
|
9 |
# Load the model
|
10 |
-
|
11 |
-
model =
|
12 |
-
model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
transforms.ToTensor(),
|
18 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
19 |
-
])
|
20 |
|
|
|
21 |
def predict(image):
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
with torch.no_grad():
|
26 |
-
output = model(
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
32 |
fn=predict,
|
33 |
-
inputs="
|
34 |
-
outputs=
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
)
|
38 |
|
39 |
-
# Launch the
|
40 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from torchvision import models, transforms
|
4 |
from PIL import Image
|
5 |
+
|
6 |
|
7 |
# Set the device
|
8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
|
10 |
+
# Define the class names
|
11 |
+
class_names = ['COVID', 'Lung_Opacity', 'No_Tumor', 'Normal', 'Tumor', 'Viral_Pneumonia']
|
12 |
+
|
13 |
# Load the model
|
14 |
+
def load_model(model_path, num_classes):
|
15 |
+
model = models.efficientnet_b0(weights=None)
|
16 |
+
model.classifier = torch.nn.Sequential(
|
17 |
+
torch.nn.Dropout(p=0.2, inplace=True),
|
18 |
+
torch.nn.Linear(in_features=1280, out_features=num_classes, bias=True)
|
19 |
+
)
|
20 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
21 |
+
model.eval()
|
22 |
+
return model
|
23 |
+
|
24 |
|
25 |
+
model_path = 'transfer_balanced_learning_model.pth'
|
26 |
+
num_classes = len(class_names)
|
27 |
+
model = load_model(model_path, num_classes)
|
|
|
|
|
|
|
28 |
|
29 |
+
# Function to make predictions
|
30 |
def predict(image):
|
31 |
+
preprocess = transforms.Compose([
|
32 |
+
transforms.Resize(256),
|
33 |
+
transforms.CenterCrop(224),
|
34 |
+
transforms.ToTensor(),
|
35 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
36 |
+
])
|
37 |
+
image = preprocess(image)
|
38 |
+
input_batch = image.unsqueeze(0)
|
39 |
+
|
40 |
with torch.no_grad():
|
41 |
+
output = model(input_batch)
|
42 |
+
|
43 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
44 |
+
_, predicted_idx = torch.max(output, 1)
|
45 |
+
predicted_label = class_names[predicted_idx.item()]
|
46 |
+
|
47 |
+
return {class_names[i]: float(prob) for i, prob in enumerate(probabilities)}, predicted_label
|
48 |
|
49 |
+
# Create the Gradio interface
|
50 |
+
iface = gr.Interface(
|
51 |
fn=predict,
|
52 |
+
inputs=gr.Image(type="pil"),
|
53 |
+
outputs=[
|
54 |
+
gr.Label(num_top_classes=len(class_names)),
|
55 |
+
gr.Label(label="Predicted Class")
|
56 |
+
],
|
57 |
+
title="Medical Image Classification",
|
58 |
+
description="Upload a medical image to classify it into one of the following categories: COVID, Lung Opacity, No Tumor, Normal, Tumor, or Viral Pneumonia."
|
59 |
)
|
60 |
|
61 |
+
# Launch the interface
|
62 |
+
iface.launch()
|