-
Notifications
You must be signed in to change notification settings - Fork 105
/
eval.py
187 lines (160 loc) · 5.67 KB
/
eval.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
# coding=utf-8
from __future__ import print_function
import optparse
import torch
import time
import cPickle
from torch.autograd import Variable
from loader import *
from utils import *
t = time.time()
# python -m visdom.server
optparser = optparse.OptionParser()
optparser.add_option(
"-t", "--test", default="dataset/eng.testb",
help="Test set location"
)
optparser.add_option(
'--score', default='evaluation/temp/score.txt',
help='score file location'
)
optparser.add_option(
"-f", "--crf", default="0",
type='int', help="Use CRF (0 to disable)"
)
optparser.add_option(
"-g", '--use_gpu', default='1',
type='int', help='whether or not to ues gpu'
)
optparser.add_option(
'--loss', default='loss.txt',
help='loss file location'
)
optparser.add_option(
'--model_path', default='models/lstm_crf.model',
help='model path'
)
optparser.add_option(
'--map_path', default='models/mapping.pkl',
help='model path'
)
optparser.add_option(
'--char_mode', choices=['CNN', 'LSTM'], default='CNN',
help='char_CNN or char_LSTM'
)
opts = optparser.parse_args()[0]
mapping_file = opts.map_path
with open(mapping_file, 'rb') as f:
mappings = cPickle.load(f)
word_to_id = mappings['word_to_id']
tag_to_id = mappings['tag_to_id']
id_to_tag = {k[1]: k[0] for k in tag_to_id.items()}
char_to_id = mappings['char_to_id']
parameters = mappings['parameters']
word_embeds = mappings['word_embeds']
use_gpu = opts.use_gpu == 1 and torch.cuda.is_available()
assert os.path.isfile(opts.test)
assert parameters['tag_scheme'] in ['iob', 'iobes']
if not os.path.isfile(eval_script):
raise Exception('CoNLL evaluation script not found at "%s"' % eval_script)
if not os.path.exists(eval_temp):
os.makedirs(eval_temp)
lower = parameters['lower']
zeros = parameters['zeros']
tag_scheme = parameters['tag_scheme']
test_sentences = load_sentences(opts.test, lower, zeros)
update_tag_scheme(test_sentences, tag_scheme)
test_data = prepare_dataset(
test_sentences, word_to_id, char_to_id, tag_to_id, lower
)
model = torch.load(opts.model_path)
model_name = opts.model_path.split('/')[-1].split('.')[0]
if use_gpu:
model.cuda()
model.eval()
# def getmaxlen(tags):
# l = 1
# maxl = 1
# for tag in tags:
# tag = id_to_tag[tag]
# if 'I-' in tag:
# l += 1
# elif 'B-' in tag:
# l = 1
# elif 'E-' in tag:
# l += 1
# maxl = max(maxl, l)
# l = 1
# elif 'O' in tag:
# l = 1
# return maxl
def eval(model, datas, maxl=1):
prediction = []
confusion_matrix = torch.zeros((len(tag_to_id) - 2, len(tag_to_id) - 2))
for data in datas:
ground_truth_id = data['tags']
# l = getmaxlen(ground_truth_id)
# if not l == maxl:
# continue
words = data['str_words']
chars2 = data['chars']
caps = data['caps']
if parameters['char_mode'] == 'LSTM':
chars2_sorted = sorted(chars2, key=lambda p: len(p), reverse=True)
d = {}
for i, ci in enumerate(chars2):
for j, cj in enumerate(chars2_sorted):
if ci == cj and not j in d and not i in d.values():
d[j] = i
continue
chars2_length = [len(c) for c in chars2_sorted]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_sorted), char_maxl), dtype='int')
for i, c in enumerate(chars2_sorted):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
if parameters['char_mode'] == 'CNN':
d = {}
chars2_length = [len(c) for c in chars2]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_length), char_maxl), dtype='int')
for i, c in enumerate(chars2):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
dwords = Variable(torch.LongTensor(data['words']))
dcaps = Variable(torch.LongTensor(caps))
if use_gpu:
val, out = model(dwords.cuda(), chars2_mask.cuda(), dcaps.cuda(),chars2_length, d)
else:
val, out = model(dwords, chars2_mask, dcaps, chars2_length, d)
predicted_id = out
for (word, true_id, pred_id) in zip(words, ground_truth_id, predicted_id):
line = ' '.join([word, id_to_tag[true_id], id_to_tag[pred_id]])
prediction.append(line)
confusion_matrix[true_id, pred_id] += 1
prediction.append('')
predf = eval_temp + '/pred.' + model_name
scoref = eval_temp + '/score.' + model_name
with open(predf, 'wb') as f:
f.write('\n'.join(prediction))
os.system('%s < %s > %s' % (eval_script, predf, scoref))
with open(scoref, 'rb') as f:
for l in f.readlines():
print(l.strip())
print(("{: >2}{: >7}{: >7}%s{: >9}" % ("{: >7}" * confusion_matrix.size(0))).format(
"ID", "NE", "Total",
*([id_to_tag[i] for i in range(confusion_matrix.size(0))] + ["Percent"])
))
for i in range(confusion_matrix.size(0)):
print(("{: >2}{: >7}{: >7}%s{: >9}" % ("{: >7}" * confusion_matrix.size(0))).format(
str(i), id_to_tag[i], str(confusion_matrix[i].sum()),
*([confusion_matrix[i][j] for j in range(confusion_matrix.size(0))] +
["%.3f" % (confusion_matrix[i][i] * 100. / max(1, confusion_matrix[i].sum()))])
))
# for l in range(1, 6):
# print('maxl=', l)
# eval(model, test_data, l)
# # print()
# # for i in range(10):
# # eval(model, test_data, 100)
print(time.time() - t)