-
Notifications
You must be signed in to change notification settings - Fork 1
/
explainers.py
316 lines (224 loc) · 9.08 KB
/
explainers.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from urllib.error import HTTPError
import matplotlib.pyplot as plt
from PIL import Image
import torch.nn as nn
import numpy as np
import os, json
import matplotlib.patches as patches
import torch
from torchvision import models, transforms
from torch.autograd import Variable
import torch.nn.functional as F
from predictor import Classifier, read_image_from_url
from lime import lime_image
from skimage import io
from skimage.segmentation import mark_boundaries
from coco_dataset import COCO
from PIL import Image as im
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
from sklearn.model_selection import train_test_split
from tqdm import tqdm
# Import dependencies
def get_pil_transform():
transf = transforms.Compose([
transforms.Resize((256, 256)),
transforms.CenterCrop(224)
])
return transf
# Transform PIL image
def lime_explainer(img, clf, pill_transf = get_pil_transform()):
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(np.array(pill_transf(img)),
clf.batch_predict, # classification function
top_labels=5,
hide_color=0,
num_samples=1000) # number of images that will be sent to classification function
return explanation
def saliency_explainer(img, clf, pill_transf=get_pil_transform()):
#we don't need gradients w.r.t. weights for a trained model
for param in clf.model.parameters():
param.requires_grad = False
#transoform input PIL image to torch.Tensor and normalize
input = transforms.ToTensor()(pill_transf(img))
input.unsqueeze_(0)
#we want to calculate gradient of higest score w.r.t. input
#so set requires_grad to True for input
input.requires_grad = True
#forward pass to calculate predictions
preds = clf.model.forward(input)
score, _ = torch.max(preds, 1)
#backward pass to get gradients of score predicted class w.r.t. input image
score.backward()
#get max along channel axis
slc, _ = torch.max(torch.abs(input.grad[0]), dim=0)
#normalize to [0..1]
slc = (slc - slc.min())/(slc.max()-slc.min())
return slc
def visualize_saliency(slc, img, pill_transf=get_pil_transform()):
#plot image and its saleincy map
fig = plt.figure(figsize=(10, 10))
plt.subplot(1, 2, 1)
plt.imshow(img.resize((256, 256)))
plt.xticks([])
plt.yticks([])
plt.subplot(1, 2, 2)
plt.imshow(slc.numpy(), cmap="jet",alpha=0.8)
plt.xticks([])
plt.yticks([])
plt.show()
def similarity(explainer_1, explainer_2):
norm_expl1 = nn.functional.normalize(explainer_1.float(), dim=0)
norm_expl2 = nn.functional.normalize(explainer_2.float(), dim=0)
return norm_expl1 @ norm_expl2
def bbox_mask(img_shape, bbox, transform=get_pil_transform()):
mask = np.zeros(img_shape, dtype=np.uint8)
x_min, y_min, w, h = np.round(bbox).astype(int)
y_max = h + y_min
x_max = w + x_min
mask[y_min:y_max, x_min:x_max] = 1
img_tmp = im.fromarray(mask)
output = transforms.ToTensor()(transform(img_tmp))
return output.squeeze(0)
def visualize_bboxes(image, bboxes, figsize=(5, 5)):
#for bbox visualization...
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(1,1,1)
plt.xticks([])
plt.yticks([])
for box in bboxes:
bb = patches.Rectangle((box[0],box[1]), box[2],box[3], linewidth=2, edgecolor="blue", facecolor="none")
ax.add_patch(bb)
ax.imshow(image)
return fig
def prepare_data(dataset, labels, threshold = 60, include_other = False):
#This function prepares data, to be explained with trees!
#receives coco dataset and corresponding labeled data!
#features, containing bow embeddings!
X = list()
#labels in integer form, to be used in training
y = list()
#label images!
#keeps frequencies, so as to remove infrequent labels!!!
freqs = dict()
for id in tqdm(dataset.coco):
try:
label = labels[str(id)]
X.append(dataset.coco[id]['bow'])
y.append(label)
if label not in freqs:
freqs[label] = 1
else:
freqs[label] += 1
except:
pass
start_length = len(y)
#filter infrequent labels!!!
X_ = list()
y_ = list()
if threshold != -1:
for label, features in tqdm(zip(y, X), desc="filter infrequent classes"):
if freqs[label] >= threshold:
#keep these only!
X_.append(features)
y_.append(label)
else:
if include_other:
#all low frequency classes are bumped to one...
X_.append(features)
y_.append('other')
X = X_
y = y_
finish_length = len(y)
del X_, y_
print('Percentage of filtered variables: ', (1-(finish_length/start_length))*100,"%")
#finds an integer for each unique label!
y_unique = list(set(y))
y_unique.sort()
y_unique = {label: num for num, label in enumerate(y_unique)}
for i in range(len(y)):
y[i] = y_unique[y[i]]
return X, y, y_unique
def tree_explainer(dataset, labels, threshold = 60, include_other = False,
test_size=0.33, random_state=42,
**tree_kwargs):
'''
This explainer uses a tree classifier to produce an explanation
about the predicted labels of a coco classifier!
dataset: coco object
labels: dictionary containing predicted labels from a black box predictor!
threshold:
tree_kwargs: keyword arguments for an sklearn tree classifier!!!
'''
#preparing data for training the tree!!!
X, y, y_unique = prepare_data(dataset, labels,
threshold = threshold, include_other = include_other)
#splitting data train/test...
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=test_size, random_state=random_state)
#define tree...
tree_ = DecisionTreeClassifier(**tree_kwargs)
tree_.fit(X_train, y_train)
print('Traing accuracy: ',tree_.score(X_train, y_train))
print('Testing accuracy: ',tree_.score(X_test, y_test))
return tree_, y_unique
if __name__ == "__main__":
import json
# Load Json File with annotations
with open("./annotations/instances_train2014.json", "rb") as f:
data = json.load(f)
#coco dataset...
coco = COCO(data)
coco2 = COCO(data)
#pruning dataset!!!
coco.prune_dataset(["surfboard"])
coco2.prune_dataset(["pizza"])
#join
coco.join(coco2)
img_url = coco.coco[327758]["url"]
actual_image = read_image_from_url(img_url)
clf = Classifier("resnet18")
explanation_lime = lime_explainer(actual_image, clf)
print(clf.classify(img_url)[0][0])
temp, mask = explanation_lime.get_image_and_mask(explanation_lime.top_labels[0], positive_only=True, num_features=10, hide_rest=False)
img_boundry1 = mark_boundaries(temp/255.0, mask, outline_color = [250.0, 20.0, 5.0])
lime_tensor = torch.from_numpy(mask.flatten())
plt.imshow(img_boundry1)
plt.show()
saliency = saliency_explainer(actual_image, clf)
saliency_tensor = saliency.flatten()
explainer_similarity = similarity(lime_tensor, saliency_tensor)
print(f"Explainer similarity: {explainer_similarity}")
visualize_saliency(saliency, actual_image)
temp_img = coco.coco[327758]
img_url = temp_img["url"]
img_bboxes = temp_img["bboxes"]
# actual_img = io.imread(img_url)
lime_bbox_sim = []
saliency_bbox_sim = []
for box in img_bboxes:
mask_tmp = (bbox_mask(actual_image.size[::-1], box))
lime_bbox_sim.append(similarity(mask_tmp.flatten(), lime_tensor))
saliency_bbox_sim.append(similarity(mask_tmp.flatten(), saliency_tensor))
print(coco.coco[327758]['label_texts'])
print(torch.Tensor(lime_bbox_sim)/torch.sum(torch.Tensor(lime_bbox_sim)))
print(torch.Tensor(saliency_bbox_sim)/torch.sum(torch.Tensor(saliency_bbox_sim)))
#tree explainer...
#calculates bag of words!!!
coco.find_uniques()
coco.bag_of_words()
with open("./saved_labels/labels.json", "r") as f:
labels = json.load(f)
tree_, unique_labels = tree_explainer(coco, labels, max_depth = 3)
import re
print(re.sub('class: ([0-9]+)',
lambda x: 'Class: '+[key for key in unique_labels if int(x.groups(1)[0]) == unique_labels[key]][0],
tree.export_text(tree_,
feature_names=[name for name in coco.unique_objects])))
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=800)
tree.export_graphviz(tree_,
out_file = './tree.dot',
feature_names = [name for name in coco.unique_objects],
class_names=[name for name in unique_labels],
filled = True)
os.system('dot -Tpng tree.dot -o tree.png')