-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
523 lines (447 loc) · 22 KB
/
main.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import os
import os.path as osp
import torch
import numpy as np
import pandas as pd
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader, RandomSampler
from torch.cuda.amp import GradScaler, autocast
from datetime import datetime
from easydict import EasyDict as edict
from tqdm import tqdm
from collections import defaultdict
from config import cfg
from torchlight import initialize_exp, set_seed, get_dump_path
from src.data import load_data, Collator_base, EADataset
from src.utils import set_optim, Loss_log, pairwise_distances, csls_sim
# add model here
from model import MCLEA
from src.distributed_utils import init_distributed_mode, dist_pdb, is_main_process, reduce_value, cleanup
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
import torch.nn.functional as F
import scipy
import gc
import copy
class Runner:
def __init__(self, args, writer=None, logger=None, rank=0):
self.datapath = edict()
self.datapath.log_dir = get_dump_path(args)
self.datapath.model_dir = os.path.join(self.datapath.log_dir, 'model')
self.rank = rank
# TODO: data init code
self.args = args
self.writer = writer
self.logger = logger
self.scaler = GradScaler()
# TODO: model init code
self.model_list = []
set_seed(args.random_seed)
self.data_init()
self.model_choise()
set_seed(args.random_seed)
if self.args.only_test:
self.dataloader_init(test_set=self.test_set)
else:
self.dataloader_init(train_set=self.train_set, eval_set=self.eval_set, test_set=self.test_set)
if self.args.dist:
self.model_sync()
else:
self.model_list = [self.model]
if self.args.il:
assert self.args.il_start < self.args.epoch
train_epoch_1_stage = self.args.il_start
else:
train_epoch_1_stage = self.args.epoch
self.optim_init(self.args, total_epoch=train_epoch_1_stage)
def model_sync(self):
folder = osp.join(self.args.data_path, "tmp")
if not os.path.exists(folder):
os.makedirs(folder)
checkpoint_path = osp.join(folder, "initial_weights.pt")
if self.rank == 0:
torch.save(self.model.state_dict(), checkpoint_path)
dist.barrier()
self.model = self._model_sync(self.model, checkpoint_path)
def _model_sync(self, model, checkpoint_path):
model.load_state_dict(torch.load(checkpoint_path, map_location=self.args.device))
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(self.args.device)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[self.args.gpu], find_unused_parameters=True)
self.model_list.append(model)
model = model.module
return model
def model_choise(self):
if self.args.model_name == "MCLEA":
self.model = MCLEA(self.KGs, self.args)
self.model = self._load_model(self.model)
total_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
self.logger.info(f"total params num: {total_params}")
def optim_init(self, opt, total_step=None, total_epoch=None, accumulation_step=None):
step_per_epoch = len(self.train_dataloader)
if total_epoch is not None:
opt.total_steps = int(step_per_epoch * total_epoch)
else:
opt.total_steps = int(step_per_epoch * opt.epoch) if total_step is None else int(total_step)
opt.warmup_steps = int(opt.total_steps * 0.15)
if self.rank == 0 and total_step is None:
self.logger.info(f"warmup_steps: {opt.warmup_steps}")
self.logger.info(f"total_steps: {opt.total_steps}")
self.logger.info(f"weight_decay: {opt.weight_decay}")
freeze_part = []
self.optimizer, self.scheduler = set_optim(opt, self.model_list, freeze_part, accumulation_step)
def data_init(self):
self.KGs, self.non_train, self.train_set, self.eval_set, self.test_set, self.test_ill_ = load_data(self.logger, self.args)
self.train_ill = self.train_set.data
self.eval_left = torch.LongTensor(self.eval_set[:, 0].squeeze()).cuda()
self.eval_right = torch.LongTensor(self.eval_set[:, 1].squeeze()).cuda()
if self.test_set is not None:
self.test_left = torch.LongTensor(self.test_ill[:, 0].squeeze()).cuda()
self.test_right = torch.LongTensor(self.test_ill[:, 1].squeeze()).cuda()
self.eval_sampler = None
if self.args.dist and not self.args.only_test:
self.train_sampler = torch.utils.data.distributed.DistributedSampler(self.train_set)
self.eval_sampler = torch.utils.data.distributed.DistributedSampler(self.eval_set)
if self.test_set is not None:
self.test_sampler = torch.utils.data.distributed.DistributedSampler(self.test_set)
def dataloader_init(self, train_set=None, eval_set=None, test_set=None):
bs = self.args.batch_size
collator = Collator_base(self.args)
if self.args.dist and not self.args.only_test:
self.args.workers = min([os.cpu_count(), self.args.batch_size, self.args.workers])
if train_set is not None:
self.train_dataloader = self._dataloader_dist(train_set, self.train_sampler, bs, collator)
if test_set is not None:
self.test_dataloader = self._dataloader_dist(test_set, self.test_sampler, bs, collator)
if eval_set is not None:
self.eval_dataloader = self._dataloader_dist(eval_set, self.eval_sampler, bs, collator)
else:
self.args.workers = min([os.cpu_count(), self.args.batch_size, self.args.workers])
if train_set is not None:
self.train_dataloader = self._dataloader(train_set, bs, collator)
if test_set is not None:
self.test_dataloader = self._dataloader(test_set, bs, collator)
if eval_set is not None:
self.eval_dataloader = self._dataloader(eval_set, bs, collator)
def _dataloader_dist(self, train_set, train_sampler, batch_size, collator):
train_dataloader = DataLoader(
train_set,
sampler=train_sampler,
pin_memory=True,
num_workers=self.args.workers,
persistent_workers=True,
drop_last=True,
batch_size=batch_size,
collate_fn=collator
)
return train_dataloader
def _dataloader(self, train_set, batch_size, collator):
train_dataloader = DataLoader(
train_set,
num_workers=self.args.workers,
persistent_workers=True,
shuffle=(self.args.only_test == 0),
# drop_last=(self.args.only_test == 0),
drop_last=False,
batch_size=batch_size,
collate_fn=collator
)
return train_dataloader
def run(self):
self.loss_log = Loss_log()
self.curr_loss = 0.
self.lr = self.args.lr
self.curr_loss_dic = defaultdict(float)
self.weight = [1, 1, 1, 1, 1, 1]
self.loss_weight = [1, 1]
self.loss_item = 99999.
self.step = 1
self.epoch = 0
self.new_links = []
self.best_model_wts = None
self.best_mrr = 0
self.early_stop_init = 500
self.early_stop_count = self.early_stop_init
self.stage = 0
with tqdm(total=self.args.epoch) as _tqdm:
for i in range(self.args.epoch):
if self.args.dist and not self.args.only_test:
self.train_sampler.set_epoch(i)
# -------------------------------
self.epoch = i
if self.args.il and (self.epoch == self.args.il_start and self.stage == 0) or (self.early_stop_count <= 0 and self.epoch <= self.args.il_start):
if self.early_stop_count <= 0:
logger.info(f"Early stop in epoch {self.epoch}... Begin iteration....")
self.stage = 1
self.early_stop_init = 800
self.early_stop_count = self.early_stop_init
self.step = 1
self.args.lr = self.args.lr / 5
self.optim_init(self.args, total_epoch=(self.args.epoch - self.args.il_start) * 3)
if self.best_model_wts is not None:
self.logger.info("load from the best model before IL... ")
self.model.load_state_dict(self.best_model_wts)
name = self._save_name_define()
self.test(save_name=f"{name}_test_ep{self.args.epoch}_no_iter")
if self.stage == 1 and (self.epoch + 1) % self.args.semi_learn_step == 0 and self.args.il:
self.il_for_ea()
if self.stage == 1 and (self.epoch + 1) % (self.args.semi_learn_step * 10) == 0 and len(self.new_links) != 0 and self.args.il:
self.il_for_data_ref()
self.train(_tqdm)
self.loss_log.update(self.curr_loss)
self.loss_item = self.loss_log.get_loss()
_tqdm.set_description(f'Train | Ep [{self.epoch}/{self.args.epoch}] Step [{self.step}/{self.args.total_steps}] LR [{self.lr:.5f}] Loss {self.loss_log.get_loss():.5f} ')
self.update_loss_log()
if (i + 1) % self.args.eval_epoch == 0:
self.eval()
_tqdm.update(1)
if self.stage == 1 and self.early_stop_count <= 0:
logger.info(f"Early stop in epoch {self.epoch}")
break
name = self._save_name_define()
if self.best_model_wts is not None:
self.logger.info("load from the best model before final testing ... ")
self.model.load_state_dict(self.best_model_wts)
self.test(save_name=f"{name}_test_ep{self.args.epoch}")
if self.rank == 0:
self.logger.info(f"min loss {self.loss_log.get_min_loss()}")
if not self.args.only_test and self.args.save_model:
self._save_model(self.model, input_name=name)
def il_for_ea(self):
with torch.no_grad():
final_emb = self.model.joint_emb_generat()
final_emb = F.normalize(final_emb)
self.new_links = self.model.Iter_new_links(self.epoch, self.non_train["left"], final_emb, self.non_train["right"], new_links=self.new_links)
if (self.epoch + 1) % (self.args.semi_learn_step * 5) == 0:
self.logger.info(f"[epoch {self.epoch}] #links in candidate set: {len(self.new_links)}")
def il_for_data_ref(self):
self.non_train["left"], self.non_train["right"], self.train_ill, self.new_links = self.model.data_refresh(
self.logger, self.train_ill, self.test_ill_, self.non_train["left"], self.non_train["right"], new_links=self.new_links)
set_seed(self.args.random_seed)
self.train_set = EADataset(self.train_ill)
self.dataloader_init(train_set=self.train_set)
def _save_name_define(self):
prefix = ""
if self.args.dist:
prefix = f"dist_{prefix}"
if self.args.il:
prefix = f"il{self.args.epoch-self.args.il_start}_b{self.args.il_start}_{prefix}"
name = f'{self.args.exp_id}_{prefix}'
return name
def train(self, _tqdm):
self.model.train()
curr_loss = 0.
self.loss_log.acc_init()
accumulation_steps = self.args.accumulation_steps
for batch in self.train_dataloader:
loss, output = self.model(batch)
loss = loss / accumulation_steps
self.scaler.scale(loss).backward()
if self.args.dist:
loss = reduce_value(loss, average=True)
self.step += 1
if not self.args.dist or is_main_process():
curr_loss += loss.item()
self.output_statistic(loss, output)
if self.step % accumulation_steps == 0:
self.scaler.unscale_(self.optimizer)
for model in self.model_list:
torch.nn.utils.clip_grad_norm_(model.parameters(), self.args.clip)
scale = self.scaler.get_scale()
self.scaler.step(self.optimizer)
self.scaler.update()
skip_lr_sched = (scale > self.scaler.get_scale())
if not skip_lr_sched:
self.scheduler.step()
if not self.args.dist or is_main_process():
self.lr = self.scheduler.get_last_lr()[-1]
self.writer.add_scalars("lr", {"lr": self.lr}, self.step)
for model in self.model_list:
model.zero_grad(set_to_none=True)
if self.args.dist:
torch.cuda.synchronize(self.args.device)
return curr_loss
def output_statistic(self, loss, output):
self.curr_loss += loss.item()
if output is None:
return
for key in output['loss_dic'].keys():
self.curr_loss_dic[key] += output['loss_dic'][key]
if 'weight' in output and output['weight'] is not None:
self.weight = output['weight']
if 'loss_weight' in output and output['loss_weight'] is not None:
self.loss_weight = output['loss_weight']
def update_loss_log(self):
vis_dict = {"train_loss": self.curr_loss}
vis_dict.update(self.curr_loss_dic)
self.writer.add_scalars("loss", vis_dict, self.step)
if self.weight is not None:
weight_dic = {}
weight_dic["img"] = self.weight[0]
weight_dic["attr"] = self.weight[1]
weight_dic["rel"] = self.weight[2]
weight_dic["graph"] = self.weight[3]
if self.args.w_name or self.args.w_char:
weight_dic["name"] = self.weight[4]
weight_dic["char"] = self.weight[5]
self.writer.add_scalars("modal_weight", weight_dic, self.step)
if self.loss_weight is not None and self.loss_weight != [1, 1]:
weight_dic = {}
weight_dic["mask"] = 1 / (self.loss_weight[0]**2)
weight_dic["kpi"] = 1 / (self.loss_weight[1]**2)
self.writer.add_scalars("loss_weight", weight_dic, self.step)
self.curr_loss = 0.
for key in self.curr_loss_dic:
self.curr_loss_dic[key] = 0.
def eval(self, last_epoch=False, save_name=""):
test_left = self.eval_left
test_right = self.eval_right
self.model.eval()
self._test(test_left, test_right, last_epoch=last_epoch, save_name=save_name)
def test(self, save_name=""):
if self.test_set is None:
test_left = self.eval_left
test_right = self.eval_right
else:
test_left = self.test_left
test_right = self.test_right
self.model.eval()
self.logger.info(" --------------------- Test result --------------------- ")
self._test(test_left, test_right, last_epoch=True, save_name=save_name)
def _test(self, test_left, test_right, last_epoch=False, save_name="", loss=None):
with torch.no_grad():
w_normalized = F.softmax(self.model.multimodal_encoder.fusion.weight.reshape(-1), dim=0)
if self.rank == 0:
appdx = ""
if self.args.w_name and self.args.w_char:
appdx = f"-[name_{w_normalized[4]:.3f}]-[char_{w_normalized[5]:.3f}]"
self.logger.info(f"weight_raw:[img_{w_normalized[0]:.3f}]-[attr_{w_normalized[1]:.3f}]-[rel_{w_normalized[2]:.3f}]-[graph_{w_normalized[3]:.3f}]{appdx}")
final_emb = self.model.joint_emb_generat()
final_emb = F.normalize(final_emb)
top_k = [1, 10, 50]
acc_l2r = np.zeros((len(top_k)), dtype=np.float32)
acc_r2l = np.zeros((len(top_k)), dtype=np.float32)
test_total, test_loss, mean_l2r, mean_r2l, mrr_l2r, mrr_r2l = 0, 0., 0., 0., 0., 0.
if self.args.distance == 2:
distance = pairwise_distances(final_emb[test_left], final_emb[test_right])
elif self.args.distance == 1:
distance = torch.FloatTensor(scipy.spatial.distance.cdist(
final_emb[test_left].cpu().data.numpy(),
final_emb[test_right].cpu().data.numpy(), metric="cityblock"))
if self.args.csls is True:
distance = 1 - csls_sim(1 - distance, self.args.csls_k)
if last_epoch:
to_write = []
test_left_np = test_left.cpu().numpy()
test_right_np = test_right.cpu().numpy()
to_write.append(["idx", "rank", "query_id", "gt_id", "ret1", "ret2", "ret3"])
for idx in range(test_left.shape[0]):
values, indices = torch.sort(distance[idx, :], descending=False)
rank = (indices == idx).nonzero(as_tuple=False).squeeze().item()
mean_l2r += (rank + 1)
mrr_l2r += 1.0 / (rank + 1)
for i in range(len(top_k)):
if rank < top_k[i]:
acc_l2r[i] += 1
# save idx, correct rank pos, and indices
if last_epoch:
indices = indices.cpu().numpy()
to_write.append([idx, rank, test_left_np[idx], test_right_np[idx], test_right_np[indices[0]], test_right_np[indices[1]], test_right_np[indices[2]]])
if last_epoch:
import csv
if save_name == "":
save_name = self.args.model_name
save_pred_path = osp.join(self.args.data_path, self.args.model_name, f"{save_name}_pred")
os.makedirs(save_pred_path, exist_ok=True)
with open(osp.join(save_pred_path, f"{self.args.data_choice}_pred.txt"), "w") as f:
wr = csv.writer(f, dialect='excel')
wr.writerows(to_write)
for idx in range(test_right.shape[0]):
_, indices = torch.sort(distance[:, idx], descending=False)
rank = (indices == idx).nonzero(as_tuple=False).squeeze().item()
mean_r2l += (rank + 1)
mrr_r2l += 1.0 / (rank + 1)
for i in range(len(top_k)):
if rank < top_k[i]:
acc_r2l[i] += 1
mean_l2r /= test_left.size(0)
mean_r2l /= test_right.size(0)
mrr_l2r /= test_left.size(0)
mrr_r2l /= test_right.size(0)
for i in range(len(top_k)):
acc_l2r[i] = round(acc_l2r[i] / test_left.size(0), 4)
acc_r2l[i] = round(acc_r2l[i] / test_right.size(0), 4)
gc.collect()
Loss_out = f", Loss = {self.loss_item:.4f}"
if self.rank == 0:
self.logger.info(f"Ep {self.epoch} | l2r: acc of top {top_k} = {acc_l2r}, mr = {mean_l2r:.3f}, mrr = {mrr_l2r:.3f}{Loss_out}")
self.logger.info(f"Ep {self.epoch} | r2l: acc of top {top_k} = {acc_r2l}, mr = {mean_r2l:.3f}, mrr = {mrr_r2l:.3f}{Loss_out}")
self.early_stop_count -= 1
if mrr_l2r > max(self.loss_log.acc) and not last_epoch:
self.logger.info(f"Best model update in Ep {self.epoch}: MRR from [{max(self.loss_log.acc)}] --> [{mrr_l2r}] ... ")
self.loss_log.update_acc(mrr_l2r)
self.early_stop_count = self.early_stop_init
self.best_model_wts = copy.deepcopy(self.model.state_dict())
def _load_model(self, model, model_name=None):
if model_name is None:
model_name = self.args.model_name_save
save_path = osp.join(self.args.data_path, self.args.model_name, 'save')
save_path = osp.join(save_path, f'{model_name}.pkl')
if (len(model_name) == 0 or not os.path.exists(save_path)) and self.rank == 0:
if len(model_name) > 0:
self.logger.info(f"{model_name}.pkl not exist!!")
else:
self.logger.info("Random init...")
model.cuda()
return model
if 'Dist' in self.args.model_name:
model.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(save_path, map_location=self.args.device).items()})
else:
model.load_state_dict(torch.load(save_path, map_location=self.args.device))
model.cuda()
if self.rank == 0:
self.logger.info(f"loading model [{model_name}.pkl] done!")
return model
def _save_model(self, model, input_name=""):
model_name = self.args.model_name
save_path = osp.join(self.args.data_path, model_name, 'save')
os.makedirs(save_path, exist_ok=True)
if input_name == "":
input_name = self._save_name_define()
save_path = osp.join(save_path, f'{input_name}.pkl')
if model is None:
return
if self.args.save_model:
torch.save(model.state_dict(), save_path)
self.logger.info(f"saving [{save_path}] done!")
return save_path
if __name__ == '__main__':
cfg = cfg()
cfg.get_args()
cfgs = cfg.update_train_configs()
set_seed(cfgs.random_seed)
if cfgs.dist and not cfgs.only_test:
init_distributed_mode(args=cfgs)
else:
torch.multiprocessing.set_sharing_strategy('file_system')
rank = cfgs.rank
writer, logger = None, None
if rank == 0:
logger = initialize_exp(cfgs)
logger_path = get_dump_path(cfgs)
cfgs.time_stamp = "{0:%Y-%m-%dT%H-%M-%S/}".format(datetime.now())
comment = f'bath_size={cfgs.batch_size} exp_id={cfgs.exp_id}'
if not cfgs.no_tensorboard and not cfgs.only_test:
writer = SummaryWriter(log_dir=os.path.join(logger_path, 'tensorboard', cfgs.time_stamp), comment=comment)
cfgs.device = torch.device(cfgs.device)
torch.cuda.set_device(cfgs.gpu)
runner = Runner(cfgs, writer, logger, rank)
if cfgs.only_test:
runner.test()
else:
runner.run()
if not cfgs.no_tensorboard and not cfgs.only_test and rank == 0:
writer.close()
logger.info("done!")
if cfgs.dist and not cfgs.only_test:
dist.barrier()
dist.destroy_process_group()