-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatistic.py
More file actions
359 lines (343 loc) · 15.1 KB
/
Copy pathstatistic.py
File metadata and controls
359 lines (343 loc) · 15.1 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
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
357
358
359
import ujson as json
import numpy as np
from prepro import read_docred
from torch.utils.data import DataLoader
from utils import collate_fn
from tqdm import tqdm
docred_rel2id = json.load(open('meta/rel2id.json', 'r'))
def visualize_doc(file_in, file_out, id_list):
rel_info_file = '/home/ubuntu/SemiSup_DocRE/dataset/docred/rel_info.json'
with open(rel_info_file, "r") as f:
rel_info = json.load(f)
with open(file_in, "r") as fh:
data = json.load(fh)
docs = list()
for i in id_list:
sample = dict()
sample['id'] = i
sample['title'] = data[i]['title']
sents = data[i]['sents']
vertexset = data[i]['vertexSet']
labels = data[i]['labels']
sample['sents'] = '\n'.join([' '.join(sents[k]) for k in range(len(sents))])
id2entity = dict()
for k in range(len(vertexset)):
id2entity[k] = vertexset[k][0]['name']
sample['entities'] = id2entity
sample_labels = list()
for label in labels:
sample_label = dict()
sample_label['subject'] = vertexset[label['h']][0]['name']
sample_label['object'] = vertexset[label['t']][0]['name']
sample_label['relation'] = rel_info[label['r']]
# sample_label['h'] = [vertexset[label['h']][k]['name'] for k in range(len(vertexset[label['h']]))]
# sample_label['t'] = [vertexset[label['t']][k]['name'] for k in range(len(vertexset[label['t']]))]
sample_labels.append(sample_label)
sample['instances'] = sample_labels
docs.append(sample)
with open(file_out, "w") as f:
json.dump(docs, f)
return
def id2info(file_rel2id, file_rel2info, file_out):
with open(file_rel2id, "r") as f:
rel2id = json.load(f)
with open(file_rel2info, "r") as f:
rel2info = json.load(f)
id2info = dict()
for rel in rel2id.keys():
if rel == 'Na':
continue
id2info[rel2id[rel]] = rel2info[rel]
id2info_sorted = dict()
for i in range(1,97):
id2info_sorted[i] = id2info[i]
with open(file_out, "w") as f:
json.dump(id2info_sorted, f)
return
def visualize_predicts(file_out, hts, dl, lt):
predicts = dict()
for i in range(len(hts)):
distant_label = np.nonzero(dl[i] == 1).squeeze(dim=1).cpu().numpy().tolist()
teacher_label = np.nonzero(lt[i] > lt[i][0]).squeeze(dim=1).cpu().numpy().tolist()
teacher_logit = lt[i].cpu().numpy().tolist()
distant_label_logit = []
distant_label_rank = []
threshold_logit = teacher_logit[0]
for label in distant_label:
count = 0
distant_label_logit.append(teacher_logit[label])
for k in range(1, len(teacher_logit)):
if k == label:
continue
if teacher_logit[k] > teacher_logit[label]:
count += 1
distant_label_rank.append(count+1)
predict = dict()
predict['hts'] = hts[i]
predict['distant_label'] = distant_label
predict['dl_logit'] = distant_label_logit
predict['dl_rank'] = distant_label_rank
predict['th_logit'] = threshold_logit
predict['teacher_label'] = teacher_label
predicts[i] = predict
with open(file_out, "w") as f:
json.dump(predicts, f)
return
def visualize_test_result(file_out, hts, logits, labels):
predicts = dict()
for i in range(len(hts)):
label = np.nonzero(labels[i] == 1).squeeze(dim=1).cpu().numpy().tolist()
pred = np.nonzero(logits[i] > logits[i][0]).squeeze(dim=1).cpu().numpy().tolist()
logit = logits[i].cpu().numpy().tolist()
label_logit = []
label_rank = []
threshold_logit = logit[0]
for lb in label:
count = 0
label_logit.append(logit[lb])
for k in range(1, len(logit)):
if k == lb:
continue
if logit[k] > logit[lb]:
count += 1
label_rank.append(count+1)
predict = dict()
predict['hts'] = hts[i]
predict['pred'] = pred
predict['label'] = label
predict['lb_logit'] = label_logit
predict['lb_rank'] = label_rank
predict['th_logit'] = threshold_logit
predicts[i] = predict
with open(file_out, "w") as f:
json.dump(predicts, f)
return
def visualize_dev_wrong(dev_file, file_out, model, tokenizer):
dev_features = read_docred(dev_file, tokenizer, max_seq_length=1024)
dev_dataloader = DataLoader(dev_features, batch_size=1, shuffle=False, collate_fn=collate_fn, drop_last=True)
wrong_result = dict()
for idx, batch in enumerate(dev_dataloader):
predicts = dict()
inputs = {'input_ids': batch[0].to('cuda'),
'attention_mask': batch[1].to('cuda'),
'entity_pos': batch[3],
'hts': batch[4],
}
labels = batch[2].to('cuda')
logits = model(**inputs)
hts = inputs['hts'][0]
for i in range(len(hts)):
label = np.nonzero(labels[i] == 1).squeeze(dim=1).cpu().numpy().tolist()
pred = np.nonzero(logits[i] > logits[i][0]).squeeze(dim=1).cpu().numpy().tolist()
if label == [0] and pred == []:
continue
if set(label) == set(pred):
continue
logit = logits[i].cpu().numpy().tolist()
label_logit = []
label_rank = []
threshold_logit = logit[0]
for lb in label:
count = 0
label_logit.append(logit[lb])
for k in range(1, len(logit)):
if k == lb:
continue
if logit[k] > logit[lb]:
count += 1
label_rank.append(count+1)
predict = dict()
predict['hts'] = hts[i]
predict['pred'] = pred
predict['label'] = label
predict['lb_logit'] = label_logit
predict['lb_rank'] = label_rank
predict['th_logit'] = threshold_logit
predicts[i] = predict
wrong_result[idx] = predicts
with open(file_out, "w") as f:
json.dump(wrong_result, f)
def visualize_dev(dev_file, file_out_prefix, model, tokenizer):
dev_features = read_docred(dev_file, tokenizer, max_seq_length=1024)
dev_dataloader = DataLoader(dev_features, batch_size=1, shuffle=False, collate_fn=collate_fn, drop_last=True)
tp, tn, fp, fn_rank_wrong, fn_rank_right = [], [], [], [], []
num, num_tp, num_tn, num_fp, num_fn_rank_wrong, num_fn_rank_right = 0, 0, 0, 0, 0, 0
num_tp_exp, num_tn_exp, num_fp_exp, num_fn_rank_wrong_exp, num_fn_rank_right_exp = 0, 0, 0, 0, 0
for idx, batch in enumerate(dev_dataloader):
inputs = {'input_ids': batch[0].to('cuda'),
'attention_mask': batch[1].to('cuda'),
'entity_pos': batch[3],
'hts': batch[4],
}
labels = batch[2].to('cuda')
logits = model(**inputs)
hts = inputs['hts'][0]
input_ids = inputs['input_ids'][0]
entity_pos = inputs['entity_pos'][0]
for i in range(len(hts)):
num += 1
label = np.nonzero(labels[i] == 1).squeeze(dim=1).detach().cpu().numpy().tolist()
pred = np.nonzero(logits[i] > logits[i][0]).squeeze(dim=1).detach().cpu().numpy().tolist()
logit = logits[i].detach().cpu().numpy().tolist()
item = dict()
item['batch_id'] = idx
item['hts_id'] = i
item['text'] = tokenizer.decode(input_ids)
item['hts'] = hts[i]
h_pos = entity_pos[hts[i][0]][0] # 选第0个mention可视化一下
t_pos = entity_pos[hts[i][1]][0]
item['subject'] = tokenizer.decode(input_ids[h_pos[0] + 1: h_pos[1] + 1])
item['object'] = tokenizer.decode(input_ids[t_pos[0] + 1: t_pos[1] + 1])
item['logit'] = logit
item['label'] = label
item['pred'] = pred
item['th_logit'] = logit[0]
if label == [0] and pred == []:
# tn
num_tn += 1
# if num_tn_exp <= 20:
if idx == 9 and num_tn_exp <= 5:
num_tn_exp += 1
tn.append(item)
continue
label_logit = []
label_rank = []
for lb in label:
count = 0
label_logit.append(logit[lb])
for k in range(1, len(logit)):
if k == lb:
continue
if logit[k] > logit[lb]:
count += 1
label_rank.append(count + 1)
if set(label) == set(pred):
# tp
num_tp += 1
# if len(label) > 1 and num_tp_exp <= 20:
if idx == 9 and num_tp_exp <= 5:
num_tp_exp += 1
item['label_logit'] = label_logit
tp.append(item)
continue
if pred == []:
# fn
if set(label_rank) in [{1}, {1, 2}, {1, 2, 3}]:
num_fn_rank_right += 1
# if len(label) > 1 and num_fn_rank_right_exp <= 20:
if idx == 9:
num_fn_rank_right_exp += 1
item['label_logit'] = label_logit
item['label_rank'] = label_rank
fn_rank_right.append(item)
else:
num_fn_rank_wrong += 1
# if len(label) > 1 and num_fn_rank_wrong_exp <= 20:
if idx == 9:
num_fn_rank_wrong_exp += 1
item['label_logit'] = label_logit
item['label_rank'] = label_rank
fn_rank_wrong.append(item)
else:
# fp
num_fp += 1
# if len(label) > 1 and num_fp_exp <= 20:
if idx == 9:
num_fp_exp += 1
item['label_logit'] = label_logit
item['label_rank'] = label_rank
pred_logit = []
for p in pred:
pred_logit.append(logit[p])
item['pred_logit'] = pred_logit
fp.append(item)
assert num == num_tp + num_tn + num_fp + num_fn_rank_wrong + num_fn_rank_right
stat = dict()
stat['num'], stat['num_tp'], stat['num_tn'], stat['num_fp'], stat['num_fn_rank_wrong'], \
stat['num_fn_rank_right']= num, num_tp, num_tn, num_fp, num_fn_rank_wrong, num_fn_rank_right
with open(file_out_prefix+'_stat.json', 'w') as f:
json.dump(stat, f)
with open(file_out_prefix+'_tp.json', 'w') as f:
json.dump(tp, f)
with open(file_out_prefix+'_tn.json', 'w') as f:
json.dump(tn, f)
with open(file_out_prefix+'_fp.json', 'w') as f:
json.dump(fp, f)
with open(file_out_prefix+'_fn_rank_wrong.json', 'w') as f:
json.dump(fn_rank_wrong, f)
with open(file_out_prefix+'_fn_rank_right.json', 'w') as f:
json.dump(fn_rank_right, f)
def visualize_dev_example(dev_file, file_out_prefix, model, tokenizer, batch_id, hts_id):
dev_features = read_docred(dev_file, tokenizer, max_seq_length=1024)
dev_dataloader = DataLoader(dev_features, batch_size=1, shuffle=False, collate_fn=collate_fn, drop_last=True)
for idx, batch in enumerate(dev_dataloader):
if idx != batch_id:
continue
else:
inputs = {'input_ids': batch[0].to('cuda'),
'attention_mask': batch[1].to('cuda'),
'entity_pos': batch[3],
'hts': batch[4],
}
labels = batch[2].to('cuda')
logits = model(**inputs)
hts = inputs['hts'][0]
input_ids = inputs['input_ids'][0]
entity_pos = inputs['entity_pos'][0]
for i in range(len(hts)):
if i != hts_id:
continue
else:
label = np.nonzero(labels[i] == 1).squeeze(dim=1).detach().cpu().numpy().tolist()
pred = np.nonzero(logits[i] > logits[i][0]).squeeze(dim=1).detach().cpu().numpy().tolist()
logit = logits[i].detach().cpu().numpy().tolist()
item = dict()
item['batch_id'] = idx
item['hts_id'] = i
item['text'] = tokenizer.decode(input_ids)
item['hts'] = hts[i]
h_pos = entity_pos[hts[i][0]][0] # 选第0个mention可视化一下
t_pos = entity_pos[hts[i][1]][0]
item['subject'] = tokenizer.decode(input_ids[h_pos[0] + 1: h_pos[1] + 1])
item['object'] = tokenizer.decode(input_ids[t_pos[0] + 1: t_pos[1] + 1])
item['logit'] = logit
item['label'] = label
item['pred'] = pred
item['th_logit'] = logit[0]
label_logit = []
label_rank = []
for lb in label:
count = 0
label_logit.append(logit[lb])
for k in range(1, len(logit)):
if k == lb:
continue
if logit[k] > logit[lb]:
count += 1
label_rank.append(count + 1)
item['label_logit'] = label_logit
item['label_rank'] = label_rank
with open(file_out_prefix+'_example.json','w') as f:
json.dump(item, f)
break
break
def relation_hist(file_in):
if file_in == "":
return None
with open(file_in, "r") as fh:
data = json.load(fh)
relation_hist = [0] * 97
for sample in tqdm(data, desc="Example"):
# label里给的三元组,以字典形式收集,train_triple[(h,t)]=[{r,evidence},...]
train_triple = {}
if "labels" in sample:
for label in sample['labels']:
evidence = label['evidence']
r = int(docred_rel2id[label['r']])
if (label['h'], label['t']) not in train_triple:
train_triple[(label['h'], label['t'])] = [{'relation': r, 'evidence': evidence}]
relation_hist[r] += 1
else:
train_triple[(label['h'], label['t'])].append({'relation': r, 'evidence': evidence})
relation_hist[r] += 1
return relation_hist