-
Notifications
You must be signed in to change notification settings - Fork 40
/
main_pretrain.py
217 lines (172 loc) · 7.26 KB
/
main_pretrain.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
import json
import os
import time
from shutil import copyfile
import torch
import torch.distributed as dist
from torch.backends import cudnn
from torch.nn.parallel import DistributedDataParallel
from torch.utils.data.distributed import DistributedSampler
from torch.utils.tensorboard import SummaryWriter
from contrast import models
from contrast import resnet
from contrast.data import get_loader
from contrast.logger import setup_logger
from contrast.lr_scheduler import get_scheduler
from contrast.option import parse_option
from contrast.util import AverageMeter
from contrast.lars import add_weight_decay, LARS
try:
# noinspection PyUnresolvedReferences
from apex import amp
except ImportError:
amp = None
def build_model(args):
encoder = resnet.__dict__[args.arch]
model = models.__dict__[args.model](encoder, args).cuda()
if args.optimizer == 'sgd':
optimizer = torch.optim.SGD(
model.parameters(),
lr=args.batch_size * dist.get_world_size() / 256 * args.base_learning_rate,
momentum=args.momentum,
weight_decay=args.weight_decay,)
elif args.optimizer == 'lars':
params = add_weight_decay(model, args.weight_decay)
optimizer = torch.optim.SGD(
params,
lr=args.batch_size * dist.get_world_size() / 256 * args.base_learning_rate,
momentum=args.momentum,)
optimizer = LARS(optimizer)
else:
raise NotImplementedError
if args.amp_opt_level != "O0":
model, optimizer = amp.initialize(model, optimizer, opt_level=args.amp_opt_level)
model = DistributedDataParallel(model, device_ids=[args.local_rank], broadcast_buffers=False)
return model, optimizer
def load_pretrained(model, pretrained_model):
ckpt = torch.load(pretrained_model, map_location='cpu')
state_dict = ckpt['model']
model_dict = model.state_dict()
model_dict.update(state_dict)
model.load_state_dict(model_dict)
logger.info(f"==> loaded checkpoint '{pretrained_model}' (epoch {ckpt['epoch']})")
def load_checkpoint(args, model, optimizer, scheduler, sampler=None):
logger.info(f"=> loading checkpoint '{args.resume}'")
checkpoint = torch.load(args.resume, map_location='cpu')
args.start_epoch = checkpoint['epoch'] + 1
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
if args.amp_opt_level != "O0" and checkpoint['opt'].amp_opt_level != "O0":
amp.load_state_dict(checkpoint['amp'])
logger.info(f"=> loaded successfully '{args.resume}' (epoch {checkpoint['epoch']})")
del checkpoint
torch.cuda.empty_cache()
def save_checkpoint(args, epoch, model, optimizer, scheduler, sampler=None):
logger.info('==> Saving...')
state = {
'opt': args,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'epoch': epoch,
}
if args.amp_opt_level != "O0":
state['amp'] = amp.state_dict()
file_name = os.path.join(args.output_dir, f'ckpt_epoch_{epoch}.pth')
torch.save(state, file_name)
copyfile(file_name, os.path.join(args.output_dir, 'current.pth'))
def main(args):
train_prefix = 'train'
train_loader = get_loader(
args.aug, args,
two_crop=args.model in ['PixPro'],
prefix=train_prefix,
return_coord=True,)
args.num_instances = len(train_loader.dataset)
logger.info(f"length of training dataset: {args.num_instances}")
model, optimizer = build_model(args)
scheduler = get_scheduler(optimizer, len(train_loader), args)
# optionally resume from a checkpoint
if args.pretrained_model:
assert os.path.isfile(args.pretrained_model)
load_pretrained(model, args.pretrained_model)
if args.auto_resume:
resume_file = os.path.join(args.output_dir, "current.pth")
if os.path.exists(resume_file):
logger.info(f'auto resume from {resume_file}')
args.resume = resume_file
else:
logger.info(f'no checkpoint found in {args.output_dir}, ignoring auto resume')
if args.resume:
assert os.path.isfile(args.resume)
load_checkpoint(args, model, optimizer, scheduler, sampler=train_loader.sampler)
# tensorboard
if dist.get_rank() == 0:
summary_writer = SummaryWriter(log_dir=args.output_dir)
else:
summary_writer = None
for epoch in range(args.start_epoch, args.epochs + 1):
if isinstance(train_loader.sampler, DistributedSampler):
train_loader.sampler.set_epoch(epoch)
train(epoch, train_loader, model, optimizer, scheduler, args, summary_writer)
if dist.get_rank() == 0 and (epoch % args.save_freq == 0 or epoch == args.epochs):
save_checkpoint(args, epoch, model, optimizer, scheduler, sampler=train_loader.sampler)
def train(epoch, train_loader, model, optimizer, scheduler, args, summary_writer):
"""
one epoch training
"""
model.train()
batch_time = AverageMeter()
loss_meter = AverageMeter()
end = time.time()
for idx, data in enumerate(train_loader):
data = [item.cuda(non_blocking=True) for item in data]
# In PixPro, data[0] -> im1, data[1] -> im2, data[2] -> coord1, data[3] -> coord2
loss = model(data[0], data[1], data[2], data[3])
# backward
optimizer.zero_grad()
if args.amp_opt_level != "O0":
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
scheduler.step()
# update meters and print info
loss_meter.update(loss.item(), data[0].size(0))
batch_time.update(time.time() - end)
end = time.time()
train_len = len(train_loader)
if idx % args.print_freq == 0:
lr = optimizer.param_groups[0]['lr']
logger.info(
f'Train: [{epoch}/{args.epochs}][{idx}/{train_len}] '
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f}) '
f'lr {lr:.3f} '
f'loss {loss_meter.val:.3f} ({loss_meter.avg:.3f})')
# tensorboard logger
if summary_writer is not None:
step = (epoch - 1) * len(train_loader) + idx
summary_writer.add_scalar('lr', lr, step)
summary_writer.add_scalar('loss', loss_meter.val, step)
if __name__ == '__main__':
opt = parse_option(stage='pre-train')
if opt.amp_opt_level != "O0":
assert amp is not None, "amp not installed!"
torch.cuda.set_device(opt.local_rank)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
cudnn.benchmark = True
# setup logger
os.makedirs(opt.output_dir, exist_ok=True)
logger = setup_logger(output=opt.output_dir, distributed_rank=dist.get_rank(), name="contrast")
if dist.get_rank() == 0:
path = os.path.join(opt.output_dir, "config.json")
with open(path, 'w') as f:
json.dump(vars(opt), f, indent=2)
logger.info("Full config saved to {}".format(path))
# print args
logger.info(
"\n".join("%s: %s" % (k, str(v)) for k, v in sorted(dict(vars(opt)).items()))
)
main(opt)