forked from hcmlab/nova
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageExplainerInnvestigate.py
More file actions
219 lines (161 loc) · 5.92 KB
/
Copy pathImageExplainerInnvestigate.py
File metadata and controls
219 lines (161 loc) · 5.92 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import sys
class CatchOutErr:
def __init__(self):
self.value = ''
def write(self, txt):
self.value += txt
catchOutErr = CatchOutErr()
sys.stderr = catchOutErr
from keras.preprocessing import image as kerasimg
from keras.models import load_model
import os
import numpy as np
import io as inputoutput
from PIL import Image as pilimage
import numpy as np
import innvestigate
import innvestigate.utils as iutils
import innvestigate.utils.visualizations as ivis
import matplotlib.pyplot as plot
def transform_img_fn(img):
img = pilimage.open(inputoutput.BytesIO(bytes(img)))
imgR = img.resize((224, 224))
x = kerasimg.img_to_array(imgR)
x = np.expand_dims(x, axis=0)
return (x, img)
def loadModel(modelPath):
try:
model = load_model(modelPath)
except Exception as ex:
return None
return model
def getTopXpredictions(prediction, topLabels):
prediction_class = []
for i in range(0, len(prediction[0])):
prediction_class.append((i, prediction[0][i]))
prediction_class.sort(key=lambda x: x[1], reverse=True)
return prediction_class[:topLabels]
def explain(model, img, postprocess, explainer):
explanation = []
img, oldImg = transform_img_fn(img)
img = img*(1./255)
prediction = model.predict(img)
topClass = getTopXpredictions(prediction, 1)
model_wo_sm = iutils.keras.graph.model_wo_softmax(model)
analyzer = []
if explainer == "GUIDEDBACKPROP":
analyzer = innvestigate.analyzer.GuidedBackprop(model_wo_sm)
elif explainer == "GRADIENT":
analyzer = innvestigate.analyzer.Gradient(model_wo_sm)
elif explainer == "DECONVNET":
analyzer = innvestigate.analyzer.Deconvnet(model_wo_sm)
elif explainer == "LRPEPSILON":
analyzer = innvestigate.analyzer.LRPEpsilon(model_wo_sm)
elif explainer == "LRPZ":
analyzer = innvestigate.analyzer.LRPZ(model_wo_sm)
elif explainer == "LRPALPHABETA":
analyzer = innvestigate.analyzer.LRPAlphaBeta(model_wo_sm, beta=1)
elif explainer == "DEEPTAYLOR":
analyzer = innvestigate.analyzer.DeepTaylor(model_wo_sm)
# Applying the analyzer
analysis = analyzer.analyze(img)
imgFinal = []
if postprocess == "GRAYMAP":
imgFinal = graymap(analysis)[0]
elif postprocess =="HEATMAP":
imgFinal = heatmap(analysis)[0]
elif postprocess == "BK_PROJ":
imgFinal = bk_proj(analysis)[0]
elif postprocess == "GNUPLOT2":
imgFinal = heatmapgnuplot2(analysis)[0]
elif postprocess == "CMRMAP":
imgFinal = heatmapCMRmap(analysis)[0]
elif postprocess == "NIPY_SPECTRAL":
imgFinal = heatmapnipy_spectral(analysis)[0]
elif postprocess == "RAINBOW":
imgFinal = heatmap_rainbow(analysis)[0]
elif postprocess == "INFERNO":
imgFinal = heatmap_inferno(analysis)[0]
elif postprocess == "GIST_HEAT":
imgFinal = heatmap_gist_heat(analysis)[0]
elif postprocess == "VIRIDIS":
imgFinal = heatmap_viridis(analysis)[0]
imgFinal = np.uint8(imgFinal*255)
img = pilimage.fromarray(imgFinal)
imgByteArr = inputoutput.BytesIO()
img.save(imgByteArr, format='JPEG')
imgByteArr = imgByteArr.getvalue()
explanation = (topClass[0][0], topClass[0][1], imgByteArr)
return explanation
def test():
modelPath = "C:/Users/Alex Heimerl/Desktop/test/pokemon.trainer.PythonModel.model.keras_vgg_face.h5"
img_path = "C:/Users/Alex Heimerl/Desktop/test/pikachu.jpeg"
img = pilimage.open(img_path)
imgByteArr = inputoutput.BytesIO()
img.save(imgByteArr, format='JPEG')
imgByteArr = imgByteArr.getvalue()
img, oldImg = transform_img_fn(imgByteArr)
img = img*(1./255)
model = load_model(modelPath)
model_wo_sm = iutils.keras.graph.model_wo_softmax(model)
# Creating an analyzer
gradient_analyzer = innvestigate.analyzer.GuidedBackprop(model_wo_sm)
# Applying the analyzer
# analysis = gradient_analyzer.analyze(img)
testanalyzer = []
testanalyzer = innvestigate.analyzer.DeepTaylor(model_wo_sm)
analysis = testanalyzer.analyze(img)
testfilter = heatmap_rainbow(analysis)[0]
plot.imshow(testfilter)
plot.show()
imgFinal = graymap(analysis)[0]
imgFinal = np.uint8(imgFinal*255)
img = pilimage.fromarray(imgFinal)
imgByteArr = inputoutput.BytesIO()
img.save(imgByteArr, format='JPEG')
imgByteArr = imgByteArr.getvalue()
plot.imshow(graymap(analysis)[0])
plot.show()
def preprocess(X, net):
X = X.copy()
X = net["preprocess_f"](X)
return X
def postprocess(X, color_conversion, channels_first):
X = X.copy()
X = iutils.postprocess_images(
X, color_coding=color_conversion, channels_first=channels_first)
return X
def image(X):
X = X.copy()
return ivis.project(X, absmax=255.0, input_is_postive_only=True)
def bk_proj(X):
X = ivis.clip_quantile(X, 1)
return ivis.project(X)
def heatmap(X):
X = ivis.gamma(X, minamp=0, gamma=0.95)
return ivis.heatmap(X)
def heatmapgnuplot2(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="gnuplot2", input_is_postive_only=True)
def heatmapCMRmap(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="CMRmap", input_is_postive_only=True)
def heatmapnipy_spectral(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="nipy_spectral", input_is_postive_only=True)
def heatmap_rainbow(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="rainbow", input_is_postive_only=True)
def heatmap_inferno(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="inferno", input_is_postive_only=True)
def heatmap_viridis(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="viridis", input_is_postive_only=True)
def heatmap_gist_heat(X):
X = np.abs(X)
return ivis.heatmap(X, cmap_type="gist_heat", input_is_postive_only=True)
def graymap(X):
return ivis.graymap(np.abs(X), input_is_postive_only=True)
if __name__ == '__main__':
test()