-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
286 lines (258 loc) · 11.7 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
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
import numpy as np
import torch.nn as nn
import torch
import os
import zipfile
import pickle
import fnmatch
import json
import string
from collections import Counter
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from utils import correct_predictions
from utils import rank_logger_info
from torch.utils.data import DataLoader
from drlstm.data import NLIDataset
from drlstm.model import DRLSTM
from drlstm.multi_model import multi_model as Multi_DRLSTM
import time
from tqdm import tqdm
import matplotlib.pyplot as plt
"""
Test the model on some preprocessed dataset.
"""
# Aurelien Coet, 2018.
def test(args,model, dataloader,device):
model.eval()
time_start = time.time()
batch_time = 0.0
accuracy = 0.0
first = True
batch_i = 0
n_high_overlap = 0
n_reg_overlap = 0
n_low_overlap = 0
n_long_sentence = 0
n_reg_sentence = 0
n_short_sentence = 0
n_negation = 0
n_quantifier = 0
n_belief = 0
n_total_high_overlap = 0
n_total_reg_overlap = 0
n_total_low_overlap = 0
n_total_long_sentence = 0
n_total_reg_sentence = 0
n_total_short_sentence = 0
n_total_negation = 0
n_total_quantifier = 0
n_total_belief = 0
n_total_entailment = 0
n_total_neutral = 0
n_total_contradiction = 0
n_correct_entailment = 0
n_correct_neutral = 0
n_correct_contradiction = 0
stat_file = args.test_statistics
with open(stat_file, "rb") as pkl:
test_statistics = pickle.load(pkl)
set_idx_high_overlap = set(test_statistics["high_overlap"])
set_idx_reg_overlap = set(test_statistics["reg_overlap"])
set_idx_low_overlap = set(test_statistics["low_overlap"])
set_idx_long_sentence = set(test_statistics["long_sentence"])
set_idx_reg_sentence = set(test_statistics["reg_sentence"])
set_idx_short_sentence = set(test_statistics["short_sentence"])
set_idx_negation = set(test_statistics["negation"])
set_idx_quantifier = set(test_statistics["quantifier"])
set_idx_belief = set(test_statistics["belief"])
# Deactivate autograd for evaluation.
with torch.no_grad():
for batch in dataloader:
batch_start = time.time()
# Move input and output data to the GPU if one is used.
premises = batch["premise"].to(device)
premises_lengths = batch["premise_length"].to(device)
hypotheses = batch["hypothesis"].to(device)
hypotheses_lengths = batch["hypothesis_length"].to(device)
labels = batch["label"].to(device)
_, probs = model(premises,
premises_lengths,
hypotheses,
hypotheses_lengths)
accuracy += correct_predictions(probs, labels)
batch_time += time.time() - batch_start
_, out_classes = probs.max(dim=1)
# if first:
# print ('Predictions for the first 5 sentences:')
# print ('Predictions:')
# print (out_classes[:5])
# print ('Labels:')
# print (labels[:5])
# print ('0 = entailment, 1 = neutral, 2 = contradiction')
# first = False
# statistics
for i in range(len(out_classes)):
line_i = batch_i*32 + i
if labels[i] == 0:
n_total_entailment += 1
elif labels[i] == 1:
n_total_neutral += 1
elif labels[i] == 2:
n_total_contradiction += 1
if line_i in set_idx_high_overlap:
n_total_high_overlap += 1
if line_i in set_idx_reg_overlap:
n_total_reg_overlap += 1
if line_i in set_idx_low_overlap:
n_total_low_overlap += 1
if line_i in set_idx_long_sentence:
n_total_long_sentence += 1
if line_i in set_idx_reg_sentence:
n_total_reg_sentence += 1
if line_i in set_idx_short_sentence:
n_total_short_sentence += 1
if line_i in set_idx_negation:
n_total_negation += 1
if line_i in set_idx_quantifier:
n_total_quantifier += 1
if line_i in set_idx_belief:
n_total_belief += 1
if out_classes[i] == labels[i]:
if labels[i] == 0:
n_correct_entailment += 1
elif labels[i] == 1:
n_correct_neutral += 1
elif labels[i] == 2:
n_correct_contradiction += 1
if line_i in set_idx_high_overlap:
n_high_overlap += 1
if line_i in set_idx_reg_overlap:
n_reg_overlap += 1
if line_i in set_idx_low_overlap:
n_low_overlap += 1
if line_i in set_idx_long_sentence:
n_long_sentence += 1
if line_i in set_idx_reg_sentence:
n_reg_sentence += 1
if line_i in set_idx_short_sentence:
n_short_sentence += 1
if line_i in set_idx_negation:
n_negation += 1
if line_i in set_idx_quantifier:
n_quantifier += 1
if line_i in set_idx_belief:
n_belief += 1
batch_i += 1
# print ('Total Entailment:' + str(n_total_entailment))
# print ('Correct Entailment:' + str(n_correct_entailment))
# print ('Accuracy:' + str(float(n_correct_entailment) / n_total_entailment))
# print ('Total Neutral:' + str(n_total_neutral))
# print ('Correct Neutral:' + str(n_correct_neutral))
# print ('Accuracy:' + str(float(n_correct_neutral) / n_total_neutral))
#
# print ('Total Contradiction:' + str(n_total_contradiction))
# print ('Correct Contradiction:' + str(n_correct_contradiction))
# print ('Accuracy:' + str(float(n_correct_contradiction) / n_total_contradiction))
# print ('Total high overlap sentence:' + str(n_total_high_overlap))
# print ('Correct high overlap sentence:' + str(n_high_overlap))
# print ('Accuracy:' + str(float(n_high_overlap) / n_total_high_overlap))
# print ('Total regular overlap sentence:' + str(n_total_reg_overlap))
# print ('Correct regular overlap sentence:' + str(n_reg_overlap))
# print ('Accuracy:' + str(float(n_reg_overlap) / n_total_reg_overlap))
# print ('Total low overlap sentence:' + str(n_total_low_overlap))
# print ('Correct low overlap sentence:' + str(n_low_overlap))
# print ('Accuracy:' + str(float(n_low_overlap) / n_total_low_overlap))
# print ('Total long sentence:' + str(n_total_long_sentence))
# print ('Correct long sentence:' + str(n_long_sentence))
# print ('Accuracy:' + str(float(n_long_sentence) / n_total_long_sentence))
# print ('Total regular sentence:' + str(n_total_reg_sentence))
# print ('Correct regular sentence:' + str(n_reg_sentence))
# print ('Accuracy:' + str(float(n_reg_sentence) / n_total_reg_sentence))
# print ('Total short sentence:' + str(n_total_short_sentence))
# print ('Correct short sentence:' + str(n_short_sentence))
# print ('Accuracy:' + str(float(n_short_sentence) / n_total_short_sentence))
# print ('Total sentence with negation:' + str(n_total_negation))
# print ('Correct sentence with negation:' + str(n_negation))
# print ('Accuracy:' + str(float(n_negation) / n_total_negation))
# print ('Total sentence with quantifier:' + str(n_total_quantifier))
# print ('Correct sentence with quantifier:' + str(n_quantifier))
# print ('Accuracy:' + str(float(n_quantifier) / n_total_quantifier))
# print ('Total sentence with belief:' + str(n_total_belief))
# print ('Correct sentence with belief:' + str(n_belief))
# print ('Accuracy:' + str(float(n_belief) / n_total_belief))
batch_time /= len(dataloader)
total_time = time.time() - time_start
accuracy /= (len(dataloader.dataset))
return batch_time, total_time, accuracy
def main_test(args,logger):
test_file = args.test_data
embedding_file = args.embeddings
pretrained_file = os.path.join(args.save_path, "best.pth.tar")
batch_size = args.batch_size
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
checkpoint = torch.load(pretrained_file, map_location=torch.device(device))
epoch = checkpoint["epoch"]
model_state_dict = checkpoint["model_state_dict"]
best_score = checkpoint["best_score"]
train_losses = checkpoint["train_losses"]
valid_losses = checkpoint["valid_losses"]
with open(test_file, "rb") as pkl:
test_data = NLIDataset(pickle.load(pkl))
test_loader = DataLoader(test_data, shuffle=False, batch_size=batch_size)
with open(embedding_file, "rb") as pkl:
embeddings = torch.tensor(pickle.load(pkl), dtype=torch.float).to(device)
if args.multimodel:
model = Multi_DRLSTM(embeddings.shape[0],
embeddings.shape[1],
hidden_size=args.hidden_size,
embeddings=embeddings,
padding_idx=0,
dropout=0.4,
num_classes=3,
device=device,
pooling_method_lst=args.pooling_method)
else:
model = DRLSTM(embeddings.shape[0],
embeddings.shape[1],
hidden_size=args.hidden_size,
embeddings=embeddings,
padding_idx=0,
dropout=args.dropout,
num_classes=args.num_classes,
device=device,
pooling_method_lst=args.pooling_method,
embedding_dropout = args.embedding_dropout)
if args.local_rank != -1:
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in model_state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
else:
new_state_dict = model_state_dict
model.load_state_dict(new_state_dict)
model.to(device)
info = 20 * "=" + " Testing model on device: {} ".format(device) + 20 * "="
rank_logger_info(logger, args.local_rank,info)
batch_time, total_time, accuracy = test(args, model, test_loader,device)
info = "-> Average batch processing time: {:.4f}s, total test time:\
{:.4f}s, accuracy: {:.4f}%".format(batch_time, total_time, (accuracy*100))
rank_logger_info(logger, args.local_rank,info)
if __name__ == '__main__':
# change
# test_data = 'test_data.pkl'
test_data = 'test_data.pkl'
checkpoint = 'multimodel_result/max_avg_max_avg/best.pth.tar'
batch_size = 32
print ('First 5 test sentences:')
sentences = ['This church choir sings to the masses as they sing joyous songs from the book at a church. The church has cracks in the ceiling.',
'This church choir sings to the masses as they sing joyous songs from the book at a church. The church is filled with song.',
'This church choir sings to the masses as they sing joyous songs from the book at a church. A choir singing at a baseball game.',
'A woman with a green headscarf, blue shirt and a very big grin. The woman is young.',
'A woman with a green headscarf, blue shirt and a very big grin. The woman is very happy.']
for sent in sentences:
print (sent)
main_test(test_data,
checkpoint,
batch_size)