-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils_visualise.py
101 lines (82 loc) · 3.3 KB
/
utils_visualise.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
# -*- coding: utf-8 -*-
"""
Some utility functions for visualisation, not documented properly
"""
from skimage import color
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pylab
def plot_results(x_test, x_test_im, sensMap, predDiff, tarFunc, classnames, testIdx, save_path):
'''
Plot the results of the relevance estimation
'''
imsize = x_test.shape
tarIdx = np.argmax(tarFunc(x_test)[-1])
tarClass = classnames[tarIdx]
#tarIdx = 287
plt.figure()
plt.subplot(2,2,1)
plt.imshow(x_test_im, interpolation='nearest')
plt.title('original')
frame = pylab.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])
plt.subplot(2,2,2)
plt.imshow(sensMap, cmap=cm.Greys_r, interpolation='nearest')
plt.title('sensitivity map')
frame = pylab.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])
plt.subplot(2,2,3)
p = predDiff.reshape((imsize[1],imsize[2],-1))[:,:,tarIdx]
plt.imshow(p, cmap=cm.seismic, vmin=-np.max(np.abs(p)), vmax=np.max(np.abs(p)), interpolation='nearest')
plt.colorbar()
#plt.imshow(np.abs(p), cmap=cm.Greys_r)
plt.title('weight of evidence')
frame = pylab.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])
plt.subplot(2,2,4)
plt.title('class: {}'.format(tarClass))
p = get_overlayed_image(x_test_im, p)
#p = predDiff[0,:,:,np.argmax(netPred(net, x_test)[0]),1].reshape((224,224))
plt.imshow(p, cmap=cm.seismic, vmin=-np.max(np.abs(p)), vmax=np.max(np.abs(p)), interpolation='nearest')
#plt.title('class entropy')
frame = pylab.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])
fig = plt.gcf()
fig.set_size_inches(np.array([12,12]), forward=True)
plt.tight_layout()
plt.tight_layout()
plt.tight_layout()
plt.savefig(save_path)
plt.close()
def get_overlayed_image(x, c, gray_factor_bg = 0.3):
'''
For an image x and a relevance vector c, overlay the image with the
relevance vector to visualise the influence of the image pixels.
'''
imDim = x.shape[0]
if np.ndim(c)==1:
c = c.reshape((imDim,imDim))
if np.ndim(x)==2: # this happens with the MNIST Data
x = 1-np.dstack((x, x, x))*gray_factor_bg # make it a bit grayish
if np.ndim(x)==3: # this is what happens with cifar data
x = color.rgb2gray(x)
x = 1-(1-x)*0.5
x = np.dstack((x,x,x))
alpha = 0.8
# Construct a colour image to superimpose
im = plt.imshow(c, cmap = cm.seismic, vmin=-np.max(np.abs(c)), vmax=np.max(np.abs(c)), interpolation='nearest')
color_mask = im.to_rgba(c)[:,:,[0,1,2]]
# Convert the input image and color mask to Hue Saturation Value (HSV) colorspace
img_hsv = color.rgb2hsv(x)
color_mask_hsv = color.rgb2hsv(color_mask)
# Replace the hue and saturation of the original image
# with that of the color mask
img_hsv[..., 0] = color_mask_hsv[..., 0]
img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha
img_masked = color.hsv2rgb(img_hsv)
return img_masked