Chirag1994 commited on
Commit
422fb0d
1 Parent(s): 845deaa

demo.launch() fix

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -1,4 +1,4 @@
1
- ## Importing Libraries
2
  import os
3
  import torch
4
  import numpy as np
@@ -6,10 +6,11 @@ import gradio as gr
6
  from model import Model
7
  import albumentations as A
8
 
9
- ## Creating a model instance
10
- efficientnet_b5_model = Model()
11
- efficientnet_b5_model = torch.nn.DataParallel(efficientnet_b5_model) ## Must wrap our model in nn.DataParallel()
12
- ## if used multi-gpu's to train the model otherwise we would get state_dict keys mismatch error.
 
13
  efficientnet_b5_model.load_state_dict(
14
  torch.load(
15
  f='efficientnet_b5_checkpoint_fold_0.pt',
@@ -17,7 +18,9 @@ efficientnet_b5_model.load_state_dict(
17
  )
18
  )
19
 
20
- ## Predict on a single image
 
 
21
  def predict_on_single_image(img):
22
  """
23
  Function takes an image, transforms for
@@ -28,13 +31,13 @@ def predict_on_single_image(img):
28
  having melanoma.
29
  """
30
  img = np.array(img)
31
- transforms = A.Compose([A.Resize(512,512),
32
- A.Normalize(mean=(0.485, 0.456, 0.406),
33
- std=(0.229, 0.224, 0.225),
34
- max_pixel_value=255.0,
35
- always_apply=True
36
- )]
37
- )
38
  img = transforms(image=img)['image']
39
  image = np.transpose(img, (2, 0, 1)).astype(np.float32)
40
  image = torch.tensor(image, dtype=torch.float).unsqueeze(dim=0)
@@ -43,27 +46,28 @@ def predict_on_single_image(img):
43
  probs = torch.sigmoid(efficientnet_b5_model(image))
44
  prob_of_melanoma = probs[0].item()
45
  prob_of_not_having_melanoma = 1 - prob_of_melanoma
46
- pred_label = {"Probability of Having Melanoma": prob_of_melanoma,
47
- "Probability of Not having Melanoma": prob_of_not_having_melanoma}
48
  return pred_label
49
-
50
- ## Gradio App
51
- import gradio as gr
52
 
53
- ## Examples directory path
 
 
 
54
  melanoma_app_examples_path = "examples"
55
 
56
- ## Creating the title and description strings
57
  title = "Melanoma Cancer Detection App"
58
  description = 'An efficientnet-b5 model that predicts the probability of a patient having melanoma skin cancer or not.'
59
- example_list = [["examples/" + example] for example in os.listdir(melanoma_app_examples_path)]
 
60
 
61
- ## Create the Gradio demo
62
  demo = gr.Interface(fn=predict_on_single_image,
63
- inputs=gr.Image(type='pil'),
64
- outputs=[gr.Label(label='Probabilities')],
65
- examples=example_list, title=title,
66
- description=description)
67
 
68
- ## Launch the demo!
69
- demo.launch(debug=False, share=True)
 
1
+ # Importing Libraries
2
  import os
3
  import torch
4
  import numpy as np
 
6
  from model import Model
7
  import albumentations as A
8
 
9
+ # Creating a model instance
10
+ efficientnet_b5_model = Model()
11
+ efficientnet_b5_model = torch.nn.DataParallel(
12
+ efficientnet_b5_model) # Must wrap our model in nn.DataParallel()
13
+ # if used multi-gpu's to train the model otherwise we would get state_dict keys mismatch error.
14
  efficientnet_b5_model.load_state_dict(
15
  torch.load(
16
  f='efficientnet_b5_checkpoint_fold_0.pt',
 
18
  )
19
  )
20
 
21
+ # Predict on a single image
22
+
23
+
24
  def predict_on_single_image(img):
25
  """
26
  Function takes an image, transforms for
 
31
  having melanoma.
32
  """
33
  img = np.array(img)
34
+ transforms = A.Compose([A.Resize(512, 512),
35
+ A.Normalize(mean=(0.485, 0.456, 0.406),
36
+ std=(0.229, 0.224, 0.225),
37
+ max_pixel_value=255.0,
38
+ always_apply=True
39
+ )]
40
+ )
41
  img = transforms(image=img)['image']
42
  image = np.transpose(img, (2, 0, 1)).astype(np.float32)
43
  image = torch.tensor(image, dtype=torch.float).unsqueeze(dim=0)
 
46
  probs = torch.sigmoid(efficientnet_b5_model(image))
47
  prob_of_melanoma = probs[0].item()
48
  prob_of_not_having_melanoma = 1 - prob_of_melanoma
49
+ pred_label = {"Probability of Having Melanoma": prob_of_melanoma,
50
+ "Probability of Not having Melanoma": prob_of_not_having_melanoma}
51
  return pred_label
 
 
 
52
 
53
+
54
+ # Gradio App
55
+
56
+ # Examples directory path
57
  melanoma_app_examples_path = "examples"
58
 
59
+ # Creating the title and description strings
60
  title = "Melanoma Cancer Detection App"
61
  description = 'An efficientnet-b5 model that predicts the probability of a patient having melanoma skin cancer or not.'
62
+ example_list = [["examples/" + example]
63
+ for example in os.listdir(melanoma_app_examples_path)]
64
 
65
+ # Create the Gradio demo
66
  demo = gr.Interface(fn=predict_on_single_image,
67
+ inputs=gr.Image(type='pil'),
68
+ outputs=[gr.Label(label='Probabilities')],
69
+ examples=example_list, title=title,
70
+ description=description)
71
 
72
+ # Launch the demo!
73
+ demo.launch()