Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
-
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Class names in correct order (based on ImageFolder class index mapping)
|
| 11 |
-
class_names = [
|
| 12 |
-
"History of MI",
|
| 13 |
-
"Myocardial Infarction Patients",
|
| 14 |
-
"Abnormal heartbeat",
|
| 15 |
-
"Normal Person"
|
| 16 |
-
]
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
transforms.Resize((224, 224)),
|
| 21 |
-
transforms.ToTensor(),
|
| 22 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 23 |
-
std=[0.229, 0.224, 0.225])
|
| 24 |
-
])
|
| 25 |
|
| 26 |
-
# Load
|
| 27 |
def load_model():
|
| 28 |
model = models.densenet201(weights=None)
|
| 29 |
in_features = model.classifier.in_features
|
|
@@ -31,33 +19,38 @@ def load_model():
|
|
| 31 |
nn.Linear(in_features, 512),
|
| 32 |
nn.ReLU(),
|
| 33 |
nn.Dropout(0.4),
|
| 34 |
-
nn.Linear(512,
|
| 35 |
)
|
| 36 |
-
model.load_state_dict(torch.load("
|
| 37 |
-
model.to(device)
|
| 38 |
model.eval()
|
| 39 |
-
return model
|
| 40 |
|
| 41 |
-
# Load model once
|
| 42 |
model = load_model()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Prediction function
|
| 45 |
-
def
|
| 46 |
-
image = image.convert("RGB")
|
| 47 |
-
|
| 48 |
with torch.no_grad():
|
| 49 |
-
|
| 50 |
-
_, predicted = torch.max(
|
| 51 |
return class_names[predicted.item()]
|
| 52 |
|
| 53 |
-
#
|
| 54 |
interface = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
-
inputs=gr.Image(type="
|
| 57 |
-
outputs=
|
| 58 |
-
title="ECG
|
| 59 |
-
description="Upload an ECG image to classify it into one of four categories
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from torchvision import models
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
# Class labels
|
| 9 |
+
class_names = ['History of MI', 'Myocardial Infarction Patients', 'Abnormal heartbeat', 'Normal Person']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Device
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Load the model
|
| 15 |
def load_model():
|
| 16 |
model = models.densenet201(weights=None)
|
| 17 |
in_features = model.classifier.in_features
|
|
|
|
| 19 |
nn.Linear(in_features, 512),
|
| 20 |
nn.ReLU(),
|
| 21 |
nn.Dropout(0.4),
|
| 22 |
+
nn.Linear(512, 4)
|
| 23 |
)
|
| 24 |
+
model.load_state_dict(torch.load("best_model.pth", map_location=device))
|
|
|
|
| 25 |
model.eval()
|
| 26 |
+
return model.to(device)
|
| 27 |
|
|
|
|
| 28 |
model = load_model()
|
| 29 |
|
| 30 |
+
# Preprocessing
|
| 31 |
+
transform = transforms.Compose([
|
| 32 |
+
transforms.Resize((224, 224)),
|
| 33 |
+
transforms.ToTensor(),
|
| 34 |
+
transforms.Normalize([0.485, 0.456, 0.406],
|
| 35 |
+
[0.229, 0.224, 0.225])
|
| 36 |
+
])
|
| 37 |
+
|
| 38 |
# Prediction function
|
| 39 |
+
def predict_ecg(image):
|
| 40 |
+
image = Image.fromarray(image).convert("RGB")
|
| 41 |
+
image_tensor = transform(image).unsqueeze(0).to(device)
|
| 42 |
with torch.no_grad():
|
| 43 |
+
output = model(image_tensor)
|
| 44 |
+
_, predicted = torch.max(output, 1)
|
| 45 |
return class_names[predicted.item()]
|
| 46 |
|
| 47 |
+
# Gradio Interface
|
| 48 |
interface = gr.Interface(
|
| 49 |
+
fn=predict_ecg,
|
| 50 |
+
inputs=gr.Image(type="numpy", label="Upload ECG Image"),
|
| 51 |
+
outputs=gr.Label(num_top_classes=4),
|
| 52 |
+
title="ECG Classification with DenseNet201",
|
| 53 |
+
description="Upload an ECG image to classify it into one of four categories."
|
| 54 |
)
|
| 55 |
|
| 56 |
+
interface.launch()
|
|
|