-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrad-cam-resnet50.py
362 lines (268 loc) · 13.8 KB
/
grad-cam-resnet50.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import os
import cv2
import glob
import torch
import traceback
from torch.autograd import Variable
from torch.autograd import Function
import xml.etree.ElementTree as ET
from torchvision import models
from torchvision import utils
import numpy as np
import argparse
class FeatureExtractor():
""" Class for extracting activations and
registering gradients from targetted intermediate layers """
def __init__(self, model, target_layers):
self.model = model
self.target_layers = target_layers
self.gradients = []
def save_gradient(self, grad):
self.gradients.append(grad)
def __call__(self, x):
outputs = []
self.gradients = []
for name, module in self.model._modules.items():
if name == "fc":
x = x.view(x.size(0), -1)
x = module(x)
if name in self.target_layers:
x.register_hook(self.save_gradient)
outputs += [x]
return outputs, x
class ModelOutputs():
""" Class for making a forward pass, and getting:
1. The network output.
2. Activations from intermeddiate targetted layers.
3. Gradients from intermeddiate targetted layers. """
def __init__(self, model, target_layers):
self.model = model
self.feature_extractor = FeatureExtractor(self.model, target_layers)
def get_gradients(self):
return self.feature_extractor.gradients
def __call__(self, x):
target_activations, output = self.feature_extractor(x)
return target_activations, output
def preprocess_image(img):
means = [0.485, 0.456, 0.406]
stds = [0.229, 0.224, 0.225]
preprocessed_img = img.copy()[:, :, ::-1]
for i in range(3):
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] - means[i]
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] / stds[i]
preprocessed_img = np.ascontiguousarray(np.transpose(preprocessed_img, (2, 0, 1)))
preprocessed_img = torch.from_numpy(preprocessed_img)
preprocessed_img.unsqueeze_(0)
input = Variable(preprocessed_img, requires_grad=True)
return input
class GradCam:
def __init__(self, model, target_layer_names, device):
self.model = model
self.model.eval()
self.model = model.to(device)
self.extractor = ModelOutputs(self.model, target_layer_names)
def forward(self, input):
return self.model(input)
def __call__(self, input, topk=1):
bboxes_top = list()
for k in range(topk):
features, output = self.extractor(input.to(device))
index = np.argsort(output.cpu().data.numpy())[-1][-(k+1)] # top probs to low
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0][index] = 1
one_hot = Variable(torch.from_numpy(one_hot), requires_grad=True)
one_hot = torch.sum(one_hot.to(device) * output)
#self.model.features.zero_grad()
#self.model.classifier.zero_grad()
self.model.zero_grad()
one_hot.backward(retain_graph=True)
cam_list = []
for j in range(len(features)):
grads_val = self.extractor.get_gradients()[j].cpu().data.numpy()
target = features[len(features) - j - 1]
target = target.cpu().data.numpy()[0, :]
weights = np.mean(grads_val, axis=(2, 3))[0, :]
cam = np.zeros(target.shape[1:], dtype=np.float32)
for i, w in enumerate(weights):
cam += w * target[i, :, :]
cam = np.maximum(cam, 0)
cam = cv2.resize(cam, (224, 224))
cam = cam - np.min(cam)
cam = cam / np.max(cam)
cam_list.append(cam)
for i in range(len(cam_list) - 1):
cam_list[i] = cv2.resize(cam_list[i], (cam_list[i + 1].shape[0], cam_list[i + 1].shape[1]))
#cam_list[i] = cv2.resize(cam_list[i], (224, 224))
#cam_list[i + 1] = cam_list[i + 1] + cam_list[i]
cam_list[i + 1] = np.array((cam_list[i + 1], cam_list[i])).max(axis=0)
mask = cam_list[-1]
mask_copy = mask.copy()
shreld = mask.sum() / (mask.shape[0] * mask.shape[1]) * 1.7
mask = np.array(mask >= shreld, dtype='uint8')
ret, binary = cv2.threshold(mask, shreld, 255, cv2.THRESH_BINARY)
_, contours, hierarcy = cv2.findContours(binary.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cont_ = sorted(contours, key=cv2.contourArea, reverse=True)
if len(cont_) == 0:
box = np.zeros((4,2), dtype=int)
else:
c = cont_[0]
# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))
bboxes_top.append({"classify":index, "bbox":box, "mask":mask_copy})
return bboxes_top
def visualize_label(img, boxes, path, color=(0, 255, 0)):
"""
img: HWC
boxes: array of num * 4 * 2
"""
boxes = np.array(boxes).reshape(-1, 4, 2)
img = np.ascontiguousarray(img)
cv2.drawContours(img, boxes, -1, color, thickness=2)
cv2.imwrite(path, img * 255)
def show_cam_on_image(img, mask, path):
heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
cam = heatmap + np.float32(img)
cam = cam / np.max(cam)
cv2.imwrite(path, np.uint8(255 * cam))
def checkpoint(box, origin_ratio):
h_ratio, w_ratio = origin_ratio
box = np.array(box).reshape(-1, 4, 2)
xmin, ymin = box.min(1)[0]
xmax, ymax = box.max(1)[0]
xmin,ymin,xmax,ymax = np.max((xmin,0)),np.max((ymin,0)),np.min((xmax,224)),np.min((ymax,224))
xmin,ymin,xmax,ymax = int(xmin * w_ratio), int(ymin * h_ratio), int(xmax * w_ratio), int(ymax * h_ratio)
return str(xmin), str(ymin), str(xmax), str(ymax)
def visualize(img, boxes, path, color=(0, 255, 0)):
xmin, ymin, xmax, ymax = boxes
img = np.ascontiguousarray(img)
x,y,w,h = int(xmin), int(ymin), int(xmax)-int(xmin), int(ymax)-int(ymin)
#cv2.drawContours(img, boxes, -1, color, thickness=2)
cv2.rectangle(img,(x,y),(x+w,y+h),color,2)
# return img
cv2.imwrite(path, img)
def cal_IOU(box1, box2):
"""
box1, box2: list or numpy array of size 4*2 or 8, h_index first
"""
box1 = [box1[0], box1[1], box1[2], box1[1], box1[2], box1[3], box1[0], box1[3]]
box2 = [box2[0], box2[1], box2[2], box2[1], box2[2], box2[3], box2[0], box2[3]]
box1 = np.array(box1, dtype=np.int).reshape([1, 4, 2])
box2 = np.array(box2, dtype=np.int).reshape([1, 4, 2])
box1_max = box1.max(axis=1)
box2_max = box2.max(axis=1)
w_max = int(max(box1_max[0][0], box2_max[0][0]))
h_max = int(max(box1_max[0][1], box2_max[0][1]))
canvas = np.zeros((h_max + 1, w_max + 1))
# print(canvas.shape)
box1_canvas = canvas.copy()
box1_area = np.sum(cv2.drawContours(box1_canvas, box1, -1, 1, thickness=-1))
# print(box1_area)
box2_canvas = canvas.copy()
box2_area = np.sum(cv2.drawContours(box2_canvas, box2, -1, 1, thickness=-1))
# print(box2_area)
cv2.drawContours(canvas, box1, -1, 1, thickness=-1)
cv2.drawContours(canvas, box2, -1, 1, thickness=-1)
union = np.sum(canvas)
intersction = box1_area + box2_area - union
return intersction / union
def _load_pascal_annotation(filename):
tree = ET.parse(filename)
objs = tree.findall('object')
objects = list()
# Load object bounding boxes into a data frame.
for ix, obj in enumerate(objs):
bbox = obj.find('bndbox')
# Make pixel indexes 0-based
x1 = float(bbox.find('xmin').text)
y1 = float(bbox.find('ymin').text)
x2 = float(bbox.find('xmax').text)
y2 = float(bbox.find('ymax').text)
class_name = obj.find('name').text.lower().strip()
objects.append([x1, y1, x2, y2, class_name])
return objects
def vis_bbox(im, gt, rect, path):
rect[1] = int(rect[1])
rect[2] = int(rect[2])
rect[3] = int(rect[3])
rect[4] = int(rect[4])
cv2.rectangle(im, (rect[1], rect[2]), (rect[3], rect[4]), (204, 0, 0), 4)
cv2.putText(im, '%s' % ("ROIs"), (rect[1], rect[2] + 15), cv2.FONT_HERSHEY_PLAIN, 2.0, (204, 0, 0), thickness=2)
cv2.rectangle(im, (int(gt[0]), int(gt[1])), (int(gt[2]), int(gt[3])), (0, 0, 255), 4)
cv2.putText(im, '%s' % ("ground truth"), (int(gt[0]), int(gt[1]) + 15), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), thickness=2)
cv2.imwrite(path, im)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cuda', type= bool, default=True, help='Use NVIDIA GPU acceleration')
parser.add_argument('--gpus', type=str, default="2", help="default GPU devices (0,1)")
parser.add_argument('--target_layer', type=list, default=["layer1", "layer2", "layer3", "layer4"], help="default GPU devices (0,1)")#"8", "17", "26", "35",
parser.add_argument('--result-path', type=str, default='./Resnet50_bbox_result', help='Input image path')
parser.add_argument('--ground-truth-path', type=str, default='./val', help='Input image path')
parser.add_argument('--image-path', type=str, default='./dataset', help='Input image path')
args = parser.parse_args()
if args.cuda:
print("=====> use gpu id: '{}'".format(args.gpus))
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
if not torch.cuda.is_available():
raise Exception("No GPU found or Wrong gpu id, please run without --cuda")
device = 'cuda'
print("Using GPU for acceleration")
else:
device = 'cpu'
print("Using CPU for computation")
image_list = glob.glob(os.path.join(args.image_path, "*.JPEG"))
if not os.path.exists(args.result_path):
os.makedirs(args.result_path)
#writer = open(os.path.join(args.result_path, "submission.txt"), "a")
logger = open(os.path.join(args.result_path, "log.txt"), "a")
for num, image_path in enumerate(sorted(image_list)):
try:
# get the gradient model
grad_cam = GradCam(model=models.resnet50(pretrained=True), target_layer_names=args.target_layer, device=device)
image_name = image_path.replace(args.image_path+"/", "").replace(".JPEG", "")
xml_name = image_name + ".xml"
result_img_dir = args.result_path
if not os.path.exists(result_img_dir):
os.makedirs(result_img_dir)
img = cv2.imread(image_path, 1)
img_ = img.copy()
origin_ratio = (img.shape[0] / 224, img.shape[1] / 224)
img = np.float32(cv2.resize(img, (224, 224))) / 255
#cv2.imwrite(os.path.join(result_img_dir, image_name+"_source.jpg"), img * 255)
input = preprocess_image(img)
# If None, returns the map for the highest scoring category(top one).
# Otherwise, returns the top5 category. the bbox is gotten by top to low.
bboxs = grad_cam(input, topk=5)
cam = np.zeros(img.shape[:-1], dtype=np.float32)
ROIs = list()
for i, bbox in enumerate(bboxs):
if i == 0:
#show_cam_on_image(img.copy(), bbox["mask"], os.path.join(result_img_dir, "top_" + str(i) + "_" + image_name + "_cam.jpg"))
#visualize_label(img.copy(), bbox["bbox"], os.path.join(result_img_dir, "top_" + str(i) + "_" + image_name + "_bbox.jpg"))
ROIs.append(str(bbox["classify"]))
ROIs = ROIs + list(checkpoint(bbox["bbox"], origin_ratio))
cam = np.array((cam, bbox["mask"])).max(axis=0)
ROIs_copy = ROIs.copy()
ground_truth = _load_pascal_annotation(os.path.join(args.ground_truth_path, xml_name))
if len(ground_truth) > 1:
logger.write(xml_name + " " + str(len(ground_truth)) +"\n")
for gt in ground_truth:
#cv2.imwrite(os.path.join(result_img_dir, image_name+"_source.jpg"), img_)
if cal_IOU(gt, ROIs_copy) >= 0.5:
vis_bbox(img_, gt, ROIs_copy, os.path.join(result_img_dir, image_name + "_bbox.jpg"))
break
else:
continue
else:
#show_cam_on_image(img.copy(), bbox["mask"], os.path.join(result_img_dir, "top_" + str(i) + "_" + image_name + "_cam.jpg"))
#visualize_label(img.copy(), bbox["bbox"], os.path.join(result_img_dir, "top_" + str(i) + "_" + image_name + "_bbox.jpg"))
#ROIs.append(str(bbox["classify"]))
#ROIs = ROIs + list(checkpoint(bbox["bbox"], origin_ratio))
#cam = np.array((cam, bbox["mask"])).max(axis=0)
pass
#writer.write(" ".join(ROIs)+"\n")
#show_cam_on_image(img.copy(), cam, os.path.join(result_img_dir, image_name + "_all_cam.jpg"))
print(num, image_name + ".JPEG is finished!")
except Exception as e:
traceback.print_exc()