forked from trinhvg/IMPash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_contrast.py
98 lines (71 loc) · 2.85 KB
/
main_contrast.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
"""
DDP training for Contrastive Learning
"""
from __future__ import print_function
# import os
import os
os.environ["CUDA_VISIBLE_DEVICES"]="4,5"
# print(os.environ)
import torch
import torch.nn as nn
import torch.utils.data.distributed
import torch.multiprocessing as mp
from options.train_options import TrainOptions
from learning.contrast_trainer_stitch import ContrastTrainer
from networks.build_backbone import build_model
from datasets.util import build_contrast_loader
from memory.build_memory import build_mem
def main():
args = TrainOptions().parse()
args.distributed = args.world_size > 1 or args.multiprocessing_distributed
ngpus_per_node = torch.cuda.device_count()
if args.multiprocessing_distributed:
args.world_size = ngpus_per_node * args.world_size
mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, args))
else:
raise NotImplementedError('Currently only DDP training')
def main_worker(gpu, ngpus_per_node, args):
# initialize trainer and ddp environment
trainer = ContrastTrainer(args)
trainer.init_ddp_environment(gpu, ngpus_per_node)
# build model
model, model_ema = build_model(args)
# build dataset
train_dataset, train_loader, train_sampler = \
build_contrast_loader(args, ngpus_per_node)
# show_augment(train_dataset, 0)
# build memory
contrast = build_mem(args, len(train_dataset))
contrast.cuda()
# build criterion and optimizer
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(model.parameters(),
lr=args.learning_rate,
momentum=args.momentum,
weight_decay=args.weight_decay)
# wrap up models
model, model_ema, optimizer = trainer.wrap_up(model, model_ema, optimizer)
# optional step: synchronize memory
trainer.broadcast_memory(contrast)
print(contrast.memory.shape)
# check and resume a model
start_epoch = trainer.resume_model(model, model_ema, contrast, optimizer)
# init tensorboard logger
trainer.init_tensorboard_logger()
for epoch in range(start_epoch, args.epochs + 1):
train_sampler.set_epoch(epoch)
trainer.adjust_learning_rate(optimizer, epoch)
outs = trainer.train(epoch, train_loader, model, model_ema,
contrast, criterion, optimizer)
# log to tensorbard
trainer.logging(epoch, outs, optimizer.param_groups[0]['lr'])
# save model
trainer.save(model, model_ema, contrast, optimizer, epoch)
if __name__ == '__main__':
main()
# python main_contrast.py \
# --method CMC \
# --cosine \
# --data_folder /path/to/data \
# --multiprocessing-distributed --world-size 1 --rank 0 \
# --method CMC --cosine --data_folder /path/to/data --multiprocessing-distributed --world-size 1 --rank 0