-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval.py
298 lines (242 loc) · 11 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
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
from utils.load_parse_args import parse_args
import os
import random
import time
import warnings
import yaml
import ipdb
import numpy as np
import torch
import torch.distributed as dist
from cosine_annealing_warmup import CosineAnnealingWarmupRestarts
from sklearn.metrics import auc, roc_curve
from torch.cuda.amp import autocast, GradScaler
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.tensorboard import SummaryWriter
from torchsummary import summary
from tqdm import tqdm
import torch.nn as nn
from Data.clip_dataset import load_dataset_ddp
from configs.defaults import get_cfg_defaults
from models.build_model import build_model,update_logit_scale
from utils.logger import setup_logger
from utils.loss import compute_cls_loss, compute_seq_loss, compute_info_loss_neg, compute_gumbel_loss
from collections import OrderedDict
from utils.metrics import compute_WDR, pred_dist
from utils.preprocess import frames_preprocess
from utils.utils_distributed import all_gather_concat,all_reduce_mean,all_reduce_sum,all_gather_object
from retrival import retrieval
from Data.clip_dataset_retrival import load_dataset_retrival_ddp
warnings.filterwarnings("ignore")
# TORCH_DISTRIBUTED_DEBUG = 'Detail'
# torch.autograd.set_detect_anomaly(True)
def setup(local_rank):
torch.cuda.set_device(local_rank)
dist.init_process_group(backend='nccl')
def check_args_cfg(args, cfg):
pass
def update_yaml():
yamlpath = os.path.join('./train_yaml', 'test.yaml')
def init_log(cfg=None, eval_cfg=None, args=None, local_rank=0,):
cfg, eval_cfg = update_cfg_from_args(cfg, eval_cfg, args)
logger_path = os.path.join(cfg.TRAIN.SAVE_PATH, args.tensorboard + '/logs')
logger = setup_logger('Sequence Verification', logger_path, args.log_name, args.local_rank)
logger.info('----------------Running with args----------------\n{}\n'.format(vars(args)))
if args.cfg_from_args:
logger.info('-------------Update training cfg from args----------------\n')
logger.info('Running training with config:\n{}\n'.format(cfg))
if args.eval:
logger.info('-------------Update eval cfg from train config-------------\n')
logger.info('Running eval with config:\n{}\n'.format(eval_cfg))
# if cfg.MODEL.SAVE_MODEL_LOG and args.local_rank==0:
# model = build_model(cfg=cfg, args=args, model_log=True).to(args.local_rank)
# if args.backbone == 'resnet':
# model_log = summary(model, (16, 3, 180, 320), depth=3)
# else:
# model_log = summary(model, (16, 3, 224, 224), depth=3)
# logger.info('Running training with model:\n{}\n'.format(model_log))
# del model
return logger
def train(cfg, eval_cfg, args,):
if cfg.DATASET.NAME == 'CSV':
MAX_SEQ_LENGTH = 20
elif cfg.DATASET.NAME == 'COIN-SV':
MAX_SEQ_LENGTH = 25
elif cfg.DATASET.NAME == 'DIVING48-SV':
MAX_SEQ_LENGTH = 4
else:
raise ValueError('wrong cfg,DATASET.NAME')
local_rank = args.local_rank
setup(local_rank)
setup_seed(cfg.TRAIN.SEED + local_rank)
logger = init_log(cfg, eval_cfg, args, local_rank)
if dist.get_rank() == 0:
log_dir = args.tensorboard
writer = SummaryWriter(log_dir=os.path.join('log/', log_dir))
else:
writer = None
model = build_model(cfg=cfg, args=args, model_log=False).to(local_rank)
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = DDP(model, device_ids=[local_rank], output_device=local_rank, find_unused_parameters=True)
backbone_params = list(map(id, model.module.backbone.parameters()))
finetune_params = filter(lambda p: id(p) not in backbone_params, model.parameters())
# optimizer = torch.optim.AdamW([
# {'params': model.module.backbone.parameters(), 'lr': 1e-7},
# {'params': finetune_params},
# ], lr=cfg.TRAIN.LR, weight_decay=0.01)
if not args.pair:
args.info_mask = False
if args.info_ddp:
if args.info_mask:
from utils.loss import compute_info_loss_mask_ddp as compute_info_loss
logger.info('info w mask, w ddp')
else:
from utils.loss import compute_info_loss_ddp as compute_info_loss
logger.info('info w/o mask, w ddp')
else:
if args.info_mask:
from utils.loss import compute_info_loss_mask as compute_info_loss
logger.info('info w mask, w/o ddp')
else:
from utils.loss import compute_info_loss as compute_info_loss
logger.info('info w/0 mask, w/o ddp')
# Load checkpoint
start_epoch = 0
if args.load_path and os.path.isfile(args.load_path):
map_location = {'cuda:%d' % 0: 'cuda:%d' % local_rank}
checkpoint = torch.load(args.load_path, map_location=map_location)
new_state_dict = OrderedDict()
import ipdb
# ipdb.set_trace()
for k, v in checkpoint['model_state_dict'].items():
name = 'module.' + k
new_state_dict[name] = v
# ipdb.set_trace()
model.load_state_dict(new_state_dict, strict=False)
# model.load_state_dict(checkpoint['model_state_dict'])
start_epoch = checkpoint['epoch']
logger.info('-> Loaded checkpoint %s (epoch: %d)' % (args.load_path, start_epoch))
# Create checkpoint dir
if cfg.TRAIN.SAVE_PATH:
checkpoint_dir = os.path.join(cfg.TRAIN.SAVE_PATH, args.tensorboard + '/save_models')
if not os.path.exists(checkpoint_dir) and dist.get_rank() == 0:
os.makedirs(checkpoint_dir)
else:
checkpoint_dir = None
test_loader = load_dataset_ddp(eval_cfg, args, drop_last=False)
# if cfg.DATASET.NAME =='COIN-SV' or cfg.DATASET.NAME =='DIVING48-SV':
# eval_cfg2 = eval_cfg
# eval_cfg2.DATASET.TXT_PATH = cfg.DATASET.TXT_PATH.replace('train_pairs.txt', 'val_pairs.txt')
# eval_cfg2.DATASET.MODE = 'val'
# val_loader = load_dataset_ddp(eval_cfg2, args, drop_last=False)
if args.retrival:
retrival_cfg = eval_cfg
retrival_cfg.DATASET.TXT_PATH = cfg.DATASET.TXT_PATH.replace('train_pairs.txt', 'text_retrieval.txt')
retrival_loader = load_dataset_retrival_ddp(retrival_cfg, args, drop_last=False)
start_time = time.time()
# eval
model.eval()
# -------------------------------------------
# test on test set
auc_value, wdr_value = eval_per_epoch(model, test_loader, local_rank, eval_cfg, args)
logger.info('Epoch [{}/{}], AUC: {:.6f}, WDR: {:.4f}.'
.format(start_epoch, cfg.TRAIN.MAX_EPOCH, auc_value, wdr_value))
# -------------------------------------------
dist.barrier()
end_time = time.time()
duration = end_time - start_time
hour = duration // 3600
minute = (duration % 3600) // 60
sec = duration % 60
logger.info('Training cost %dh%dm%ds' % (hour, minute, sec))
def eval_per_epoch(model, val_loader,local_rank,eval_cfg,args):
with torch.no_grad():
labels, preds, labels1_all, labels2_all = None, None, None, None
for iter, sample in tqdm(enumerate(val_loader)):
# if iter == 1 and args.debug:
if iter == 1 and args.debug:
break
frames1_list = sample['clips1']
frames2_list = sample['clips2']
assert len(frames1_list) == len(frames2_list), 'frames1_list:{},frames2_list{}'.format(
len(frames1_list), len(frames2_list))
labels1 = sample['labels1']
labels2 = sample['labels2']
label = torch.tensor(np.array(labels1) == np.array(labels2)).to(local_rank)
embeds1_list = []
embeds2_list = []
for i in range(len(frames1_list)):
frames1 = frames_preprocess(frames1_list[i]).to(local_rank, non_blocking=True)
frames2 = frames_preprocess(frames2_list[i]).to(local_rank, non_blocking=True)
embeds1 = model(frames1, embed=True)
embeds2 = model(frames2, embed=True)
embeds1_list.append(embeds1.unsqueeze(dim=0))
embeds2_list.append(embeds2.unsqueeze(dim=0))
embeds1_avg = (torch.cat(embeds1_list, dim=0)).mean(dim=0)
embeds2_avg = (torch.cat(embeds2_list, dim=0)).mean(dim=0)
pred = pred_dist(args.dist, embeds1_avg, embeds2_avg)
torch.cuda.synchronize()
# gather from other gpu
pred = all_gather_concat(pred)
label = all_gather_concat(label)
labels1 = all_gather_object(labels1)
labels2 = all_gather_object(labels2)
# add all data to list
if iter == 0:
preds = pred
labels = label
labels1_all = labels1
labels2_all = labels2
else:
preds = torch.cat([preds, pred])
labels = torch.cat([labels, label])
labels1_all += labels1
labels2_all += labels2
fpr, tpr, thresholds = roc_curve(labels.cpu().detach().numpy(), preds.cpu().detach().numpy(), pos_label=0)
auc_value_val = auc(fpr, tpr)
wdr_value_val = compute_WDR(preds, labels1_all, labels2_all, eval_cfg.DATASET.NAME)
return auc_value_val, wdr_value_val
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def update_cfg_from_args(cfg, eval_cfg, args):
if args.cfg_from_args:
cfg.TRAIN.BATCH_SIZE = args.batch_size
cfg.TRAIN.LR = args.lr
cfg.DATASET.NUM_SAMPLE = args.num_samples
cfg.MODEL.SEQ_LOSS_COEF = args.seq_loss
cfg.MODEL.INFO_LOSS_COEF = args.info_loss
cfg.MODEL.SAVE_EPOCHS = args.save_epochs
cfg.TRAIN.MAX_EPOCH = args.max_epoch
cfg.DATASET.NUM_CLIP = args.num_clip
cfg.DATASET.NUM_SAMPLE = args.NUM_SAMPLE
cfg.TRAIN.BATCH_SIZE = cfg.TRAIN.BATCH_SIZE if args.pair else cfg.TRAIN.BATCH_SIZE*2
# print(cfg.TRAIN.BATCH_SIZE)
# cfg.DATASET.NUM_SAMPLE = cfg.DATASET.NUM_SAMPLE if args.pair else 800
cfg.DATASET.RANDOM_SAMPLE = args.random_sample
cfg.TRAIN.SAVE_PATH = os.path.join(cfg.TRAIN.SAVE_PATH, args.tensorboard)
if args.eval:
eval_cfg.TRAIN.BATCH_SIZE = cfg.TRAIN.BATCH_SIZE
eval_cfg.DATASET.NUM_CLIP = cfg.DATASET.NUM_CLIP
eval_cfg.DATASET.NUM_WORKERS = cfg.DATASET.NUM_WORKERS
eval_cfg.DATASET.NAME = cfg.DATASET.NAME
eval_cfg.DATASET.TXT_PATH = cfg.DATASET.TXT_PATH.replace('train_pairs.txt', 'test_pairs.txt')
eval_cfg.DATASET.NUM_CLASS = cfg.DATASET.NUM_CLASS
return cfg, eval_cfg
if __name__ == "__main__":
args = parse_args()
cfg = get_cfg_defaults()
if args.config:
cfg.merge_from_file(args.config)
if args.eval:
eval_cfg = get_cfg_defaults()
if args.eval_config:
eval_cfg.merge_from_file(args.eval_config)
else:
raise IOError('need value')
print('Warning: IF USE PAIR DATA, PLS CHECK NUM CLIP.')
use_cuda = cfg.TRAIN.USE_CUDA and torch.cuda.is_available()
train(cfg, eval_cfg, args,)