datamllab / xdeep Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hi,
Below is the code for Grad-CAM++ as implemented in this repository.
I don't understand why 1e-7
is added to the denominator out here alpha = alpha_num.div(alpha_denom + 1e-7)
.
Any reason for adding this term?
class GradCAMpp(GradCAM):
"""
GradCAM++, inherit from BaseCAM
"""
def __init__(self, model_dict):
super(GradCAMpp, self).__init__(model_dict)
def forward(self, input_image, class_idx=None, retain_graph=False):
"""Generates GradCAM++ result.
# Arguments
input_image: torch.Tensor. Preprocessed image with shape (1, C, H, W).
class_idx: int. Index of target class. Defaults to be index of predicted class.
# Return
Result of GradCAM++ (torch.Tensor) with shape (1, H, W).
"""
b, c, h, w = input_image.size()
logit = self.model_arch(input_image)
if class_idx is None:
score = logit[:, logit.max(1)[-1]].squeeze()
else:
score = logit[:, class_idx].squeeze()
if torch.cuda.is_available():
score = score.cuda()
logit = logit.cuda()
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()
if torch.cuda.is_available():
activations = activations.cuda()
gradients = gradients.cuda()
alpha_num = gradients.pow(2)
global_sum = activations.view(b, k, u * v).sum(-1, keepdim=True).view(b, k, 1, 1)
alpha_denom = gradients.pow(2).mul(2) + global_sum.mul(gradients.pow(3))
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)
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.interpolate(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
@daochenzha hi thanks for opensourcing the code , can we use xdeep for models like MaskRCNN/Deeplab and FasterRCNN,Yolov2, RetinaNet
Model: Alexnet
Dataset: CIFAR-10
The code works well with layer_name='features'
but I get this error when I change it to layer_name='classifier'
Code:
# GRADCAM++
image_path = 'xyz'
image = load_image(image_path)
norm_image = apply_transforms(image, size=32)
model_dict = dict(arch=model, layer_name='classifier_0', input_size=(32, 32))
gradcampp = GradCAMpp(model_dict)
output = gradcampp(norm_image)
visualize(norm_image, output)
Error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-63-fed255ab9edc> in <module>()
7
8 gradcampp = GradCAMpp(model_dict)
----> 9 output = gradcampp(norm_image)
10 visualize(norm_image, output)
1 frames
<ipython-input-46-e7d860e12ca6> in __call__(self, input_, class_idx, retain_graph)
70
71 def __call__(self, input_, class_idx=None, retain_graph=False):
---> 72 return self.forward(input_, class_idx, retain_graph)
<ipython-input-49-d221b5d1444c> in forward(self, input_image, class_idx, retain_graph)
32 gradients = self.gradients['value']
33 activations = self.activations['value']
---> 34 b, k, u, v = gradients.size()
35
36 alpha_num = gradients.pow(2)
ValueError: not enough values to unpack (expected 4, got 2)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.