Hali5 commited on
Commit
bb06753
·
1 Parent(s): fadf1fb

split the functions

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -36,7 +36,7 @@ model.eval()
36
 
37
  tf = v2.Compose([
38
  v2.ToImage(),
39
- v2.Resize((64, 64)),
40
  v2.ToDtype(torch.float32, scale=True),
41
  ])
42
 
@@ -74,17 +74,26 @@ for i in range(number_of_examples):
74
  example_rows_val.append([val_file_path, val_truth_label_text])
75
 
76
  @spaces.GPU
77
- def predict(image,truth_labels=None):
78
  if image is None:
79
  return None
80
 
81
- if truth_labels:
82
- # already preprocessed test/val_samples
83
- img_tensor = torch.from_numpy(image).unsqueeze(0).to(device)
84
- else:
85
- # uplouded image
86
- pil_img = Image.fromarray(image.astype('uint8'), 'RGB')
87
- img_tensor = tf(pil_img).unsqueeze(0).to(device)
 
 
 
 
 
 
 
 
 
88
 
89
  with torch.no_grad():
90
  outputs = model(img_tensor)
@@ -101,11 +110,10 @@ custom_css = """
101
  }
102
  """
103
 
104
-
105
- with gradio.Blocks(css=custom_css) as demo:
106
  gradio.Markdown("# PathMNIST Image Classification")
107
 
108
- with gradio.Tab("Predict"):
109
  gradio.Markdown("## Upload a tissue image for classification")
110
  with gradio.Row():
111
  input_img = gradio.Image(height=512, width=512)
@@ -114,7 +122,7 @@ with gradio.Blocks(css=custom_css) as demo:
114
  btn = gradio.Button("Predict")
115
  btn.click(fn=predict, inputs=input_img, outputs=output_lbl)
116
 
117
- with gradio.Tab("Examples (Validation)"):
118
  gradio.Markdown("## Select an example below to test the model against the PathMNIST validation dataset")
119
 
120
  with gradio.Row():
@@ -127,11 +135,11 @@ with gradio.Blocks(css=custom_css) as demo:
127
  examples=example_rows_val,
128
  inputs=[input_img_ex, truth_box],
129
  outputs=output_lbl_ex,
130
- fn=predict,
131
  cache_examples=True,
132
  )
133
 
134
- with gradio.Tab("Examples (Test)"):
135
  gradio.Markdown("## Select an example below to test the model against the PathMNIST test dataset")
136
 
137
  with gradio.Row():
@@ -144,7 +152,7 @@ with gradio.Blocks(css=custom_css) as demo:
144
  examples=example_rows_test,
145
  inputs=[input_img_ex, truth_box],
146
  outputs=output_lbl_ex,
147
- fn=predict,
148
  cache_examples=True,
149
  )
150
 
 
36
 
37
  tf = v2.Compose([
38
  v2.ToImage(),
39
+ v2.Resize((64, 64), antialias=True),
40
  v2.ToDtype(torch.float32, scale=True),
41
  ])
42
 
 
74
  example_rows_val.append([val_file_path, val_truth_label_text])
75
 
76
  @spaces.GPU
77
+ def predict(image):
78
  if image is None:
79
  return None
80
 
81
+ # uplouded image
82
+ img_tensor = tf(image).unsqueeze(0).to(device)
83
+
84
+ with torch.no_grad():
85
+ outputs = model(img_tensor)
86
+ probabilities = torch.nn.functional.softmax(outputs.squeeze(0), dim=0)
87
+
88
+ return {LABELS[i]: float(probabilities[i]) for i in range(len(LABELS))}
89
+
90
+ @spaces.GPU
91
+ def sample_predict(image,truth_labels=None):
92
+ if image is None:
93
+ return None
94
+
95
+ # already preprocessed test/val_samples
96
+ img_tensor = torch.from_numpy(image.astype(numpy.float32)).unsqueeze(0).to(device)
97
 
98
  with torch.no_grad():
99
  outputs = model(img_tensor)
 
110
  }
111
  """
112
 
113
+ with gradio.Blocks() as demo:
 
114
  gradio.Markdown("# PathMNIST Image Classification")
115
 
116
+ with gradio.Tab("Predict",css=custom_css):
117
  gradio.Markdown("## Upload a tissue image for classification")
118
  with gradio.Row():
119
  input_img = gradio.Image(height=512, width=512)
 
122
  btn = gradio.Button("Predict")
123
  btn.click(fn=predict, inputs=input_img, outputs=output_lbl)
124
 
125
+ with gradio.Tab("Examples (Validation)",css=custom_css):
126
  gradio.Markdown("## Select an example below to test the model against the PathMNIST validation dataset")
127
 
128
  with gradio.Row():
 
135
  examples=example_rows_val,
136
  inputs=[input_img_ex, truth_box],
137
  outputs=output_lbl_ex,
138
+ fn=sample_predict,
139
  cache_examples=True,
140
  )
141
 
142
+ with gradio.Tab("Examples (Test)",css=custom_css):
143
  gradio.Markdown("## Select an example below to test the model against the PathMNIST test dataset")
144
 
145
  with gradio.Row():
 
152
  examples=example_rows_test,
153
  inputs=[input_img_ex, truth_box],
154
  outputs=output_lbl_ex,
155
+ fn=sample_predict,
156
  cache_examples=True,
157
  )
158