Spaces:
Sleeping

Ehsanul75 commited on
Commit
2f778be
·
verified ·
1 Parent(s): 71047d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -37
app.py CHANGED
@@ -1,29 +1,17 @@
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
4
- from torchvision import models, transforms
 
5
  from PIL import Image
6
 
7
- # Set device
8
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
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
- # Image preprocessing (test-time transform)
19
- transform = transforms.Compose([
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 model function
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, len(class_names))
35
  )
36
- model.load_state_dict(torch.load("densenet.pth", map_location=device))
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 predict(image):
46
- image = image.convert("RGB")
47
- image = transform(image).unsqueeze(0).to(device)
48
  with torch.no_grad():
49
- outputs = model(image)
50
- _, predicted = torch.max(outputs, 1)
51
  return class_names[predicted.item()]
52
 
53
- # Create Gradio interface
54
  interface = gr.Interface(
55
- fn=predict,
56
- inputs=gr.Image(type="pil"),
57
- outputs="text",
58
- title="ECG Classifier with DenseNet201",
59
- description="Upload an ECG image to classify it into one of four categories: History of MI, Myocardial Infarction Patients, Abnormal heartbeat, or Normal Person."
60
  )
61
 
62
- if __name__ == "__main__":
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()