-
Notifications
You must be signed in to change notification settings - Fork 1
/
VeriX.py
259 lines (252 loc) · 12.1 KB
/
VeriX.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import random
import numpy as np
import onnx
import onnxruntime as ort
from skimage.color import label2rgb
from matplotlib import pyplot as plt
# import sys
# sys.path.insert(0, "Marabou")
"""
After installing Marabou, load it from maraboupy.
"""
from maraboupy import Marabou
class VeriX:
"""
This is the VeriX class to take in an image and a neural network, and then output an explanation.
"""
image = None
keras_model = None
mara_model = None
traverse: str
sensitivity = None
dataset: str
label: int
inputVars = None
outputVars = None
epsilon: float
"""
Marabou options: 'timeoutInSeconds' is the timeout parameter.
"""
options = Marabou.createOptions(numWorkers=16,
timeoutInSeconds=300,
verbosity=0,
solveWithMILP=True)
def __init__(self,
dataset,
image,
model_path,
plot_original=True):
"""
To initialize the VeriX class.
:param dataset: 'MNIST' or 'GTSRB'.
:param image: an image array of shape (width, height, channel).
:param model_path: the path to the neural network.
:param plot_original: if True, then plot the original image.
"""
self.dataset = dataset
self.image = image
"""
Load the onnx model.
"""
self.onnx_model = onnx.load(model_path)
self.onnx_session = ort.InferenceSession(model_path)
prediction = self.onnx_session.run(None, {self.onnx_model.graph.input[0].name: np.expand_dims(image, axis=0)})
prediction = np.asarray(prediction[0])
self.label = prediction.argmax()
"""
Load the onnx model into Marabou.
Note: to ensure sound and complete analysis, load the model before the softmax activation function;
if the model is trained from logits directly, then load the whole model.
"""
self.mara_model = Marabou.read_onnx(model_path)
if self.onnx_model.graph.node[-1].op_type == "Softmax":
mara_model_output = self.onnx_model.graph.node[-1].input
else:
mara_model_output = None
self.mara_model = Marabou.read_onnx(filename=model_path,
outputNames=mara_model_output)
self.inputVars = np.arange(image.shape[0] * image.shape[1])
self.outputVars = self.mara_model.outputVars[0].flatten()
if plot_original:
save_figure(image=image,
path=f"original-predicted-as-{self.label}.png",
cmap="gray" if self.dataset == 'MNIST' else None)
def traversal_order(self,
traverse="heuristic",
plot_sensitivity=True,
seed=0):
"""
To compute the traversal order of checking all the pixels in the image.
:param traverse: 'heuristic' (by default) or 'random'.
:param plot_sensitivity: if True, plot the sensitivity map.
:param seed: if traverse by 'random', then set a random seed.
:return: an updated inputVars that contains the traversal order.
"""
self.traverse = traverse
if self.traverse == "heuristic":
width, height, channel = self.image.shape[0], self.image.shape[1], self.image.shape[2]
temp = self.image.reshape(width * height, channel)
image_batch = np.kron(np.ones(shape=(width * height, 1, 1), dtype=temp.dtype), temp)
image_batch_manip = image_batch.copy()
for i in range(width * height):
"""
Different ways to compute sensitivity: use pixel reversal for MNIST and deletion for GTSRB.
"""
if self.dataset == "MNIST":
image_batch_manip[i][i][:] = 1 - image_batch_manip[i][i][:]
elif self.dataset == "GTSRB":
image_batch_manip[i][i][:] = 0
else:
print("Dataset not supported: try 'MNIST' or 'GTSRB'.")
image_batch = image_batch.reshape((width * height, width, height, channel))
predictions = self.onnx_session.run(None, {self.onnx_model.graph.input[0].name: image_batch})
predictions = np.asarray(predictions[0])
image_batch_manip = image_batch_manip.reshape((width * height, width, height, channel))
predictions_manip = self.onnx_session.run(None, {self.onnx_model.graph.input[0].name: image_batch_manip})
predictions_manip = np.asarray(predictions_manip[0])
difference = predictions - predictions_manip
features = difference[:, self.label]
sorted_index = features.argsort()
self.inputVars = sorted_index
self.sensitivity = features.reshape(width, height)
if plot_sensitivity:
save_figure(image=self.sensitivity, path=f'{self.dataset}-sensitivity-{self.traverse}.png')
elif self.traverse == "random":
random.seed(seed)
random.shuffle(self.inputVars)
else:
print("Traversal not supported: try 'heuristic' or 'random'.")
def get_explanation(self,
epsilon,
plot_explanation=True,
plot_counterfactual=False,
plot_timeout=False):
"""
To compute the explanation for the model and the neural network.
:param epsilon: the perturbation magnitude.
:param plot_explanation: if True, plot the explanation.
:param plot_counterfactual: if True, plot the counterfactual(s).
:param plot_timeout: if True, plot the timeout pixel(s).
:return: an explanation, and possible counterfactual(s).
"""
unsat_set = []
sat_set = []
timeout_set = []
width, height, channel = self.image.shape[0], self.image.shape[1], self.image.shape[2]
image = self.image.reshape(width * height, channel)
for pixel in self.inputVars:
for i in self.inputVars:
"""
Set constraints on the input variables.
"""
if i == pixel or i in unsat_set:
"""
Set allowable perturbations on the current pixel and the irrelevant pixels.
"""
if self.dataset == "MNIST":
self.mara_model.setLowerBound(i, max(0, image[i][:] - epsilon))
self.mara_model.setUpperBound(i, min(1, image[i][:] + epsilon))
elif self.dataset == "GTSRB":
self.mara_model.setLowerBound(3 * i, max(0, image[i][0] - epsilon))
self.mara_model.setUpperBound(3 * i, min(1, image[i][0] + epsilon))
self.mara_model.setLowerBound(3 * i + 1, max(0, image[i][1] - epsilon))
self.mara_model.setUpperBound(3 * i + 1, min(1, image[i][1] + epsilon))
self.mara_model.setLowerBound(3 * i + 2, max(0, image[i][2] - epsilon))
self.mara_model.setUpperBound(3 * i + 2, min(1, image[i][2] + epsilon))
else:
print("Dataset not supported: try 'MNIST' or 'GTSRB'.")
else:
"""
Make sure the other pixels are fixed.
"""
if self.dataset == "MNIST":
self.mara_model.setLowerBound(i, image[i][:])
self.mara_model.setUpperBound(i, image[i][:])
elif self.dataset == "GTSRB":
self.mara_model.setLowerBound(3 * i, image[i][0])
self.mara_model.setUpperBound(3 * i, image[i][0])
self.mara_model.setLowerBound(3 * i + 1, image[i][1])
self.mara_model.setUpperBound(3 * i + 1, image[i][1])
self.mara_model.setLowerBound(3 * i + 2, image[i][2])
self.mara_model.setUpperBound(3 * i + 2, image[i][2])
else:
print("Dataset not supported: try 'MNIST' or 'GTSRB'.")
for j in range(len(self.outputVars)):
"""
Set constraints on the output variables.
"""
if j != self.label:
self.mara_model.addInequality([self.outputVars[self.label], self.outputVars[j]],
[1, -1], -1e-6,
isProperty=True)
exit_code, vals, stats = self.mara_model.solve(options=self.options, verbose=False)
"""
additionalEquList.clear() is to clear the output constraints.
"""
self.mara_model.additionalEquList.clear()
if exit_code == 'sat' or exit_code == 'TIMEOUT':
break
elif exit_code == 'unsat':
continue
"""
clearProperty() is to clear both input and output constraints.
"""
self.mara_model.clearProperty()
"""
If unsat, put the pixel into the irrelevant set;
if timeout, into the timeout set;
if sat, into the explanation.
"""
if exit_code == 'unsat':
unsat_set.append(pixel)
elif exit_code == 'TIMEOUT':
timeout_set.append(pixel)
elif exit_code == 'sat':
sat_set.append(pixel)
if plot_counterfactual:
counterfactual = [vals.get(i) for i in self.mara_model.inputVars[0].flatten()]
counterfactual = np.asarray(counterfactual).reshape(self.image.shape)
prediction = [vals.get(i) for i in self.outputVars]
prediction = np.asarray(prediction).argmax()
save_figure(image=counterfactual,
path="counterfactual-at-pixel-%d-predicted-as-%d.png" % (pixel, prediction),
cmap="gray" if self.dataset == 'MNIST' else None)
if plot_explanation:
mask = np.zeros(self.inputVars.shape).astype(bool)
mask[sat_set] = True
mask[timeout_set] = True
plot_shape = self.image.shape[0:2] if self.dataset == "MNIST" else self.image.shape
save_figure(image=label2rgb(mask.reshape(self.image.shape[0:2]),
self.image.reshape(plot_shape),
colors=[[0, 1, 0]] if self.traverse == 'heuristic' else [[1, 0, 0]],
bg_label=0,
saturation=1),
path="explanation-%d.png" % (len(sat_set) + len(timeout_set)))
if plot_timeout:
mask = np.zeros(self.inputVars.shape).astype(bool)
mask[timeout_set] = True
plot_shape = self.image.shape[0:2] if self.dataset == "MNIST" else self.image.shape
save_figure(image=label2rgb(mask.reshape(self.image.shape[0:2]),
self.image.reshape(plot_shape),
colors=[[0, 1, 0]] if self.traverse == 'heuristic' else [[1, 0, 0]],
bg_label=0,
saturation=1),
path="timeout-%d.png" % len(timeout_set))
def save_figure(image, path, cmap=None):
"""
To plot figures.
:param image: the image array of shape (width, height, channel)
:param path: figure name.
:param cmap: 'gray' if to plot gray scale image.
:return: an image saved to the designated path.
"""
fig = plt.figure()
ax = plt.Axes(fig, [-0.5, -0.5, 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
if cmap is None:
plt.imshow(image)
else:
plt.imshow(image, cmap=cmap)
plt.savefig(path, bbox_inches='tight')
plt.close(fig)