-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradcam.py
181 lines (143 loc) · 7.45 KB
/
gradcam.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
import torch
import torch.nn.functional as F
from utils import find_alexnet_layer, find_vgg_layer, find_resnet_layer, find_densenet_layer, find_squeezenet_layer
class GradCAM(object):
"""Calculate GradCAM salinecy map.
A simple example:
# initialize a model, model_dict and gradcam
resnet = torchvision.models.resnet101(pretrained=True)
resnet.eval()
model_dict = dict(model_type='resnet', arch=resnet, layer_name='layer4', input_size=(224, 224))
gradcam = GradCAM(model_dict)
# get an image and normalize with mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
img = load_img()
normed_img = normalizer(img)
# get a GradCAM saliency map on the class index 10.
mask, logit = gradcam(normed_img, class_idx=10)
# make heatmap from mask and synthesize saliency map using heatmap and img
heatmap, cam_result = visualize_cam(mask, img)
Args:
model_dict (dict): a dictionary that contains 'model_type', 'arch', layer_name', 'input_size'(optional) as keys.
verbose (bool): whether to print output size of the saliency map givien 'layer_name' and 'input_size' in model_dict.
"""
def __init__(self, model_dict, verbose=False):
model_type = model_dict['type']
layer_name = model_dict['layer_name']
self.model_arch = model_dict['arch']
self.gradients = dict()
self.activations = dict()
def backward_hook(module, grad_input, grad_output):
self.gradients['value'] = grad_output[0]
return None
def forward_hook(module, input, output):
self.activations['value'] = output
return None
if 'vgg' in model_type.lower():
target_layer = find_vgg_layer(self.model_arch, layer_name)
elif 'resnet' in model_type.lower():
target_layer = find_resnet_layer(self.model_arch, layer_name)
elif 'densenet' in model_type.lower():
target_layer = find_densenet_layer(self.model_arch, layer_name)
elif 'alexnet' in model_type.lower():
target_layer = find_alexnet_layer(self.model_arch, layer_name)
elif 'squeezenet' in model_type.lower():
target_layer = find_squeezenet_layer(self.model_arch, layer_name)
target_layer.register_forward_hook(forward_hook)
target_layer.register_backward_hook(backward_hook)
if verbose:
try:
input_size = model_dict['input_size']
except KeyError:
print("please specify size of input image in model_dict. e.g. {'input_size':(224, 224)}")
pass
else:
device = 'cuda' if next(self.model_arch.parameters()).is_cuda else 'cpu'
self.model_arch(torch.zeros(1, 3, *(input_size), device=device))
print('saliency_map size :', self.activations['value'].shape[2:])
def forward(self, input, class_idx=None, retain_graph=False):
"""
Args:
input: input image with shape of (1, 3, H, W)
class_idx (int): class index for calculating GradCAM.
If not specified, the class index that makes the highest model prediction score will be used.
Return:
mask: saliency map of the same spatial dimension with input
logit: model output
"""
b, c, h, w = input.size()
logit = self.model_arch(input)
if class_idx is None:
score = logit[:, logit.max(1)[-1]].squeeze()
else:
score = logit[:, class_idx].squeeze()
self.model_arch.zero_grad()
score.backward(retain_graph=retain_graph)
gradients = self.gradients['value']
activations = self.activations['value']
b, k, u, v = gradients.size()
alpha = gradients.view(b, k, -1).mean(2)
#alpha = F.relu(gradients.view(b, k, -1)).mean(2)
weights = alpha.view(b, k, 1, 1)
saliency_map = (weights*activations).sum(1, keepdim=True)
saliency_map = F.relu(saliency_map)
saliency_map = F.upsample(saliency_map, size=(h, w), mode='bilinear', align_corners=False)
saliency_map_min, saliency_map_max = saliency_map.min(), saliency_map.max()
saliency_map = (saliency_map - saliency_map_min).div(saliency_map_max - saliency_map_min).data
return saliency_map, logit
def __call__(self, input, class_idx=None, retain_graph=False):
return self.forward(input, class_idx, retain_graph)
class GradCAMpp(GradCAM):
"""Calculate GradCAM++ salinecy map.
A simple example:
# initialize a model, model_dict and gradcampp
resnet = torchvision.models.resnet101(pretrained=True)
resnet.eval()
model_dict = dict(model_type='resnet', arch=resnet, layer_name='layer4', input_size=(224, 224))
gradcampp = GradCAMpp(model_dict)
# get an image and normalize with mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
img = load_img()
normed_img = normalizer(img)
# get a GradCAM saliency map on the class index 10.
mask, logit = gradcampp(normed_img, class_idx=10)
# make heatmap from mask and synthesize saliency map using heatmap and img
heatmap, cam_result = visualize_cam(mask, img)
Args:
model_dict (dict): a dictionary that contains 'model_type', 'arch', layer_name', 'input_size'(optional) as keys.
verbose (bool): whether to print output size of the saliency map givien 'layer_name' and 'input_size' in model_dict.
"""
def __init__(self, model_dict, verbose=False):
super(GradCAMpp, self).__init__(model_dict, verbose)
def forward(self, input, class_idx=None, retain_graph=False):
"""
Args:
input: input image with shape of (1, 3, H, W)
class_idx (int): class index for calculating GradCAM.
If not specified, the class index that makes the highest model prediction score will be used.
Return:
mask: saliency map of the same spatial dimension with input
logit: model output
"""
b, c, h, w = input.size()
logit = self.model_arch(input)
if class_idx is None:
score = logit[:, logit.max(1)[-1]].squeeze()
else:
score = logit[:, class_idx].squeeze()
self.model_arch.zero_grad()
score.backward(retain_graph=retain_graph)
gradients = self.gradients['value'] # dS/dA
activations = self.activations['value'] # A
b, k, u, v = gradients.size()
alpha_num = gradients.pow(2)
alpha_denom = gradients.pow(2).mul(2) + \
activations.mul(gradients.pow(3)).view(b, k, u*v).sum(-1, keepdim=True).view(b, k, 1, 1)
alpha_denom = torch.where(alpha_denom != 0.0, alpha_denom, torch.ones_like(alpha_denom))
alpha = alpha_num.div(alpha_denom+1e-7)
positive_gradients = F.relu(score.exp()*gradients) # ReLU(dY/dA) == ReLU(exp(S)*dS/dA))
weights = (alpha*positive_gradients).view(b, k, u*v).sum(-1).view(b, k, 1, 1)
saliency_map = (weights*activations).sum(1, keepdim=True)
saliency_map = F.relu(saliency_map)
saliency_map = F.upsample(saliency_map, size=(224, 224), mode='bilinear', align_corners=False)
saliency_map_min, saliency_map_max = saliency_map.min(), saliency_map.max()
saliency_map = (saliency_map-saliency_map_min).div(saliency_map_max-saliency_map_min).data
return saliency_map, logit