dusaurabh commited on
Commit
98b2ef6
1 Parent(s): f747a8c

added to give user to pass input whether they want to see grad cam output images or not

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -39,7 +39,7 @@ def resize_image_pil(image, new_width, new_height):
39
 
40
  return resized
41
 
42
- def inference(input_img, transparency = 0.5, target_layer_number = -1):
43
  input_img = resize_image_pil(input_img, 32, 32)
44
 
45
  input_img = np.array(input_img)
@@ -61,8 +61,11 @@ def inference(input_img, transparency = 0.5, target_layer_number = -1):
61
  img = input_img.squeeze(0)
62
  img = inv_normalize(img)
63
  print(transparency)
64
- visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
65
- return classes[prediction[0].item()], visualization, confidences
 
 
 
66
 
67
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
68
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
@@ -71,9 +74,10 @@ examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1], ["bird_1.jpg", 0.5, -1],
71
  demo = gr.Interface(
72
  inference,
73
  inputs = [
74
- gr.Image(width=256, height=256, label="Input Image"), gr.Slider
75
- (0, 1, value = 0.5, label="Overall Opacity of Image"),
76
- gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?")
 
77
  ],
78
  outputs = [
79
  "text",
 
39
 
40
  return resized
41
 
42
+ def inference(input_img, transparency = 0.5, target_layer_number = -1, grad_cam_option = "Yes"):
43
  input_img = resize_image_pil(input_img, 32, 32)
44
 
45
  input_img = np.array(input_img)
 
61
  img = input_img.squeeze(0)
62
  img = inv_normalize(img)
63
  print(transparency)
64
+ if grad_cam_option == "Yes":
65
+ visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
66
+ return classes[prediction[0].item()], visualization, confidences
67
+ else:
68
+ return classes[prediction[0].item()], confidences
69
 
70
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
71
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
 
74
  demo = gr.Interface(
75
  inference,
76
  inputs = [
77
+ gr.Image(width=256, height=256, label="Input Image"),
78
+ gr.Slider(0, 1, value = 0.5, label="Overall Opacity of Image"),
79
+ gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?"),
80
+ gr.Dropdown([“Yes”, “No”], label="Want to see Grad Cam Images?")
81
  ],
82
  outputs = [
83
  "text",