LN1996 commited on
Commit
dd813d9
·
1 Parent(s): cd3492a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -8,7 +8,7 @@ from torch.utils.data import DataLoader
8
  import itertools
9
  import matplotlib.pyplot as plt
10
  import matplotlib.patches as patches
11
-
12
 
13
  import config as config
14
  from model import YOLOv3
@@ -17,7 +17,7 @@ from utils import get_loaders
17
  import utils
18
 
19
  new_state_dict = {}
20
- state_dict = torch.load('results/Yolov3_Lavanya.pth', map_location=torch.device('cpu'))
21
  for key, value in state_dict.items():
22
  new_key = key.replace('model.', '')
23
  new_state_dict[new_key] = value
@@ -48,7 +48,12 @@ classes = ("aeroplane",
48
  "tvmonitor")
49
 
50
 
51
- def inference(input_img=None, iou_threshold=0.6, conf_threshold=0.5):
 
 
 
 
 
52
 
53
  if input_img is not None:
54
 
@@ -133,26 +138,39 @@ def inference(input_img=None, iou_threshold=0.6, conf_threshold=0.5):
133
 
134
  ### GradCAM
135
 
136
- # target_layer = [model.layers[-2]]
137
- # target_categories = [box[0] for box in nms_boxes]
138
- # targets = [ClassifierOutputTarget(category) for category in target_categories]
139
 
140
- # cam = ex.BaseCAM(model, target_layer, target_categories)
141
- # help_ = cam.compute_cam_per_layer(transform_img, targets, False)
142
 
143
- # print(help_)
144
- # print(np.shape(cam))
145
-
146
- # cam = EigenCAM(model, [model.layers[-2]], use_cuda=False)
147
- # grayscale_cam = cam(transform_img)[0, :, :]
148
- # print('Hello')
149
- # cam_image = show_cam_on_image(input_img, grayscale_cam, use_rgb=True)
 
 
150
 
151
- # plt.imshow(cam_image)
152
- # plt.show()
 
153
 
 
 
 
154
 
155
- outputs_inference_gc = None
 
 
 
 
 
 
 
 
156
 
157
  else:
158
  outputs_inference_bb = None
@@ -161,14 +179,16 @@ def inference(input_img=None, iou_threshold=0.6, conf_threshold=0.5):
161
  return outputs_inference_bb, outputs_inference_gc
162
 
163
 
 
164
  title = "PASCAL VOC trained on Yolov3"
165
  description = "A simple Gradio interface to infer on Yolov3 model, and get GradCAM results"
166
- examples = [['examples/test_'+str(i)+'.jpg', 0.6, 0.5] for i in range(10)]
167
 
168
  demo = gr.Interface(inference,
169
  inputs = [gr.Image(label="Input image"),
170
  gr.Slider(0, 1, value=0.6, label="IOU Threshold"),
171
  gr.Slider(0, 1, value=0.4, label="Threshold"),
 
172
  ],
173
  outputs = [
174
  gr.Image(label="Yolov3 Prediction"),
 
8
  import itertools
9
  import matplotlib.pyplot as plt
10
  import matplotlib.patches as patches
11
+ import cv2
12
 
13
  import config as config
14
  from model import YOLOv3
 
17
  import utils
18
 
19
  new_state_dict = {}
20
+ state_dict = torch.load('Yolov3_Shashank.pth', map_location=torch.device('cpu'))
21
  for key, value in state_dict.items():
22
  new_key = key.replace('model.', '')
23
  new_state_dict[new_key] = value
 
48
  "tvmonitor")
49
 
50
 
51
+ import grad_cam_func as gcf
52
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
53
+ from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
54
+ from pytorch_grad_cam.utils.image import show_cam_on_image
55
+
56
+ def inference(input_img=None, iou_threshold=0.6, conf_threshold=0.5, gc_trans=0.3):
57
 
58
  if input_img is not None:
59
 
 
138
 
139
  ### GradCAM
140
 
141
+ target_layer = [model.layers[-2]]
142
+ cam = gcf.BaseCAM(model, target_layer)
 
143
 
144
+ AnG = ActivationsAndGradients(model, target_layer, None)
145
+ outputs = AnG(transform_img)
146
 
147
+ bboxes = [[] for _ in range(1)]
148
+ for i in range(3):
149
+ batch_size, A, S, _, _ = outputs[i].shape
150
+ anchor = config.SCALED_ANCHORS[i]
151
+ boxes_scale_i = utils.cells_to_bboxes(
152
+ outputs[i], anchor, S=S, is_preds=True
153
+ )
154
+ for idx, (box) in enumerate(boxes_scale_i):
155
+ bboxes[idx] += box
156
 
157
+ nms_boxes = utils.non_max_suppression(
158
+ bboxes[0], iou_threshold=0.5, threshold=0.4, box_format="midpoint",
159
+ )
160
 
161
+ target_categories = [box[0] for box in nms_boxes]
162
+ targets = [ClassifierOutputTarget(
163
+ category) for category in target_categories]
164
 
165
+ help_ = cam.compute_cam_per_layer(transform_img, targets, False)
166
+
167
+ output_gc = cam.aggregate_multi_layers(help_)[0, :, :]
168
+
169
+ img = cv2.resize(input_img, (416, 416))
170
+ img = np.float32(img) / 255
171
+ cam_image = show_cam_on_image(img, output_gc, use_rgb=True, image_weight=gc_trans)
172
+
173
+ outputs_inference_gc = cam_image
174
 
175
  else:
176
  outputs_inference_bb = None
 
179
  return outputs_inference_bb, outputs_inference_gc
180
 
181
 
182
+
183
  title = "PASCAL VOC trained on Yolov3"
184
  description = "A simple Gradio interface to infer on Yolov3 model, and get GradCAM results"
185
+ examples = [['examples/test_'+str(i)+'.jpg', 0.6, 0.5, 0.3] for i in range(10)]
186
 
187
  demo = gr.Interface(inference,
188
  inputs = [gr.Image(label="Input image"),
189
  gr.Slider(0, 1, value=0.6, label="IOU Threshold"),
190
  gr.Slider(0, 1, value=0.4, label="Threshold"),
191
+ gr.Slider(0, 1, value=0.5, label="GradCAM Transparency"),
192
  ],
193
  outputs = [
194
  gr.Image(label="Yolov3 Prediction"),