-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
181 lines (152 loc) · 6.51 KB
/
inference.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
# /usr/bin/env python
# coding=utf-8
"""Evaluate the model"""
import json
import logging
import random
import argparse
from tqdm import tqdm
import os
import torch
import numpy as np
import pandas as pd
from metrics import tag_mapping_nearest, tag_mapping_corres
from utils import Label2IdxSub, Label2IdxObj
import utils
from dataloader import CustomDataLoader
# load args
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=2020, help="random seed for initialization")
parser.add_argument('--ex_index', type=str, default=1)
parser.add_argument('--mode', type=str, default="test")
parser.add_argument('--corpus_type', type=str, default="Job")
parser.add_argument('--device_id', type=int, default=0, help="GPU index")
parser.add_argument('--restore_file', default='last', help="name of the file containing weights to reload")
parser.add_argument('--corres_threshold', type=float, default=0.5, help="threshold of global correspondence")
parser.add_argument('--rel_threshold', type=float, default=0.5, help="threshold of relation judgement")
parser.add_argument('--ensure_corres', action='store_true', help="correspondence ablation")
parser.add_argument('--ensure_rel', action='store_true', help="relation judgement ablation")
parser.add_argument('--emb_fusion', type=str, default="concat", help="way to embedding")
def get_metrics(correct_num, predict_num, gold_num):
p = correct_num / predict_num if predict_num > 0 else 0
r = correct_num / gold_num if gold_num > 0 else 0
f1 = 2 * p * r / (p + r) if (p + r) > 0 else 0
return {
'correct_num': correct_num,
'predict_num': predict_num,
'gold_num': gold_num,
'precision': p,
'recall': r,
'f1': f1
}
def span2str(triples, tokens):
def _concat(token_list):
result = ''
for idx, t in enumerate(token_list):
if idx == 0:
result = t
elif t.startswith('##'):
result += t.lstrip('##')
else:
result += ' ' + t
return result
with open(params.data_dir / 'rel2id.json', 'r', encoding='utf-8') as fr:
id2rel = json.load(fr)[0]
output = []
for triple in triples:
rel = triple[-1]
sub_tokens = tokens[triple[0][1]:triple[0][-1]]
obj_tokens = tokens[triple[1][1]:triple[1][-1]]
sub = _concat(sub_tokens)
obj = _concat(obj_tokens)
output.append(" | ".join([sub, id2rel[str(rel)], obj]))
return output
def inference(model, data_iterator, params, ex_params, mark='Val'):
"""Evaluate the model on `steps` batches."""
# set model to evaluation mode
model.eval()
rel_num = params.rel_num
predictions = []
ground_truths = []
correct_num, predict_num, gold_num = 0, 0, 0
for batch in tqdm(data_iterator, unit='Batch', ascii=True):
# to device
batch = tuple(t.to(params.device) if isinstance(t, torch.Tensor) else t for t in batch)
input_ids, attention_mask, input_tokens = batch
bs, seq_len = input_ids.size()
# inference
with torch.no_grad():
pred_seqs, pre_corres, xi, pred_rels = model(input_ids, attention_mask=attention_mask,
ex_params=ex_params)
# (sum(x_i), seq_len)
pred_seqs = pred_seqs.detach().cpu().numpy()
# (bs, seq_len, seq_len)
pre_corres = pre_corres.detach().cpu().numpy()
if ex_params['ensure_rel']:
# (bs,)
xi = np.array(xi)
# (sum(s_i),)
pred_rels = pred_rels.detach().cpu().numpy()
# decode by per batch
xi_index = np.cumsum(xi).tolist()
# (bs+1,)
xi_index.insert(0, 0)
for idx in range(bs):
if ex_params['ensure_rel']:
pre_triples = tag_mapping_corres(predict_tags=pred_seqs[xi_index[idx]:xi_index[idx + 1]],
pre_corres=pre_corres[idx],
pre_rels=pred_rels[xi_index[idx]:xi_index[idx + 1]],
label2idx_sub=Label2IdxSub,
label2idx_obj=Label2IdxObj)
else:
pre_triples = tag_mapping_corres(predict_tags=pred_seqs[idx * rel_num:(idx + 1) * rel_num],
pre_corres=pre_corres[idx],
label2idx_sub=Label2IdxSub,
label2idx_obj=Label2IdxObj)
pred_triples = span2str(pre_triples, input_tokens[idx])
# predictions.append(" && ".join(list(set(pred_triples))))
predictions.append(" && ".join(pred_triples))
return predictions
if __name__ == '__main__':
args = parser.parse_args()
params = utils.Params(ex_index=args.ex_index, corpus_type=args.corpus_type)
ex_params = {
'corres_threshold': args.corres_threshold,
'rel_threshold': args.rel_threshold,
'ensure_corres': args.ensure_corres,
'ensure_rel': args.ensure_rel,
'emb_fusion': args.emb_fusion
}
torch.cuda.set_device(args.device_id)
print('current device:', torch.cuda.current_device())
mode = args.mode
# Set the random seed for reproducible experiments
random.seed(args.seed)
torch.manual_seed(args.seed)
params.seed = args.seed
# Set the logger
utils.set_logger()
# get dataloader
dataloader = CustomDataLoader(params)
# Define the model
logging.info('Loading the model...')
logging.info(f'Path: {os.path.join(params.model_dir, args.restore_file)}.pth.tar')
# Reload weights from the saved file
model, optimizer = utils.load_checkpoint(os.path.join(params.model_dir, args.restore_file + '.pth.tar'))
model.to(params.device)
logging.info('- done.')
logging.info("Loading the dataset...")
loader = dataloader.get_dataloader(data_sign=mode, ex_params=ex_params)
logging.info('-done')
logging.info("Starting prediction...")
predictions = inference(model, loader, params, ex_params, mark=mode)
with open(params.data_dir / 'raw_data_per.json', 'r', encoding='utf-8') as f_src:
src = json.load(f_src)
df = pd.DataFrame(
{
'text': [sample['text'] for sample in src],
'prediction': predictions
}
)
df.to_csv(params.ex_dir / f'{mode}_result.csv')
logging.info('-done')