rtik007 commited on
Commit
e4e9402
·
verified ·
1 Parent(s): 3eab27e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -8,38 +8,41 @@ import gradio as gr
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
  model = models.densenet121(pretrained=True)
10
 
11
- # Modify the classifier layer to output probabilities for 14 classes (14 pathologies)
12
  num_classes = 14
13
- model.classifier = nn.Sequential(
14
- nn.Linear(model.classifier.in_features, num_classes),
15
- nn.Sigmoid() # Use Sigmoid for multi-label classification
16
  )
17
- model.load_state_dict(torch.load('chexnet.pth', map_location=device)) # Load your pre-trained weights
18
- model = model.to(device)
 
 
 
 
19
  model.eval()
20
 
21
  # Define image transformations (resize, normalize)
22
  transform = transforms.Compose([
23
  transforms.Resize((224, 224)),
24
  transforms.ToTensor(),
25
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
26
  ])
27
 
28
  # Class names for the 14 diseases (labels from ChestX-ray14 dataset)
29
  class_names = [
30
- 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass',
31
- 'Nodule', 'Pneumonia', 'Pneumothorax', 'Consolidation', 'Edema',
32
  'Emphysema', 'Fibrosis', 'Pleural Thickening', 'Hernia'
33
  ]
34
 
35
  # Prediction function
36
  def predict_disease(image):
37
  image = transform(image).unsqueeze(0).to(device) # Transform and add batch dimension
 
38
  with torch.no_grad():
39
  outputs = model(image)
40
  outputs = outputs.cpu().numpy().flatten()
41
-
42
- # Create a dictionary of disease probabilities
43
  result = {class_name: float(prob) for class_name, prob in zip(class_names, outputs)}
44
  return result
45
 
@@ -49,9 +52,9 @@ interface = gr.Interface(
49
  inputs=gr.inputs.Image(type='pil'), # Input is an image
50
  outputs="label", # Output is a dictionary of labels with probabilities
51
  title="CheXNet Pneumonia Detection",
52
- description="Upload a chest X-ray to detect the probability of 14 different diseases."
53
  )
54
 
55
  # Launch the Gradio app
56
  if __name__ == "__main__":
57
- interface.launch()
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
  model = models.densenet121(pretrained=True)
10
 
11
+ # Modify the classifier layer to output probabilities for 14 classes (pathologies)
12
  num_classes = 14
13
+ model.fc = nn.Sequential(
14
+ nn.Linear(model.fc.in_features, num_classes),
15
+ nn.Sigmoid(), # Use Sigmoid for multi-label classification
16
  )
17
+
18
+ try:
19
+ model.load_state_dict(torch.load('chexnet.pth', map_location=device))
20
+ except Exception as e:
21
+ print(f"Error loading pre-trained weights: {e}")
22
+ model.to(device)
23
  model.eval()
24
 
25
  # Define image transformations (resize, normalize)
26
  transform = transforms.Compose([
27
  transforms.Resize((224, 224)),
28
  transforms.ToTensor(),
29
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
30
  ])
31
 
32
  # Class names for the 14 diseases (labels from ChestX-ray14 dataset)
33
  class_names = [
34
+ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass',
35
+ 'Nodule', 'Pneumonia', 'Pneumothorax', 'Consolidation', 'Edema',
36
  'Emphysema', 'Fibrosis', 'Pleural Thickening', 'Hernia'
37
  ]
38
 
39
  # Prediction function
40
  def predict_disease(image):
41
  image = transform(image).unsqueeze(0).to(device) # Transform and add batch dimension
42
+
43
  with torch.no_grad():
44
  outputs = model(image)
45
  outputs = outputs.cpu().numpy().flatten()
 
 
46
  result = {class_name: float(prob) for class_name, prob in zip(class_names, outputs)}
47
  return result
48
 
 
52
  inputs=gr.inputs.Image(type='pil'), # Input is an image
53
  outputs="label", # Output is a dictionary of labels with probabilities
54
  title="CheXNet Pneumonia Detection",
55
+ description="Upload a chest X-ray to detect the probability of 14 different diseases.",
56
  )
57
 
58
  # Launch the Gradio app
59
  if __name__ == "__main__":
60
+ interface.launch()