Sijuade commited on
Commit
0efbb53
1 Parent(s): 31b9437

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -61,21 +61,21 @@ def upload_image_inference(input_img, n_top_classes, transparency):
61
  return([confidences, [org_img, grayscale_cam, visualization]])
62
 
63
 
64
- def misclass_gr(num_images, layer_val, transparency):
65
  images_list = misclassified_data[:num_images]
66
 
67
- images_list = [image_to_array(img, layer_val, transparency) for img in images_list]
68
  return(images_list)
69
 
70
 
71
- def class_gr(num_images, layer_val, transparency):
72
  images_list = classified_data[:num_images]
73
 
74
- images_list = [image_to_array(img, layer_val, transparency) for img in images_list]
75
  return(images_list)
76
 
77
 
78
- def image_to_array(input_img, layer_val, transparency=0.6):
79
  input_tensor = input_img[0]
80
 
81
  cam = GradCAM(model=model, target_layers=[model.res_block2.conv[-layer_val]])
@@ -90,7 +90,11 @@ def image_to_array(input_img, layer_val, transparency=0.6):
90
  visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True,
91
  image_weight=transparency)
92
 
93
- plt.imshow(visualization)
 
 
 
 
94
  plt.title(r"Correct: " + classes[input_img[1].item()] + '\n' + 'Output: ' + classes[input_img[2].item()])
95
 
96
  with io.BytesIO() as buffer:
@@ -174,8 +178,9 @@ with gr.Blocks() as gradcam:
174
  with gr.Row():
175
  with gr.Column():
176
  cam_input21 = [gr.Number(minimum=1, maximum=20, precision=0, value=3, label='View Correctly Classified CAM | Num Images'),
177
- gr.Number(minimum=1, maximum=3, precision=0, value=1, label='(-) Target Layer'),
178
- gr.Slider(0, 1, value=0.6, label='Transparency')]
 
179
 
180
  image_output21 = gr.Gallery(label="Images - Grad-CAM (correct)",
181
  show_label=True, min_width=80)
@@ -183,8 +188,9 @@ with gr.Blocks() as gradcam:
183
 
184
  with gr.Column():
185
  cam_input22 = [gr.Number(minimum=1, maximum=20, precision=0, value=3, label='View Misclassified CAM | Num Images'),
186
- gr.Number(minimum=1, maximum=3, precision=0, value=1, label='(-) Target Layer'),
187
- gr.Slider(0, 1, value=0.6, label='Transparency')]
 
188
 
189
  image_output22 = gr.Gallery(label="Images - Grad-CAM (Misclassified)",
190
  show_label=True, min_width=80)
 
61
  return([confidences, [org_img, grayscale_cam, visualization]])
62
 
63
 
64
+ def misclass_gr(num_images, layer_val, transparency, isview):
65
  images_list = misclassified_data[:num_images]
66
 
67
+ images_list = [image_to_array(img, layer_val, isview, transparency) for img in images_list]
68
  return(images_list)
69
 
70
 
71
+ def class_gr(num_images, layer_val, transparency, isview):
72
  images_list = classified_data[:num_images]
73
 
74
+ images_list = [image_to_array(img, layer_val, isview, transparency) for img in images_list]
75
  return(images_list)
76
 
77
 
78
+ def image_to_array(input_img, layer_val, isview, transparency=0.6):
79
  input_tensor = input_img[0]
80
 
81
  cam = GradCAM(model=model, target_layers=[model.res_block2.conv[-layer_val]])
 
90
  visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True,
91
  image_weight=transparency)
92
 
93
+ if isview:
94
+ plt.imshow(visualization)
95
+ else:
96
+ plt.imshow(rgb_img)
97
+
98
  plt.title(r"Correct: " + classes[input_img[1].item()] + '\n' + 'Output: ' + classes[input_img[2].item()])
99
 
100
  with io.BytesIO() as buffer:
 
178
  with gr.Row():
179
  with gr.Column():
180
  cam_input21 = [gr.Number(minimum=1, maximum=20, precision=0, value=3, label='View Correctly Classified CAM | Num Images'),
181
+ gr.Number(minimum=1, maximum=3, precision=0, value=1, label='(-) Target Layer'),
182
+ gr.Slider(0, 1, value=0.6, label='Transparency'),
183
+ gr.Checkbox(label="GradCAM", value=True, info="View GradCAM ?")]
184
 
185
  image_output21 = gr.Gallery(label="Images - Grad-CAM (correct)",
186
  show_label=True, min_width=80)
 
188
 
189
  with gr.Column():
190
  cam_input22 = [gr.Number(minimum=1, maximum=20, precision=0, value=3, label='View Misclassified CAM | Num Images'),
191
+ gr.Number(minimum=1, maximum=3, precision=0, value=1, label='(-) Target Layer'),
192
+ gr.Slider(0, 1, value=0.6, label='Transparency'),
193
+ gr.Checkbox(label="GradCAM", value=True, info="View GradCAM ?")]
194
 
195
  image_output22 = gr.Gallery(label="Images - Grad-CAM (Misclassified)",
196
  show_label=True, min_width=80)