RashiAgarwal commited on
Commit
c5cadab
·
1 Parent(s): 426af82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -35,7 +35,7 @@ inv_normalize = T.Normalize(
35
 
36
  grad_cams = [GradCAM(model=model, target_layers=[model.convblock3[i]], use_cuda=False) for i in range(5)]
37
 
38
- def get_gradcam_image(input_tensor, label, target_layer):
39
  grad_cam = grad_cams[target_layer]
40
  targets = [ClassifierOutputTarget(label)]
41
  grayscale_cam = grad_cam(input_tensor=input_tensor, targets=targets)
@@ -43,7 +43,7 @@ def get_gradcam_image(input_tensor, label, target_layer):
43
  return grayscale_cam
44
 
45
 
46
- def image_classifier(input_image, top_classes=3, show_cam=True, target_layers=[2, 3], transparency=0.5):
47
  orig_image = input_image
48
  input_image = transform(input_image)
49
 
@@ -54,33 +54,33 @@ def image_classifier(input_image, top_classes=3, show_cam=True, target_layers=[2
54
  o = softmax(output.flatten())
55
 
56
  confidences = {classes[i]: float(o[i]) for i in range(10)}
57
- confidences_sorted = dict(sorted(confidences.items(), key=lambda x:x[1],reverse=True))
58
- confidences = {k: confidences_sorted[k] for k in list(confidences_sorted)[:top_classes]}
59
  _, label = torch.max(output, 1)
60
 
61
  outputs = list()
62
  if show_cam:
63
  for layer in target_layers:
64
- grayscale_cam = get_gradcam_image(input_image, label, layer)
65
  output_image = show_cam_on_image(orig_image / 255, grayscale_cam, use_rgb=True, image_weight=transparency)
66
  outputs.append((output_image, f"Layer {layer - 5}"))
67
 
68
  return outputs, confidences
69
 
70
- #examples = [["examples/cat.jpg", 3, True,["-2","-1"],0.5], ["examples/dog.jpg", 3, True,["-2","-1"],0.5]]
71
  examples = []
72
  for i in range(10):
73
  examples.append([f'examples/{classes[i]}.jpg', 3, True,["-2","-1"],0.5])
74
 
75
  demo_1 = gr.Interface(
76
- fn=image_classifier,
77
  inputs=[
78
  gr.Image(shape=(32, 32), label="Input Image").style(width=128, height=128),
79
- gr.Slider(1, 10, value=3, step=1, label="Top Classes",
80
  info="How many top classes do you want to see?"),
81
- gr.Checkbox(label="Enable GradCAM", value=True, info="Do you want to see GradCAM Images?"),
82
- gr.CheckboxGroup(["-5","-4", "-3", "-2", "-1"], value=["-2", "-1"], label="Network Layers", type='index',
83
- info="Which layer(s) GradCAM do you want to visualize?",),
84
  gr.Slider(0, 1, value=0.5, label="Transparency", step=0.1,
85
  info="Set Transparency of CAMs")
86
  ],
@@ -89,7 +89,7 @@ demo_1 = gr.Interface(
89
  )
90
 
91
 
92
- def show_incorrect(num_examples=10):
93
  result = list()
94
  for i in range(num_examples):
95
  j = np.random.randint(1,30)
@@ -97,19 +97,19 @@ def show_incorrect(num_examples=10):
97
  actual = classes[wrong_img.loc[j-1].at["actual"]]
98
  predicted = classes[wrong_img.loc[j-1].at["predicted"]]
99
 
100
- result.append((image, f"Actual:{actual} / Predicted:{predicted}"))
101
 
102
  return result
103
 
104
 
105
  demo_2 = gr.Interface(
106
- fn=show_incorrect,
107
  inputs=[
108
- gr.Number(value=10, minimum=1, maximum=30, label="Input number(s) of images", precision=0,
109
  info="How many misclassified examples do you want to view? (max 30)")
110
  ],
111
  outputs=[gr.Gallery(label="Misclassified Images (Actual / Predicted)", columns=5)]
112
  )
113
 
114
- demo = gr.TabbedInterface([demo_1, demo_2], ["Image Classifier", "Misclassified Images"])
115
  demo.launch(debug=True)
 
35
 
36
  grad_cams = [GradCAM(model=model, target_layers=[model.convblock3[i]], use_cuda=False) for i in range(5)]
37
 
38
+ def create_gradcam(input_tensor, label, target_layer):
39
  grad_cam = grad_cams[target_layer]
40
  targets = [ClassifierOutputTarget(label)]
41
  grayscale_cam = grad_cam(input_tensor=input_tensor, targets=targets)
 
43
  return grayscale_cam
44
 
45
 
46
+ def inference(input_image, top_classes=3, show_cam=True, target_layers=[2, 3], transparency=0.5):
47
  orig_image = input_image
48
  input_image = transform(input_image)
49
 
 
54
  o = softmax(output.flatten())
55
 
56
  confidences = {classes[i]: float(o[i]) for i in range(10)}
57
+ confidences = dict(sorted(confidences.items(), key=lambda x:x[1],reverse=True))
58
+ confidences = {i: confidences[i] for i in list(confidences)[:top_classes]}
59
  _, label = torch.max(output, 1)
60
 
61
  outputs = list()
62
  if show_cam:
63
  for layer in target_layers:
64
+ grayscale_cam = create_gradcam(input_image, label, layer)
65
  output_image = show_cam_on_image(orig_image / 255, grayscale_cam, use_rgb=True, image_weight=transparency)
66
  outputs.append((output_image, f"Layer {layer - 5}"))
67
 
68
  return outputs, confidences
69
 
70
+
71
  examples = []
72
  for i in range(10):
73
  examples.append([f'examples/{classes[i]}.jpg', 3, True,["-2","-1"],0.5])
74
 
75
  demo_1 = gr.Interface(
76
+ fn=inference,
77
  inputs=[
78
  gr.Image(shape=(32, 32), label="Input Image").style(width=128, height=128),
79
+ gr.Slider(1, 10, value=3, step=1, label="Top Predictions",
80
  info="How many top classes do you want to see?"),
81
+ gr.Checkbox(label="Show GradCAM", value=True, info="Do you want to see GradCAM Images?"),
82
+ gr.CheckboxGroup(["-5","-4", "-3", "-2", "-1"], value=["-2", "-1"], label="Conv Layers", type='index',
83
+ info="For which layer do you want to visualize GradCAM?",),
84
  gr.Slider(0, 1, value=0.5, label="Transparency", step=0.1,
85
  info="Set Transparency of CAMs")
86
  ],
 
89
  )
90
 
91
 
92
+ def show_misclassified(num_examples=10):
93
  result = list()
94
  for i in range(num_examples):
95
  j = np.random.randint(1,30)
 
97
  actual = classes[wrong_img.loc[j-1].at["actual"]]
98
  predicted = classes[wrong_img.loc[j-1].at["predicted"]]
99
 
100
+ result.append((image, f"Actual:{actual} \\n / Predicted:{predicted}"))
101
 
102
  return result
103
 
104
 
105
  demo_2 = gr.Interface(
106
+ fn=show_misclassified,
107
  inputs=[
108
+ gr.Number(value=10, minimum=1, maximum=30, label="Input number of images", precision=0,
109
  info="How many misclassified examples do you want to view? (max 30)")
110
  ],
111
  outputs=[gr.Gallery(label="Misclassified Images (Actual / Predicted)", columns=5)]
112
  )
113
 
114
+ demo = gr.TabbedInterface([demo_1, demo_2], ["CIFAR10 Classifier", "Mis-predicted Images"])
115
  demo.launch(debug=True)