forked from SrishtiGautam/PRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmaphelpers.py
42 lines (29 loc) · 1012 Bytes
/
heatmaphelpers.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 12:43:52 2020
@author: srishtigautam
Code built upon LRP code from https://github.com/AlexBinder/LRP_Pytorch_Resnets_Densenet
"""
import numpy as np
import matplotlib.pyplot as plt
from helpers import makedir
import torch
try:
from skimage.feature import canny
except:
from skimage.filter import canny
def imshow_im(hm,imgtensor,q=100,folder="folder",name="name"):
def invert_normalize(ten, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
s=torch.tensor(np.asarray(std,dtype=np.float32)).unsqueeze(1).unsqueeze(2)
m=torch.tensor(np.asarray(mean,dtype=np.float32)).unsqueeze(1).unsqueeze(2)
res=ten*s+m
return res
def showimgfromtensor(inpdata):
ts=invert_normalize(inpdata)
a=ts.data.squeeze(0).numpy()
saveimg=(a*255.0).astype(np.uint8)
hm = hm.squeeze().sum(dim=0).detach().numpy()
clim = np.percentile(np.abs(hm), q)
hm = hm / clim
return hm