-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_bert.py
283 lines (225 loc) · 12.3 KB
/
test_bert.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
import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '1'
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader, SequentialSampler
from transformers import BertForSequenceClassification
import argparse
from utils import set_seed, cal_entropy, get_performance, apply_dropout
from models import ModelWithTemperature
import pickle
import warnings
warnings.filterwarnings("ignore")
def main():
parser = argparse.ArgumentParser(description='Test code - measure the detection performance')
parser.add_argument('--eva_iter', default=1, type=int, help='number of passes for mc-dropout when evaluation')
parser.add_argument('--model', type=str, choices=['base', 'manifold-smoothing', 'mc-dropout', 'temperature', 'oe'],
default='base')
parser.add_argument('--seed', type=int, default=0, help='random seed for test')
parser.add_argument('--index', type=int, default=0, help='random seed you used during training')
parser.add_argument('--dataset', required=True, help='target dataset: sst')
parser.add_argument('--out_dataset', required=False, help='out-of-dist dataset')
parser.add_argument('--eval_batch_size', type=int, default=64)
parser.add_argument('--saved_dataset', type=str, default='y', choices = ['y','n'])
parser.add_argument('--eps_out', default=0.001, type=float,
help="Perturbation size of out-of-domain adversarial training")
parser.add_argument("--eps_y", default=0.1, type=float, help="Perturbation size of label")
parser.add_argument('--eps_in', default=0.0001, type=float,
help="Perturbation size of in-domain adversarial training")
parser.add_argument('--save_result', type=str, default='n', choices= ['y','n'])
parser.add_argument('--evaluate_benchmark', type=str, default='y', choices = ['y','n'])
parser.add_argument('--save_path', type=str, default='result')
parser.add_argument('--MAX_LEN', type=int, default=150)
parser.add_argument("--base_rate", default=5, type=int, help="base rate N:1")
parser.add_argument('--recall_level', type=float, default=0.9)
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
args.device = device
set_seed(args)
if args.model in ['base', 'mc-dropout']:
dirname = '{}/BERT-base-{}'.format(args.dataset, args.index)
pretrained_dir = './{}/{}'.format(args.save_path, dirname)
model = BertForSequenceClassification.from_pretrained(pretrained_dir)
model.to(args.device)
if args.model == 'temperature':
dirname = '{}/BERT-base-{}'.format(args.dataset, args.index)
pretrained_dir = './{}/{}'.format(args.save_path, dirname)
orig_model = BertForSequenceClassification.from_pretrained(pretrained_dir)
orig_model.to(args.device)
model = ModelWithTemperature(orig_model)
model.to(args.device)
elif args.model == 'manifold-smoothing':
dirname = '{}/BERT-manifold-smoothing-{}'.format(args.dataset, args.index)
pretrained_dir = './{}/{}'.format(args.save_path, dirname)
model = BertForSequenceClassification.from_pretrained(pretrained_dir)
model.to(args.device)
elif args.model == 'oe':
dirname = '{}/BERT-oe-{}'.format(args.dataset, args.index)
pretrained_dir = './{}/{}'.format(args.save_path, dirname)
model = BertForSequenceClassification.from_pretrained(pretrained_dir)
model.to(args.device)
print('Model: %s\t dir: %s'%(args.model, dirname))
if args.evaluate_benchmark == 'y':
ood_list = ['snli', 'imdb', 'multi30k', 'wmt16', 'yelp']
else:
ood_list = [args.out_dataset]
print('ood_datasets: %s\n\n' %ood_list )
print('Loading saved dataset checkpoints for testing...')
dataset_dir = 'dataset/test'
val_data = torch.load(dataset_dir + '/{}_val_in_domain.pt'.format(args.dataset))
test_data = torch.load(dataset_dir + '/{}_test_in_domain.pt'.format(args.dataset))
test_sampler = SequentialSampler(test_data)
test_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=args.eval_batch_size)
val_sampler = SequentialSampler(val_data)
val_dataloader = DataLoader(val_data, sampler=val_sampler, batch_size=args.eval_batch_size)
if args.model == 'temperature':
model.set_temperature(val_dataloader, args)
model.eval()
if args.model == 'mc-dropout':
model.apply(apply_dropout)
# ##### validation dat
# with torch.no_grad():
# for step, batch in enumerate(val_dataloader):
# batch = tuple(t.to(args.device) for t in batch)
# b_input_ids, b_input_mask, b_labels = batch
# total += b_labels.shape[0]
# batch_output = 0
# for j in range(args.eva_iter):
# if args.model == 'temperature':
# current_batch = model(input_ids=b_input_ids, token_type_ids=None,
# attention_mask=b_input_mask) # logits
# else:
# current_batch = model(input_ids=b_input_ids, token_type_ids=None, attention_mask=b_input_mask)[
# 0] # logits
# batch_output = batch_output + F.softmax(current_batch, dim=1)
# batch_output = batch_output / args.eva_iter
# output_list.append(batch_output)
# labels_list.append(b_labels)
# score, predicted = batch_output.max(1)
# correct += predicted.eq(b_labels).sum().item()
#
# ###calculate accuracy and ECE
# val_eval_accuracy = correct / total
# print("Val Accuracy: {}".format(val_eval_accuracy))
# ece_criterion = ECE_v2().to(args.device)
# softmaxes_ece = torch.cat(output_list)
# labels_ece = torch.cat(labels_list)
# val_ece = ece_criterion(softmaxes_ece, labels_ece).item()
# print('ECE on Val data: {}'.format(val_ece))
#### Test data
correct = 0
total = 0
output_list = []
labels_list = []
score_list = []
correct_index_all = []
ent_list = []
## test on in-distribution test set
with torch.no_grad():
for step, batch in enumerate(test_dataloader):
batch = tuple(t.to(args.device) for t in batch)
b_input_ids, b_input_mask, b_labels = batch
total += b_labels.shape[0]
batch_output = 0
for j in range(args.eva_iter):
if args.model == 'temperature':
current_batch = model(input_ids=b_input_ids, token_type_ids=None,
attention_mask=b_input_mask) # logits
else:
current_batch = model(input_ids=b_input_ids, token_type_ids=None, attention_mask=b_input_mask)[
0] # logits
batch_output = batch_output + F.softmax(current_batch, dim=1)
batch_output = batch_output / args.eva_iter
output_list.append(batch_output)
labels_list.append(b_labels)
score, predicted = batch_output.max(1)
correct += predicted.eq(b_labels).sum().item()
correct_index = (predicted == b_labels)
correct_index_all.append(correct_index)
score_list.append(score)
ent = cal_entropy(batch_output)
ent_list.append(ent)
eval_accuracy = correct / total
print("Test Accuracy: {}".format(eval_accuracy))
# confidence for in-distribution data
score_in_array = torch.cat(score_list)
# indices of data that are classified correctly
correct_array = torch.cat(correct_index_all)
label_array = torch.cat(labels_list)
label_array = label_array.cpu().numpy()
score_in_array = score_in_array.cpu().numpy()
correct_array = correct_array.cpu().numpy()
ent_in_array = torch.cat(ent_list)
ent_in_array = ent_in_array.cpu().numpy()
ent_succ_array = ent_in_array[correct_array]
ent_fail_array = ent_in_array[~correct_array]
in_num_examples = len(score_in_array)
ood_MAX_NUM = in_num_examples//args.base_rate
RECALL_LEVEL = args.recall_level
report_result = []
for ood_dataset in ood_list:
print('Evaluate on %s ...'% ood_dataset)
# nt_test_data = torch.load(dataset_dir + '/{}_{}_test_out_of_domain.pt'.format(args.dataset, ood_dataset))
nt_test_data = torch.load(dataset_dir + '/{}_test_out_of_domain.pt'.format(ood_dataset))
nt_test_sampler = SequentialSampler(nt_test_data)
nt_test_dataloader = DataLoader(nt_test_data, sampler=nt_test_sampler, batch_size=args.eval_batch_size)
# nt_test_dataloader = DataLoader(nt_test_data, batch_size=args.eval_batch_size, shuffle=True)
### test on out-of-distribution data
score_ood_list = []
ent_ood_list = []
with torch.no_grad():
for step, batch in enumerate(nt_test_dataloader):
if step * args.eval_batch_size > ood_MAX_NUM:
break
batch = tuple(t.to(args.device) for t in batch)
b_input_ids, b_input_mask, b_labels = batch
batch_output = 0
for j in range(args.eva_iter):
if args.model == 'temperature':
current_batch = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask)
else:
current_batch = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask)[0]
batch_output = batch_output + F.softmax(current_batch, dim=1)
batch_output = batch_output / args.eva_iter
score_out, _ = batch_output.max(1)
score_ood_list.append(score_out)
#TODO
ent_out = cal_entropy(batch_output)
ent_ood_list.append(ent_out)
score_ood_array = torch.cat(score_ood_list)
score_ood_array = score_ood_array.cpu().numpy()
ent_ood_array = torch.cat(ent_ood_list)
ent_ood_array = ent_ood_array.cpu().numpy()
expected_ap = len(score_ood_array) / (len(score_in_array) + len(score_ood_array))
ood_dect_scores = get_performance(-score_ood_array, -score_in_array, expected_ap, recall_level= RECALL_LEVEL)
print('OOD detection, \t{}\t\t FPR{:d}: {:.4f},\t AUROC: {:.4f}\t AUPR: {:.4f} '.format(ood_dataset,
int(100 * RECALL_LEVEL),
ood_dect_scores[0],
ood_dect_scores[1],
ood_dect_scores[2]))
report_result.append([ood_dect_scores[0],ood_dect_scores[1],ood_dect_scores[2]])
####### In distribution ######
score_in_succ = score_in_array[correct_array]
score_in_fail = score_in_array[~correct_array]
expected_ap = len(score_in_fail) / (len(score_in_succ) + len(score_in_fail))
mis_dect_scores = get_performance(-score_in_fail, -score_in_succ, expected_ap, recall_level= RECALL_LEVEL)
print('misclassification detection,\tFPR{:d}: {:.4f},\t AUROC: {:.4f}\t AUPR: {:.4f} '.format(int(100 * RECALL_LEVEL),
mis_dect_scores[0],
mis_dect_scores[1],
mis_dect_scores[2]))
if args.save_result == 'y':
result = {}
result['ood_msp'] = score_ood_array
result['in_msp'] = score_in_array
result['succ_msp'] = score_in_succ
result['fail_msp']= score_in_fail
result['in_ent'] = ent_in_array
result['ood_ent']= ent_ood_array
result['succ_ent'] = ent_succ_array
result['fail_ent'] = ent_fail_array
target_path = pretrained_dir + '/%s_%s_result.pt' % (args.model, ood_dataset)
print('save to %s'%target_path)
with open(target_path, 'wb') as file:
pickle.dump(result , file)
if __name__ == "__main__":
main()