srijaydeshpande commited on
Commit
e1e82d3
·
verified ·
1 Parent(s): 27294d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -21,6 +21,17 @@ model_dir = snapshot_download(
21
  repo_id="srijaydeshpande/spadesegresnet"
22
  )
23
 
 
 
 
 
 
 
 
 
 
 
 
24
  class SPADE(nn.Module):
25
  def __init__(self, norm_nc, label_nc, norm):
26
  super().__init__()
@@ -194,12 +205,12 @@ def generate_colors(n):
194
  return colors
195
 
196
  def generate_colored_image(labels):
197
- colors = generate_colors(6)
198
  w, h = labels.shape
199
  new_mk = np.empty([w, h, 3])
200
  for i in range(0,w):
201
  for j in range(0,h):
202
- new_mk[i][j] = colors[labels[i][j]]
203
  new_mk = new_mk.astype(np.uint8)
204
  return Image.fromarray(new_mk)
205
 
@@ -239,11 +250,10 @@ def segment_image(image):
239
  pred_labels = np.argmax(pred_labels_probs, axis=1)
240
  pred_labels = pred_labels[0]
241
  image = generate_colored_image(pred_labels)
242
- class_labels = ['tumor', 'stroma', 'inflammatory', 'necrosis', 'others']
243
  pixels_counts = []
244
  total=0
245
  print(np.unique(pred_labels))
246
- for i in range(1,len(class_labels)+1):
247
  current_count=np.sum(pred_labels == i)
248
  pixels_counts.append(current_count)
249
  total+=current_count
@@ -251,7 +261,7 @@ def segment_image(image):
251
  print(pixels_counts)
252
  plt.figure(figsize=(10, 6))
253
  bar_width = 0.15
254
- plt.bar(class_labels, pixels_counts, color='blue', width=bar_width)
255
  plt.xticks(rotation=45, ha='right')
256
  plt.xlabel('Tissue types', fontsize=17)
257
  plt.ylabel('Class Percentage', fontsize=17)
@@ -264,11 +274,11 @@ def segment_image(image):
264
  temp_filename = tmpfile.name
265
  stats = Image.open(temp_filename)
266
 
267
- legend = Image.open('legend.png')
268
 
269
  superimposed_image = superimpose_images(img, image)
270
 
271
- return image, legend, stats, superimposed_image
272
 
273
  def superimpose_images(image1, image2):
274
 
@@ -307,7 +317,6 @@ with gr.Row():
307
 
308
  # Second column: Remaining three outputs
309
  with gr.Column():
310
- output2 = gr.Image(label="Legend") # Second output
311
  output3 = gr.Image(label="Statistics") # Third output
312
  output4 = gr.Image(label="Superimposed Map") # Fourth output
313
 
@@ -316,7 +325,7 @@ demo = gr.Interface(
316
  segment_image,
317
  inputs=input_image,
318
  examples=examples,
319
- outputs=[output1, output2, output3, output4],
320
  title="Breast Cancer Semantic Segmentation"
321
  )
322
 
 
21
  repo_id="srijaydeshpande/spadesegresnet"
22
  )
23
 
24
+ color_map = {
25
+ 'outside_roi' : (255, 255, 255) # white
26
+ 'tumor' : (255, 0, 0) # red
27
+ 'stroma' : (0, 0, 255) # blue
28
+ 'inflammatory' : (0, 255, 0) # green
29
+ 'necrosis' : (255, 255, 0) # yello
30
+ 'others' : (8, 133, 161) # cyan
31
+ }
32
+ class_labels = ['outside_roi', 'tumor', 'stroma', 'inflammatory', 'necrosis', 'others']
33
+ colors = ['white', 'red', 'blue', 'green', 'yello', 'cyan']
34
+
35
  class SPADE(nn.Module):
36
  def __init__(self, norm_nc, label_nc, norm):
37
  super().__init__()
 
205
  return colors
206
 
207
  def generate_colored_image(labels):
208
+ # colors = generate_colors(6)
209
  w, h = labels.shape
210
  new_mk = np.empty([w, h, 3])
211
  for i in range(0,w):
212
  for j in range(0,h):
213
+ new_mk[i][j] = color_map[class_labels[labels[i][j]]]
214
  new_mk = new_mk.astype(np.uint8)
215
  return Image.fromarray(new_mk)
216
 
 
250
  pred_labels = np.argmax(pred_labels_probs, axis=1)
251
  pred_labels = pred_labels[0]
252
  image = generate_colored_image(pred_labels)
 
253
  pixels_counts = []
254
  total=0
255
  print(np.unique(pred_labels))
256
+ for i in range(1,len(class_labels)):
257
  current_count=np.sum(pred_labels == i)
258
  pixels_counts.append(current_count)
259
  total+=current_count
 
261
  print(pixels_counts)
262
  plt.figure(figsize=(10, 6))
263
  bar_width = 0.15
264
+ plt.bar(class_labels[1:], pixels_counts, color=colors[1:], width=bar_width)
265
  plt.xticks(rotation=45, ha='right')
266
  plt.xlabel('Tissue types', fontsize=17)
267
  plt.ylabel('Class Percentage', fontsize=17)
 
274
  temp_filename = tmpfile.name
275
  stats = Image.open(temp_filename)
276
 
277
+ # legend = Image.open('legend.png')
278
 
279
  superimposed_image = superimpose_images(img, image)
280
 
281
+ return image, stats, superimposed_image
282
 
283
  def superimpose_images(image1, image2):
284
 
 
317
 
318
  # Second column: Remaining three outputs
319
  with gr.Column():
 
320
  output3 = gr.Image(label="Statistics") # Third output
321
  output4 = gr.Image(label="Superimposed Map") # Fourth output
322
 
 
325
  segment_image,
326
  inputs=input_image,
327
  examples=examples,
328
+ outputs=[output1, output3, output4],
329
  title="Breast Cancer Semantic Segmentation"
330
  )
331