-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
392 lines (333 loc) · 14.4 KB
/
train.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
392
import warnings
warnings.filterwarnings('ignore')
import ast
import os
import os.path as osp
import signal
import time
import gc
import random
import argparse
import tqdm
import importlib
import socket
from pathlib import Path
from typing import Dict, Union
import torch
import torch.nn as nn
import torch.nn.functional as F
# import torch.distributed as dist
# import torch.multiprocessing as mp
# from torch.nn.parallel import DistributedDataParallel as DDP
from torch.cuda.amp import GradScaler, autocast
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader, ConcatDataset, SequentialSampler, RandomSampler
import numpy as np
import pandas as pd
import polars as pl
from timm.scheduler import CosineLRScheduler
from configs.config_0 import CFG
from local_dataset.simple_dataset import SimpleDataset
from models.finformer import YCModel
from utils import batch_to_device
from utils.handlers import BestCheckpoint
from utils.torchmetrics import CE
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def create_dataloaders(cfg: CFG, dataset_class, rank):
train_ds: Union[SimpleDataset, ConcatDataset]
val_ds: SimpleDataset
test_ds: SimpleDataset
if cfg.do_train:
train_ds = dataset_class(cfg, mode='train')
val_ds = dataset_class(cfg, mode='val')
test_ds = dataset_class(cfg, mode='test')
train_sampler = RandomSampler(train_ds) if cfg.do_train else None
val_sampler = SequentialSampler(val_ds)
test_sampler = SequentialSampler(test_ds)
train_dl = DataLoader(
train_ds,
batch_size=cfg.train_bs,
num_workers=cfg.num_workers,
sampler=train_sampler,
prefetch_factor=cfg.prefetch_factor,
pin_memory=True,
persistent_workers=True,
) if cfg.do_train else None
val_dl = DataLoader(
val_ds,
batch_size=cfg.val_bs,
num_workers=cfg.num_workers,
sampler=val_sampler,
prefetch_factor=cfg.prefetch_factor,
pin_memory=True,
persistent_workers=True,
)
test_dl = DataLoader(
test_ds,
batch_size=cfg.val_bs,
num_workers=cfg.num_workers,
sampler=test_sampler,
prefetch_factor=cfg.prefetch_factor,
pin_memory=True,
persistent_workers=True,
)
return train_dl, val_dl, test_dl, test_ds
def ce(outputs, labels):
return nn.CrossEntropyLoss()(outputs, labels)
def vec_translate(a, d):
return np.vectorize(d.__getitem__)(a)
@torch.inference_mode()
def get_embeddings(model: YCModel, dataloader, device: str = 'cuda:0'):
embeds = dict()
for data in tqdm.tqdm(dataloader):
data = batch_to_device(data)
with autocast(enabled=True):
embeddings = model.extract(data['sample'], data['mask'])
embeddings = F.normalize(embeddings, dim=1).detach().cpu().numpy()
for trackid, embedding in zip(data['track_id'].cpu().numpy(), embeddings):
embeds[trackid] = embedding
return embeds
@torch.inference_mode()
def get_ranked_list_exact(embeds, top_size: int = 100, device: str = 'cuda:0', similarity_map=False):
num_chunks = 512
index2track = list(embeds.keys())
embeds = np.stack(list(embeds.values()), axis=0)
embeds = torch.from_numpy(embeds).to(device)
ranked_list = dict()
track_id_index = 0
similarity_all = []
for chunk in tqdm.tqdm(embeds.chunk(num_chunks)):
chunk = chunk.to(device)
cos_sim_chunk_values = torch.mm(chunk, embeds.transpose(0, 1))
cos_sim_chunk_values, cos_sim_chunk = cos_sim_chunk_values.sort(dim=1, descending=True)
cos_sim_chunk = cos_sim_chunk[:, 1:top_size + 50].detach().cpu().numpy()
cos_sim_chunk_values = cos_sim_chunk_values[:, 1:top_size+ 50]
for similarity_index, similarity in zip(cos_sim_chunk, cos_sim_chunk_values):
gallery_mask = similarity_index != track_id_index
similarity_index = similarity_index[gallery_mask]
similarity = similarity[gallery_mask]
current_trackid = index2track[track_id_index]
ranked_list[current_trackid] = vec_translate(similarity_index[:top_size], index2track)
similarity_all.append(similarity[:top_size].detach().cpu().numpy())
track_id_index += 1
if similarity_map:
similarity_all = np.stack(similarity_all)
return ranked_list, similarity_all
return ranked_list
def position_discounter(position):
return 1.0 / np.sqrt(position)
def get_ideal_dcg(relevant_items_count, top_size):
dcg = 0.0
for result_indx in range(min(top_size, relevant_items_count)):
position = result_indx + 1
dcg += position_discounter(position)
return dcg
def compute_dcg(query_trackid, ranked_list, track2artist_map, top_size):
query_artistid = track2artist_map[query_trackid]
dcg = 0.0
for result_indx, result_trackid in enumerate(ranked_list[:top_size]):
assert result_trackid != query_trackid
position = result_indx + 1
discounted_position = position_discounter(position)
result_artistid = track2artist_map[result_trackid]
if result_artistid == query_artistid:
dcg += discounted_position
return dcg
def eval_submission(submission, gt_meta_info, top_size = 100):
# track2artist_map = gt_meta_info.set_index('track_id')['clique'].to_dict()
track2artist_map = {}
for _, row in gt_meta_info.iterrows():
clique = row['clique']
versions = row['versions']
for version in versions:
track2artist_map[version] = clique
artist2tracks_map = gt_meta_info.set_index('clique')['versions'].to_dict()
ndcg_list = []
for query_trackid in tqdm.tqdm(submission.keys()):
query_trackid = int(query_trackid)
ranked_list = submission[query_trackid]
query_artistid = track2artist_map[query_trackid]
query_artist_tracks_count = len(artist2tracks_map[query_artistid])
ideal_dcg = get_ideal_dcg(query_artist_tracks_count-1, top_size=top_size)
dcg = compute_dcg(query_trackid, ranked_list, track2artist_map, top_size=top_size)
try:
ndcg_list.append(dcg/ideal_dcg)
except ZeroDivisionError:
continue
return np.mean(ndcg_list)
def main_worker(cfg: CFG, config_name):
print(f"Running normal training")
seed_everything(cfg.seed)
rank = int(cfg.device.split(':')[-1])
current_device = cfg.device
torch.cuda.set_device(rank)
dataset_class = importlib.import_module(f"local_dataset.{cfg.dataset}".replace('.py', '')).SimpleDataset
model_class = importlib.import_module(f"models.{cfg.model}".replace('.py', '')).YCModel
df = pd.read_csv('data.csv')
tmp = np.sqrt(1 / np.sqrt(df[df['label'] != -1]['label'].value_counts().sort_index().values))
margins = (tmp - tmp.min()) / (tmp.max() - tmp.min()) * cfg.margin_slope + cfg.margin_offset
if cfg.label_flip_prob > 1e-6:
margins = np.stack([margins, margins]).flatten()
cfg.margins = margins
model: YCModel = model_class(cfg).to(rank)
my_list = ['arcface_head.weight']
params = list(filter(lambda kv: kv[0] in my_list, model.named_parameters()))
base_params = list(filter(lambda kv: kv[0] not in my_list, model.named_parameters()))
head_params = [p[1] for p in params]
rest_params = [p[1] for p in base_params]
if cfg.optim == 'AdamW':
# opt = torch.optim.AdamW(model.parameters(), lr=cfg.lr, weight_decay=cfg.wd)
opt = torch.optim.AdamW(
[
{"params" : head_params, "lr" : cfg.lr * cfg.head_lr_multiple},
{"params" : rest_params, "lr" : cfg.lr}
], lr=cfg.lr, weight_decay=cfg.wd
)
elif cfg.optim == 'SGD':
opt = torch.optim.SGD(model.parameters(), lr=cfg.lr, weight_decay=cfg.wd)
else:
raise NotImplementedError(f"optimizer {cfg.optim} is not supported")
if cfg.weights != '':
sd = torch.load(cfg.weights, map_location=torch.device('cpu'))
sd = {k.replace('module.', ''):v for k,v in sd.items()}
print(model.load_state_dict(sd, strict=False))
opt_sd = torch.load(cfg.weights.replace('model_', 'opt_'), map_location=torch.device('cpu'))
if cfg.load_opt:
print(opt.load_state_dict(opt_sd))
for g in opt.param_groups:
g['lr'] = cfg.lr
g['initial_lr'] = cfg.lr
scaler = GradScaler()
train_dl, val_dl, test_dl, test_ds = create_dataloaders(cfg, dataset_class, rank)
num_steps_per_epoch = len(train_dl) if cfg.do_train else 0
if cfg.use_scheduler and cfg.do_train:
warmup = cfg.epochs_warmup * num_steps_per_epoch
nsteps = cfg.epochs * num_steps_per_epoch
sched = CosineLRScheduler(
opt,
warmup_t=warmup if not cfg.use_t0 else 0,
warmup_lr_init=0.0,
warmup_prefix=not cfg.use_t0,
t_initial=(nsteps - warmup) if not cfg.use_t0 else cfg.T0,
lr_min=cfg.lr_min,
cycle_limit=1 if not cfg.use_t0 else 123456
)
else:
sched = None
if cfg.criterion == 'ce':
criterion = ce
loss_class = CE
else:
raise NotImplementedError(f"criterion : {cfg.criterion}")
exps_dir = Path(cfg.exp_dir)
if cfg.debug:
exps_dir = Path(str(exps_dir) + '_DEBUG')
exp_dir = exps_dir / f"exp_{config_name.split('_')[-1].split('.')[0]}"
weight_dir = exp_dir / 'weights'
artifacts_dir = exp_dir / 'artifacts'
if rank == 0:
summary_writer = SummaryWriter(log_dir=exp_dir)
if not osp.isdir(cfg.exp_dir):
os.makedirs(cfg.exp_dir)
if not osp.isdir(artifacts_dir):
os.makedirs(artifacts_dir)
ckpt_handler = BestCheckpoint(weight_dir, mode=cfg.checkpoint_mode)
# print(model)
accum_steps = 0
for epoch in range(1, cfg.epochs + 1):
if cfg.do_train:
train_pbar = tqdm.tqdm(train_dl) if rank == 0 else train_dl
train_loss = loss_class()
for t_iter, batch in enumerate(train_pbar):
if t_iter >= cfg.max_steps_per_epoch:
break
global_iter = (epoch - 1) * num_steps_per_epoch + t_iter
batch = batch_to_device(batch, device=current_device)
with autocast(enabled=cfg.enable_amp, dtype=cfg.autocast_dtype):
preds = model(batch['sample'], batch['label'], batch['mask'])
loss = criterion(preds, batch['label'])
if global_iter < 100 or loss.item() <= cfg.loss_threshold:
scaler.scale(loss).backward()#
accum_steps += 1
if accum_steps % cfg.grad_accum_steps == 0:
if cfg.max_grad_norm is not None:
scaler.unscale_(opt)
torch.nn.utils.clip_grad_norm_(model.parameters(), cfg.max_grad_norm)
scaler.step(opt)
scaler.update()
if cfg.use_scheduler:
sched.step(global_iter)
accum_steps = 0
opt.zero_grad(set_to_none=cfg.set_grad_to_none)
else:
scaler.scale(loss).backward()
opt.zero_grad(set_to_none=cfg.set_grad_to_none)
train_loss.accumulate(preds, batch['label'])
if rank == 0:
lr = opt.param_groups[0]['lr']
train_pbar.set_description(f"loss : {loss.item():.4f}, lr : {lr:.5f}")
summary_writer.add_scalar("lr", lr, global_iter)
if rank == 0:
summary_writer.add_scalar('loss/train', train_loss.value().item(), epoch)
## use single gpu for eval, since can't use gather for strings (required by comp. sub format)
df = pd.read_csv('data.csv')
df_valid = df[df['split'] == 'val'].reset_index(drop=True)
valid_cliques = pd.read_csv(cfg.storage_dir / 'cliques2versions.tsv', sep='\t')
valid_cliques = valid_cliques[valid_cliques['clique'].isin(df_valid['clique'])].reset_index(drop=True)
valid_cliques['versions'] = valid_cliques['versions'].apply(ast.literal_eval)
if (epoch - 1) % cfg.do_eval_every == 0 and rank == 0:
model.eval()
###
val_embeds = get_embeddings(model, val_dl)
ranked_list, sim_map_valid = get_ranked_list_exact(val_embeds, top_size=100, similarity_map=True)
print("Calculating the metric")
nDCG = eval_submission(ranked_list, valid_cliques)
summary_writer.add_scalar('nDCG/val', nDCG, epoch)
if not osp.isdir(weight_dir):
os.makedirs(weight_dir)
###
###
test_embeds = get_embeddings(model, test_dl)
ranked_list, sim_map_test = get_ranked_list_exact(test_embeds, top_size=100, similarity_map=True)
# ###
if cfg.save_checkpoints:
print(f"Creating test .parquet file...")
ckpt_handler.update(
model,
opt,
ranked_list,
val_embeds,
test_embeds,
sim_map_valid,
sim_map_test,
epoch,
nDCG
)
print(f"Done!")
model.train()
if cfg.debug and epoch >= 5:
break
if not cfg.do_train:
exit()
if rank == 0:
ds_name = f"yc2024-music-exp{config_name.split('_')[1].split('.')[0]}"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='base_config')
args = parser.parse_args()
config_name = args.config
cfg: CFG = importlib.import_module(f'configs.{config_name}'.replace('.py', '')).cfg
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
print(f'cuda:{i} - {torch.cuda.get_device_name(i)}')
else:
print('CUDA is not available')
main_worker(cfg, config_name)