-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
212 lines (153 loc) · 7.52 KB
/
test.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
import sys
sys.path.append('../')
import os
if 'p' in os.environ:
os.environ['CUDA_VISIBLE_DEVICES'] = os.environ['p']
# os.environ['CUDA_VISIBLE_DEVICES'] = '7'
import warnings
warnings.filterwarnings('ignore')
from model.data_pipe import BartNERPipe
from model.bart_multi_concat import BartSeq2SeqModel
from model.generater_multi_concat import SequenceGeneratorModel
from model.metrics import Seq2SeqSpanMetric
from model.losses import get_loss
import fitlog
import datetime
from fastNLP import Trainer
from torch import optim
from fastNLP import BucketSampler, GradientClipCallback, cache_results, EarlyStopCallback, SequentialSampler
from model.callbacks import WarmupCallback
from fastNLP.core.sampler import SortedSampler
from fastNLP.core.sampler import ConstTokenNumSampler
from model.callbacks import FitlogCallback
from fastNLP import DataSetIter
from tqdm import tqdm, trange
from fastNLP.core.utils import _move_dict_value_to_device
import random
import torch
fitlog.debug()
fitlog.set_log_dir('logs')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--bart_name', default='facebook/bart-large', type=str)
parser.add_argument('--datapath', default='./Twitter_GMNER/txt/', type=str)
parser.add_argument('--image_feature_path',default='./data/Twitter_GMNER_vinvl', type=str)
parser.add_argument('--image_annotation_path',default='./Twitter_GMNER/xml/', type=str)
parser.add_argument('--box_num',default='16', type=int)
parser.add_argument('--model_weight',default= None, type = str)
parser.add_argument('--normalize',default=False, action = "store_true")
parser.add_argument('--max_len', default=30, type=int)
parser.add_argument('--batch_size',default=16,type=int)
parser.add_argument("--log",default='./logs',type=str)
args= parser.parse_args()
model_path = args.model_weight.rsplit('/')
args.pred_output_file = '/'.join(model_path[:-1])+'/pred_'+model_path[-1]+'.txt'
dataset_name = 'twitter-ner'
args.length_penalty = 1
args.target_type = 'word'
args.schedule = 'linear'
args.decoder_type = 'avg_feature'
args.num_beams = 1
args.use_encoder_mlp = 1
args.warmup_ratio = 0.01
eval_start_epoch = 0
if 'twitter' in dataset_name:
max_len, max_len_a = args.max_len, 0.6
else:
print("Error dataset_name!")
if isinstance(args.decoder_type, str) and args.decoder_type.lower() == 'none':
args.decoder_type = None
demo = False
def get_data():
pipe = BartNERPipe(image_feature_path=args.image_feature_path,
image_annotation_path=args.image_annotation_path,
max_bbox =args.box_num,
normalize=args.normalize,
tokenizer=args.bart_name,
target_type=args.target_type)
if dataset_name == 'twitter-ner':
paths ={
'train': os.path.join(args.datapath,'train.txt'),
'dev': os.path.join(args.datapath,'dev.txt'),
'test': os.path.join(args.datapath,'test.txt') }
data_bundle = pipe.process_from_file(paths, demo=demo)
return data_bundle, pipe.tokenizer, pipe.mapping2id
data_bundle, tokenizer, mapping2id = get_data()
print(f'max_len_a:{max_len_a}, max_len:{max_len}')
print(data_bundle)
print("The number of tokens in tokenizer ", len(tokenizer.decoder))
bos_token_id = 0
eos_token_id = 1
label_ids = list(mapping2id.values())
model = BartSeq2SeqModel.build_model(args.bart_name, tokenizer, label_ids=label_ids, decoder_type=args.decoder_type,
use_encoder_mlp=args.use_encoder_mlp,box_num = args.box_num)
vocab_size = len(tokenizer)
model = SequenceGeneratorModel(model, bos_token_id=bos_token_id,
eos_token_id=eos_token_id,
max_length=max_len, max_len_a=max_len_a,num_beams=args.num_beams, do_sample=False,
repetition_penalty=1, length_penalty=args.length_penalty, pad_token_id=eos_token_id,
restricter=None, top_k = 1
)
model.load_state_dict(torch.load(args.model_weight))
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
metric = Seq2SeqSpanMetric(eos_token_id, num_labels=len(label_ids), region_num =args.box_num, target_type=args.target_type,print_mode = False )
test_dataset = data_bundle.get_dataset('test')
print(test_dataset[:3])
test_dataset.set_target('raw_words', 'raw_target')
device = torch.device(device)
model.to(device)
def Predict(args,eval_data, model, device, metric,tokenizer,ids2label):
data_iterator = DataSetIter(eval_data, batch_size=args.batch_size * 2, sampler=SequentialSampler())
# for batch_x, batch_y in tqdm(data_iterator, total=len(data_iterator)):
with open (args.pred_output_file,'w') as fw:
for batch_x, batch_y in (data_iterator):
_move_dict_value_to_device(batch_x, batch_y, device=device)
src_tokens = batch_x['src_tokens']
image_feature = batch_x['image_feature']
tgt_tokens = batch_x['tgt_tokens']
src_seq_len = batch_x['src_seq_len']
tgt_seq_len = batch_x['tgt_seq_len']
first = batch_x['first']
region_label = batch_y['region_label']
target_span = batch_y['target_span']
cover_flag = batch_y['cover_flag']
results = model.predict(src_tokens,image_feature, src_seq_len=src_seq_len, first=first)
pred,region_pred = results['pred'],results['region_pred'] ## logits:(bsz,tgt_len,class+max_len) region_logits:(??,8)
pred_pairs, target_pairs = metric.evaluate(target_span, pred, tgt_tokens, region_pred,region_label,cover_flag,predict_mode=True)
raw_words = batch_y['raw_words']
word_start_index = 8 ## 2 + 2 +4
assert len(pred_pairs) == len(target_pairs)
for i in range(len(pred_pairs)):
cur_src_token = src_tokens[i].cpu().numpy().tolist()
fw.write(' '.join(raw_words[i])+'\n')
fw.write('Pred: ')
for k,v in pred_pairs[i].items():
entity_span_ind_list =[]
for kk in k:
entity_span_ind_list.append(cur_src_token[kk-word_start_index])
entity_span = tokenizer.decode(entity_span_ind_list)
region_pred, entity_type_ind = v
entity_type = ids2label[entity_type_ind[0]]
fw.write('('+entity_span+' , '+ str(region_pred)+' , '+entity_type+' ) ')
fw.write('\n')
fw.write(' GT : ')
for k,v in target_pairs[i].items():
entity_span_ind_list =[]
for kk in k:
entity_span_ind_list.append(cur_src_token[kk-word_start_index])
entity_span = tokenizer.decode(entity_span_ind_list)
region_pred, entity_type_ind = v
entity_type = ids2label[entity_type_ind[0]]
fw.write('('+entity_span+' , '+ str(region_pred)+' , '+entity_type+' ) ')
fw.write('\n\n')
res = metric.get_metric()
fw.write(str(res))
return res
ids2label = {2+i:l for i,l in enumerate(mapping2id.keys())}
model.eval()
test_res = Predict(args,eval_data=test_dataset, model=model, device=device, metric = metric,tokenizer=tokenizer,ids2label=ids2label)
test_f = test_res['f']
print("test: "+str(test_res))