-
Notifications
You must be signed in to change notification settings - Fork 30
/
test.py
391 lines (307 loc) · 15.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import warnings
warnings.filterwarnings("ignore")
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import argparse
import ruamel_yaml as yaml
import numpy as np
import random
import time
import datetime
import json
from pathlib import Path
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
import torch.distributed as dist
from models.vit import interpolate_pos_embed
from transformers import BertTokenizerFast
import utils
from dataset import create_dataset, create_sampler, create_loader
from scheduler import create_scheduler
from optim import create_optimizer
import torch.multiprocessing as mp
from torch.utils.tensorboard import SummaryWriter
import logging
from types import MethodType
from tools.env import init_dist
from tqdm import tqdm
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from scipy.optimize import brentq
from scipy.interpolate import interp1d
from models import box_ops
from tools.multilabel_metrics import AveragePrecisionMeter, get_multi_label
from models.HAMMER import HAMMER
def setlogger(log_file):
filehandler = logging.FileHandler(log_file)
streamhandler = logging.StreamHandler()
logger = logging.getLogger('')
logger.setLevel(logging.INFO)
logger.addHandler(filehandler)
logger.addHandler(streamhandler)
def epochInfo(self, set, idx, loss, acc):
self.info('{set}-{idx:d} epoch | loss:{loss:.8f} | auc:{acc:.4f}%'.format(
set=set,
idx=idx,
loss=loss,
acc=acc
))
logger.epochInfo = MethodType(epochInfo, logger)
return logger
def text_input_adjust(text_input, fake_word_pos, device):
# input_ids adaptation
input_ids_remove_SEP = [x[:-1] for x in text_input.input_ids]
maxlen = max([len(x) for x in text_input.input_ids])-1
input_ids_remove_SEP_pad = [x + [0] * (maxlen - len(x)) for x in input_ids_remove_SEP] # only remove SEP as HAMMER is conducted with text with CLS
text_input.input_ids = torch.LongTensor(input_ids_remove_SEP_pad).to(device)
# attention_mask adaptation
attention_mask_remove_SEP = [x[:-1] for x in text_input.attention_mask]
attention_mask_remove_SEP_pad = [x + [0] * (maxlen - len(x)) for x in attention_mask_remove_SEP]
text_input.attention_mask = torch.LongTensor(attention_mask_remove_SEP_pad).to(device)
# fake_token_pos adaptation
fake_token_pos_batch = []
subword_idx_rm_CLSSEP_batch = []
for i in range(len(fake_word_pos)):
fake_token_pos = []
fake_word_pos_decimal = np.where(fake_word_pos[i].numpy() == 1)[0].tolist() # transfer fake_word_pos into numbers
subword_idx = text_input.word_ids(i)
subword_idx_rm_CLSSEP = subword_idx[1:-1]
subword_idx_rm_CLSSEP_array = np.array(subword_idx_rm_CLSSEP) # get the sub-word position (token position)
subword_idx_rm_CLSSEP_batch.append(subword_idx_rm_CLSSEP_array)
# transfer the fake word position into fake token position
for i in fake_word_pos_decimal:
fake_token_pos.extend(np.where(subword_idx_rm_CLSSEP_array == i)[0].tolist())
fake_token_pos_batch.append(fake_token_pos)
return text_input, fake_token_pos_batch, subword_idx_rm_CLSSEP_batch
@torch.no_grad()
def evaluation(args, model, data_loader, tokenizer, device, config):
# test
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Evaluation:'
print('Computing features for evaluation...')
print_freq = 200
y_true, y_pred, IOU_pred, IOU_50, IOU_75, IOU_95 = [], [], [], [], [], []
cls_nums_all = 0
cls_acc_all = 0
TP_all = 0
TN_all = 0
FP_all = 0
FN_all = 0
TP_all_multicls = np.zeros(4, dtype = int)
TN_all_multicls = np.zeros(4, dtype = int)
FP_all_multicls = np.zeros(4, dtype = int)
FN_all_multicls = np.zeros(4, dtype = int)
F1_multicls = np.zeros(4)
multi_label_meter = AveragePrecisionMeter(difficult_examples=False)
multi_label_meter.reset()
for i, (image, label, text, fake_image_box, fake_word_pos, W, H) in enumerate(metric_logger.log_every(args, data_loader, print_freq, header)):
image = image.to(device,non_blocking=True)
text_input = tokenizer(text, max_length=128, truncation=True, add_special_tokens=True, return_attention_mask=True, return_token_type_ids=False)
text_input, fake_token_pos, _ = text_input_adjust(text_input, fake_word_pos, device)
logits_real_fake, logits_multicls, output_coord, logits_tok = model(image, label, text_input, fake_image_box, fake_token_pos, is_train=False)
##================= real/fake cls ========================##
cls_label = torch.ones(len(label), dtype=torch.long).to(image.device)
real_label_pos = np.where(np.array(label) == 'orig')[0].tolist()
cls_label[real_label_pos] = 0
y_pred.extend(F.softmax(logits_real_fake,dim=1)[:,1].cpu().flatten().tolist())
y_true.extend(cls_label.cpu().flatten().tolist())
pred_acc = logits_real_fake.argmax(1)
cls_nums_all += cls_label.shape[0]
cls_acc_all += torch.sum(pred_acc == cls_label).item()
# ----- multi metrics -----
target, _ = get_multi_label(label, image)
multi_label_meter.add(logits_multicls, target)
for cls_idx in range(logits_multicls.shape[1]):
cls_pred = logits_multicls[:, cls_idx]
cls_pred[cls_pred>=0]=1
cls_pred[cls_pred<0]=0
TP_all_multicls[cls_idx] += torch.sum((target[:, cls_idx] == 1) * (cls_pred == 1)).item()
TN_all_multicls[cls_idx] += torch.sum((target[:, cls_idx] == 0) * (cls_pred == 0)).item()
FP_all_multicls[cls_idx] += torch.sum((target[:, cls_idx] == 0) * (cls_pred == 1)).item()
FN_all_multicls[cls_idx] += torch.sum((target[:, cls_idx] == 1) * (cls_pred == 0)).item()
##================= bbox cls ========================##
boxes1 = box_ops.box_cxcywh_to_xyxy(output_coord)
boxes2 = box_ops.box_cxcywh_to_xyxy(fake_image_box)
IOU, _ = box_ops.box_iou(boxes1, boxes2.to(device), test=True)
IOU_pred.extend(IOU.cpu().tolist())
IOU_50_bt = torch.zeros(IOU.shape, dtype=torch.long)
IOU_75_bt = torch.zeros(IOU.shape, dtype=torch.long)
IOU_95_bt = torch.zeros(IOU.shape, dtype=torch.long)
IOU_50_bt[IOU>0.5] = 1
IOU_75_bt[IOU>0.75] = 1
IOU_95_bt[IOU>0.95] = 1
IOU_50.extend(IOU_50_bt.cpu().tolist())
IOU_75.extend(IOU_75_bt.cpu().tolist())
IOU_95.extend(IOU_95_bt.cpu().tolist())
##================= token cls ========================##
token_label = text_input.attention_mask[:,1:].clone() # [:,1:] for ingoring class token
token_label[token_label==0] = -100 # -100 index = padding token
token_label[token_label==1] = 0
for batch_idx in range(len(fake_token_pos)):
fake_pos_sample = fake_token_pos[batch_idx]
if fake_pos_sample:
for pos in fake_pos_sample:
token_label[batch_idx, pos] = 1
logits_tok_reshape = logits_tok.view(-1, 2)
logits_tok_pred = logits_tok_reshape.argmax(1)
token_label_reshape = token_label.view(-1)
# F1
TP_all += torch.sum((token_label_reshape == 1) * (logits_tok_pred == 1)).item()
TN_all += torch.sum((token_label_reshape == 0) * (logits_tok_pred == 0)).item()
FP_all += torch.sum((token_label_reshape == 0) * (logits_tok_pred == 1)).item()
FN_all += torch.sum((token_label_reshape == 1) * (logits_tok_pred == 0)).item()
##================= real/fake cls ========================##
y_true, y_pred = np.array(y_true), np.array(y_pred)
AUC_cls = roc_auc_score(y_true, y_pred)
ACC_cls = cls_acc_all / cls_nums_all
fpr, tpr, thresholds = roc_curve(y_true, y_pred, pos_label=1)
EER_cls = brentq(lambda x: 1. - x - interp1d(fpr, tpr)(x), 0., 1.)
##================= bbox cls ========================##
IOU_score = sum(IOU_pred)/len(IOU_pred)
IOU_ACC_50 = sum(IOU_50)/len(IOU_50)
IOU_ACC_75 = sum(IOU_75)/len(IOU_75)
IOU_ACC_95 = sum(IOU_95)/len(IOU_95)
# ##================= token cls========================##
ACC_tok = (TP_all + TN_all) / (TP_all + TN_all + FP_all + FN_all)
Precision_tok = TP_all / (TP_all + FP_all)
Recall_tok = TP_all / (TP_all + FN_all)
F1_tok = 2*Precision_tok*Recall_tok / (Precision_tok + Recall_tok)
##================= multi-label cls ========================##
MAP = multi_label_meter.value().mean()
OP, OR, OF1, CP, CR, CF1 = multi_label_meter.overall()
for cls_idx in range(logits_multicls.shape[1]):
Precision_multicls = TP_all_multicls[cls_idx] / (TP_all_multicls[cls_idx] + FP_all_multicls[cls_idx])
Recall_multicls = TP_all_multicls[cls_idx] / (TP_all_multicls[cls_idx] + FN_all_multicls[cls_idx])
F1_multicls[cls_idx] = 2*Precision_multicls*Recall_multicls / (Precision_multicls + Recall_multicls)
return AUC_cls, ACC_cls, EER_cls, \
MAP.item(), OP, OR, OF1, CP, CR, CF1, F1_multicls, \
IOU_score, IOU_ACC_50, IOU_ACC_75, IOU_ACC_95, \
ACC_tok, Precision_tok, Recall_tok, F1_tok
def main_worker(gpu, args, config):
if gpu is not None:
args.gpu = gpu
init_dist(args)
eval_type = os.path.basename(config['val_file'][0]).split('.')[0]
if eval_type == 'test':
eval_type = 'all'
log_dir = os.path.join(args.output_dir, args.log_num, 'evaluation')
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, f'shell_{eval_type}.txt')
logger = setlogger(log_file)
if args.log:
logger.info('******************************')
logger.info(args)
logger.info('******************************')
logger.info(config)
logger.info('******************************')
device = torch.device(args.device)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
cudnn.benchmark = True
#### Model ####
tokenizer = BertTokenizerFast.from_pretrained(args.text_encoder)
if args.log:
print(f"Creating MAMMER")
model = HAMMER(args=args, config=config, text_encoder=args.text_encoder, tokenizer=tokenizer, init_deit=True)
model = model.to(device)
checkpoint_dir = f'{args.output_dir}/{args.log_num}/checkpoint_{args.test_epoch}.pth'
checkpoint = torch.load(checkpoint_dir, map_location='cpu')
state_dict = checkpoint['model']
pos_embed_reshaped = interpolate_pos_embed(state_dict['visual_encoder.pos_embed'],model.visual_encoder)
state_dict['visual_encoder.pos_embed'] = pos_embed_reshaped
# model.load_state_dict(state_dict)
if args.log:
print('load checkpoint from %s'%checkpoint_dir)
msg = model.load_state_dict(state_dict, strict=False)
if args.log:
print(msg)
#### Dataset ####
if args.log:
print("Creating dataset")
_, val_dataset = create_dataset(config)
if args.distributed:
samplers = create_sampler([val_dataset], [True], args.world_size, args.rank) + [None]
else:
samplers = [None]
val_loader = create_loader([val_dataset],
samplers,
batch_size=[config['batch_size_val']],
num_workers=[4],
is_trains=[False],
collate_fns=[None])[0]
model_without_ddp = model
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
model_without_ddp = model.module
if args.log:
print("Start evaluation")
AUC_cls, ACC_cls, EER_cls, \
MAP, OP, OR, OF1, CP, CR, CF1, F1_multicls, \
IOU_score, IOU_ACC_50, IOU_ACC_75, IOU_ACC_95, \
ACC_tok, Precision_tok, Recall_tok, F1_tok = evaluation(args, model_without_ddp, val_loader, tokenizer, device, config)
#============ evaluation info ============#
val_stats = {"AUC_cls": "{:.4f}".format(AUC_cls*100),
"ACC_cls": "{:.4f}".format(ACC_cls*100),
"EER_cls": "{:.4f}".format(EER_cls*100),
"MAP": "{:.4f}".format(MAP*100),
"OP": "{:.4f}".format(OP*100),
"OR": "{:.4f}".format(OR*100),
"OF1": "{:.4f}".format(OF1*100),
"CP": "{:.4f}".format(CP*100),
"CR": "{:.4f}".format(CR*100),
"CF1": "{:.4f}".format(CF1*100),
"F1_FS": "{:.4f}".format(F1_multicls[0]*100),
"F1_FA": "{:.4f}".format(F1_multicls[1]*100),
"F1_TS": "{:.4f}".format(F1_multicls[2]*100),
"F1_TA": "{:.4f}".format(F1_multicls[3]*100),
"IOU_score": "{:.4f}".format(IOU_score*100),
"IOU_ACC_50": "{:.4f}".format(IOU_ACC_50*100),
"IOU_ACC_75": "{:.4f}".format(IOU_ACC_75*100),
"IOU_ACC_95": "{:.4f}".format(IOU_ACC_95*100),
"ACC_tok": "{:.4f}".format(ACC_tok*100),
"Precision_tok": "{:.4f}".format(Precision_tok*100),
"Recall_tok": "{:.4f}".format(Recall_tok*100),
"F1_tok": "{:.4f}".format(F1_tok*100),
}
if utils.is_main_process():
log_stats = {**{f'val_{k}': v for k, v in val_stats.items()},
'epoch': args.test_epoch,
}
with open(os.path.join(log_dir, f"results_{eval_type}.txt"),"a") as f:
f.write(json.dumps(log_stats) + "\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', default='./configs/Pretrain.yaml')
parser.add_argument('--checkpoint', default='')
parser.add_argument('--resume', default=False, type=bool)
parser.add_argument('--output_dir', default='/mnt/lustre/share/rshao/data/FakeNews/Ours/results')
parser.add_argument('--text_encoder', default='bert-base-uncased')
parser.add_argument('--device', default='cuda')
parser.add_argument('--seed', default=777, type=int)
# parser.add_argument('--world_size', default=1, type=int, help='number of distributed processes')
# parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
parser.add_argument('--distributed', default=False, type=bool)
parser.add_argument('--rank', default=-1, type=int,
help='node rank for distributed training')
parser.add_argument('--world_size', default=1, type=int,
help='world size for distributed training')
parser.add_argument('--dist-url', default='tcp://127.0.0.1:23451', type=str,
help='url used to set up distributed training')
parser.add_argument('--dist-backend', default='nccl', type=str,
help='distributed backend')
parser.add_argument('--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none',
help='job launcher')
parser.add_argument('--log_num', '-l', type=str)
parser.add_argument('--model_save_epoch', type=int, default=5)
parser.add_argument('--token_momentum', default=False, action='store_true')
parser.add_argument('--test_epoch', default='best', type=str)
args = parser.parse_args()
config = yaml.load(open(args.config, 'r'), Loader=yaml.Loader)
main_worker(0, args, config)