|
a |
|
b/utilities/visUtils.py |
|
|
1 |
import os |
|
|
2 |
from PIL import Image |
|
|
3 |
import numpy as np |
|
|
4 |
from torch.autograd import Variable |
|
|
5 |
import torchvision.transforms as transforms |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
## GradCam and other Attention |
|
|
9 |
## https://github.com/jacobgil/pytorch-grad-cam |
|
|
10 |
from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad |
|
|
11 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget |
|
|
12 |
from pytorch_grad_cam.utils.image import show_cam_on_image |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class GradCamVisualizer: |
|
|
16 |
|
|
|
17 |
def __init__(self, model, visfolder, transform = None, |
|
|
18 |
grad_cam = {"layers": None, "class_target": None}, |
|
|
19 |
device = "cpu"): |
|
|
20 |
self.device = device |
|
|
21 |
if not os.path.exists(visfolder): os.makedirs(visfolder) |
|
|
22 |
self.visfolder = visfolder |
|
|
23 |
|
|
|
24 |
if transform: self.transforms = transforms |
|
|
25 |
else: transform =self.transforms = transforms.Compose([transforms.ToTensor()]) |
|
|
26 |
|
|
|
27 |
self.cam_list = [] |
|
|
28 |
for lyr in grad_cam["layers"]: |
|
|
29 |
self.cam_list.append( |
|
|
30 |
GradCAM(model=model, target_layers=grad_cam["layers"], |
|
|
31 |
use_cuda=device) ) |
|
|
32 |
|
|
|
33 |
cls_tgt = grad_cam["class_targets"] |
|
|
34 |
self.targets = [ClassifierOutputTarget(cls_tgt) ] if cls_tgt else None |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
def grad_cam_logger(self, image_path, ): |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
img, tnsr = self._image_loader() |
|
|
41 |
out_list = [] |
|
|
42 |
for cam in self.cam_list: |
|
|
43 |
grayscale_cam = cam(input_tensor=tnsr, targets=self.targets) |
|
|
44 |
grayscale_cam = grayscale_cam[0, :] |
|
|
45 |
imposed_img = show_cam_on_image(img, grayscale_cam, use_rgb=True) |
|
|
46 |
|
|
|
47 |
out_list.append() |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
def _image_loader(self, image_name): |
|
|
51 |
"""load image, returns cuda tensor""" |
|
|
52 |
|
|
|
53 |
image = Image.open(image_name) |
|
|
54 |
tensor = self.transforms(image).float() |
|
|
55 |
tensor = Variable(image, requires_grad=True) |
|
|
56 |
tensor = tensor.unsqueeze(0) #for batch Size |
|
|
57 |
return image, tensor |