-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpretability.py
356 lines (301 loc) · 13.6 KB
/
interpretability.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import torch
import torch.nn as nn
from transformers import BertTokenizer, BertModel, AutoTokenizer, AutoModel
from captum.attr import IntegratedGradients, LayerIntegratedGradients, visualization
import numpy as np
import os
import time
import sys
from os import system, name
from datetime import datetime
import json
import matplotlib.pyplot as plt
from models.dronelog_inter import DroneLogInter
import pandas as pd
from matplotlib.colors import to_rgba
from generate_report import generate_report
import pdfkit
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--embedding', type=str, default='bert', choices=['bert', 'drone-sbert'])
parser.add_argument('--target-label', type=str, default='high', choices=['high', 'medium', 'low', 'normal'])
def get_config():
config_file = open('config.json')
config_file = json.load(config_file)
now = datetime.now()
now = now.strftime("%Y%m%d_%H%M%S")
output_dir = os.path.join(config_file['output_dir'], now)
# output_dir = os.path.join(config_file['output_dir'], '27112022_190057')
# previous_step = 0
# previous_status = False
use_cuda = True if torch.cuda.is_available() == True else False
wkhtml_path = ""
if name == 'nt':
wkhtml_path = config_file['wkhtml_path']['windows']
# for mac and linux(here, os.name is 'posix')
else:
wkhtml_path = config_file['wkhtml_path']['linux']
return {
"output_dir": output_dir,
"model_dir": config_file['model_dir'],
# "previous_step": previous_step,
# "previous_status": previous_status,
"wkhtml_path": wkhtml_path,
"app_version": config_file['app_version'],
"use_cuda": use_cuda,
"evidence_dir": config_file['dataset_path'],
"evidence_filename": config_file['filename'],
}
def label2class(label):
if label[0] == 1:
return 0, "high"
else:
if label[2] == 1:
return 2, "medium"
elif label[1] == 1:
return 1, "low"
else:
return 3, "normal"
label2idx = {
'high': 0,
'low': 1,
'medium':2,
'normal': 3,
}
class2color = {
'normal': '#4CAF50',
'low': '#FFC107',
'medium': '#FF5722',
'high': '#FF5722',
}
def reconstruct_tokens(tokens, attributions):
words = []
attribution_score = []
current_word = ""
current_attr = 0
for token, attribution in zip(tokens, attributions):
if token.startswith("##"):
current_word += token[2:] # Remove "##" and append to the current word
current_attr += attribution
else:
if current_word:
words.append(current_word)
attribution_score.append(current_attr)
current_word = token
current_attr = attribution
# Append the last word
if current_word:
words.append(current_word)
attribution_score.append(current_attr)
return words, attribution_score
def infer_pred(model, input_ids, attention_mask):
logits = model(input_ids, attention_mask)
# return torch.argmax(torch.softmax(model(input)))
# for logits in logitss:
# after_sigmoid = [1 if (torch.nn.Sigmoid(logits) >= 0.5) else 0]
[after_sigmoid] = torch.sigmoid(logits).cpu().detach().numpy()
# print(f'after_sigmoid: {after_sigmoid}')
# after_sigmoid = [1 if (torch.sigmoid(element) >= 0.5) else 0 for element in after_sigmoid]
# print(f'after_sigmoid: {after_sigmoid}')
vector_label = [1 if element >= 0.5 else 0 for element in after_sigmoid]
# print(f'vector_label: {vector_label}')
labelidx, label = label2class(vector_label)
label_prob = after_sigmoid[labelidx]
if label == 'normal':
label_prob = 1 - after_sigmoid[labelidx]
return label, label_prob
def scale_attribution(distribution):
"""
Scales the input distribution to the range [-1, 1].
Parameters:
distribution (numpy.ndarray): The input distribution of values to be scaled.
Returns:
numpy.ndarray: The scaled distribution with values in the range [-1, 1].
"""
distribution = np.asarray(distribution)
min_val = np.min(distribution)
max_val = np.max(distribution)
scaled_distribution = 2 * (distribution - min_val) / (max_val - min_val) - 1
return scaled_distribution
def add_attributions_to_visualizer(attributions, text, pred, pred_ind, label, attr_label, delta, vis_data_records):
# attributions = attributions.sum(dim=2).squeeze(0)
# attributions = attributions / torch.norm(attributions)
# attributions = attributions.cpu().detach().numpy()
attributions = np.array(attributions)
# storing couple samples in an array for visualization purposes
vis_data_records.append(visualization.VisualizationDataRecord(
attributions,
pred,
pred_ind,
label,
attr_label,
attributions.sum(),
text,
delta))
vis_data_records_ig = []
def interpret(model, tokenizer, max_seq_length, text, label, attr_label):
# device = "cpu"
# bert_model_name = "bert-base-cased"
# tokenizer = BertTokenizer.from_pretrained(bert_model_name)
# bert_model = BertModel.from_pretrained(bert_model_name).to(device)
# max_seq_length = 64
# # Load your model
# model_path = os.path.join('best_models/investigate_15/filtered/gru/pytorch_model.pt')
# model = DroneLogInter(bert_model, 'gru',
# 1, 3, False, True, 384, 'avg', False, False, 4, None, False, 'cross_entropy').to(device)
# model.load_state_dict(torch.load(model_path))
model.eval()
# Load the tokenizer
# tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# Example text
# text = "Fly with caution and ensure the aircraft remains within your line of sight."
# label = 'normal'
labelidx = label2idx.get(label)
# print(f'labelidx: {labelidx}')
# Tokenize the input text
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=max_seq_length)
input_ids = inputs['input_ids']
attention_mask = inputs['attention_mask']
lig = LayerIntegratedGradients(model, model.bert_model.embeddings)
pred_label, pred_prob = infer_pred(model, input_ids, attention_mask)
target_class = label2idx.get(attr_label) # Example target class
attributions, delta = lig.attribute(inputs=input_ids,
baselines=input_ids*0,
additional_forward_args=(attention_mask,),
target=target_class,
return_convergence_delta=True)
# Sum the attributions across embedding dimensions
attributions = attributions.sum(dim=-1).squeeze(0)
# print(f'sum_attr: {attributions}')
# Normalize the attributions for better visualization
attributions = attributions / torch.norm(attributions)
# print(f'normalized: {attributions}')
# attributions = (attributions - attributions.min()) / (attributions.max() - attributions.min())
# Convert attributions to numpy
attributions = attributions.cpu().detach().numpy()
# Get the tokens
tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
tokens, attributions = reconstruct_tokens(tokens, attributions)
add_attributions_to_visualizer(attributions, tokens, pred_prob, pred_label, label, attr_label, delta, vis_data_records_ig)
return attributions, tokens, label, pred_label, pred_prob
# Function to plot attributions
# def plot_attributions(tokens, attributions, label, filename):
# fig, ax = plt.subplots(figsize=(12, 2))
# ax.axis('off')
# color = class2color.get(label)
# x = 0.05 # starting x coordinate for the first word
# y = 0.5 # y coordinate, same for all words to display in one line
# for token, attribution in zip(tokens, attributions):
# # print(f'attributions: {attributions}')
# if attribution < 0:
# attribution = abs(attribution)
# color = class2color.get('high')
# rgba_color = to_rgba(color, alpha=attribution)
# bbox_props = dict(boxstyle="round,pad=0.3", edgecolor='none', facecolor=rgba_color)
# ax.text(x, y, token, ha='center', va='center', rotation=0, size=12, bbox=bbox_props)
# x += len(token) * 0.02 + 0.05 # adjust x position for the next word
# # plt.show()
# plt.savefig(os.path.join('visualization', 'interpretation', f'{filename}.png'))
# plt.close()
# Function to plot attributions with text wrapping
def plot_attributions(tokens, attributions, label, filename):
fig, ax = plt.subplots(figsize=(12, 2))
ax.axis('off')
# color = class2color.get(label)
# Start coordinates
x = 0.05
y = 0.9
line_height = 0.20 # Adjust the line height for wrapping
attributions = scale_attribution(attributions)
# print(f'scaled: {attributions}')
for token, attribution in zip(tokens, attributions):
if attribution < 0:
# print(f'negative: {attribution}')
attribution = abs(attribution)
color = '#FF5722'
# color = '#4CAF50'
rgba_color = to_rgba(color, alpha=attribution)
bbox_props = dict(boxstyle="round,pad=0.3", edgecolor='none', facecolor=rgba_color)
else:
color = '#4CAF50'
rgba_color = to_rgba(color, alpha=attribution)
bbox_props = dict(boxstyle="round,pad=0.3", edgecolor='none', facecolor=rgba_color)
# print(f'positive: {attribution}')
# print(f'token: {token}')
# print(f'color fuck you?: {color}')
# Check if the token fits in the current line
if x + len(token) * 0.02 > 1.0:
x = 0.05 # Reset x to the start of the line
y -= line_height # Move to the next line
ax.text(x, y, token, ha='left', va='center', rotation=0, size=12, bbox=bbox_props)
x += len(token) * 0.02 + 0.03 # Adjust x position for the next word, add space
plt.savefig(os.path.join('visualization', 'interpretation-high-drone-sbert', f'{filename}.png'))
plt.savefig(os.path.join('visualization', 'interpretation-high-drone-sbert', f'{filename}.pdf'))
plt.close()
def main():
args = parser.parse_args()
embedding = args.embedding
attribution_label = args.target_label
output_dir = os.path.join('visualization', 'interpretability')
# Get the configuration
config = get_config()
config['model_dir'] = 'best_model' if embedding == 'bert' else 'lstm'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Starting interpretability report generation...\n")
time.sleep(2)
# Load the model
print("Loading the model...")
# model_dir = os.path.join(config['model_dir'], 'pytorch_model.pt')
model_path = os.path.join(config['model_dir'], 'pytorch_model.pt')
if not os.path.exists(config['model_dir']):
print("The model not found!")
sys.exit(0)
max_seq_length = 64
device = "cpu"
if embedding == 'bert':
bert_model_name = "bert-base-cased"
tokenizer = BertTokenizer.from_pretrained(bert_model_name)
bert_model = BertModel.from_pretrained(bert_model_name).to(device)
model = DroneLogInter(bert_model, 'gru', 1, 3, False, True, 384, 'avg', False, False, 4, None, False, 'logloss').to(device)
else:
bert_model_name = "drone-severity"
tokenizer = AutoTokenizer.from_pretrained(bert_model_name)
bert_model = AutoModel.from_pretrained(bert_model_name).to(device)
# # Load your model
model = DroneLogInter(bert_model, 'lstm', 1, 3, False, True, 384, 'avg', False, False, 4, None, False, 'focal').to(device)
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
print("Model loaded successfully...")
# Load the test set
print("Loading the test set...")
evidence_file = os.path.join(config['evidence_dir'], config['evidence_filename'])
if not os.path.isfile(evidence_file):
print("The test set not found!")
sys.exit(0)
test_set = pd.read_csv(os.path.join(config['evidence_dir'], config['evidence_filename'])).sort_values(by='label')
print("Test set loaded successfully...")
print("Start interpreting...")
attribution_list = []
for index, row in test_set.iterrows():
# print(f'row: {row}')
# print(f'row[0]: {row[0]}')
# print(f'row[1]: {row[1]}')
# return 0
attributions, tokens, label, pred_label, pred_prob = interpret(model, tokenizer, max_seq_length, row['message'], row['label'], attribution_label)
attribution_list.append([attributions, tokens, label, pred_label, pred_prob])
# plot_attributions(tokens, attributions, 'high', f'test_{index}')
# print(attributions, tokens, label, pred_label, pred_prob)
# if index >= 10:
# break
html_output = visualization.visualize_text(vis_data_records_ig)
with open(os.path.join(output_dir, f'report-{embedding}-{attribution_label}.html'), 'w') as f:
f.write(html_output.data)
path_to_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config_wkhtml = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
pdfkit.from_string(html_output.data, os.path.join(output_dir, f'interpretability-report-{embedding}-{attribution_label}.pdf'), configuration=config_wkhtml)
print("Finish interpreting...")
# generate_report(config, attribution_list)
print("Start generating forensic report...")
print('Report has generated successfully.')
if __name__ == "__main__":
main()