Saliency Map

class olympus.dashboard.plots.saliency.GuidedBackprop(model, activation=<class 'torch.nn.modules.activation.ReLU'>, preprocessor=<function imagenet_preprocess>, postprocessor=<function imagenet_postprocessor>)[source]

Bases: object

TODO: find original paper

Parameters:
model:

Pytorch model

activation:

Type of the activation layer to use, defaults to ReLU

preprocessor: Callable[[List[PILImage]], Tensor]

use to apply preprocessing to images

postprocesoor: Callable[[Tensor], List[PilImage]]

used to reconstruct the image from a tensor

References

[1]J. T. Springenberg, A. Dosovitskiy, T. Brox, and M. Riedmiller. “Striving for Simplicity: The All Convolutional Net” https://arxiv.org/abs/1412.6806
[2]K. Simonyan, A. Vedaldi, A. Zisserman. “Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps” https://arxiv.org/abs/1312.6034
[3]https://arxiv.org/pdf/1810.03292v1.pdf

Examples

>>> import torchvision.models as models
>>> from torchvision import transforms
>>> from PIL import Image
>>> path = 'docs/_static/images/cat.jpg'
>>> img = Image.open(path)
>>> model = models.alexnet(pretrained=True)
>>> guided = GuidedBackprop(model)
>>> _ = guided([img], [285])
>>> for i, grad in enumerate(guided.negative_saliency()):
...     img = imagenet_postprocessor(grad)
...     img.save(f'negative_saliency_{i}.jpg')
../../_images/cat.jpg ../../_images/negative_saliency.jpg

Methods

__call__(images[, classes])
Parameters:
activation_backward(module, grad_in, grad_out) Backward hook to the activation layer
activation_forward(module, ten_in, ten_out) Forward hook to the activation layer
fetch_gradient(module, grad_in, grad_out) Fetch last gradient or gradient of the first layer
grad_x_input() Returns the gradient multiplied by the input image
negative_saliency() Returns negative gradients
positive_saliency() Returns positive gradients
activation_backward(module, grad_in, grad_out)[source]

Backward hook to the activation layer

activation_forward(module, ten_in, ten_out)[source]

Forward hook to the activation layer

fetch_gradient(module, grad_in, grad_out)[source]

Fetch last gradient or gradient of the first layer

grad_x_input()[source]

Returns the gradient multiplied by the input image

negative_saliency()[source]

Returns negative gradients

positive_saliency()[source]

Returns positive gradients

olympus.dashboard.plots.saliency.imagenet_postprocessor(image)[source]
olympus.dashboard.plots.saliency.imagenet_preprocess(img)[source]