-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
191 lines (154 loc) · 7.54 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
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import argparse
import time
import pdb
import shutil
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
import datasets
from utils.metric import MultiClassMetric, AverageMeter
from models import *
import tqdm
import logging
import importlib
from utils.logger import config_logger
from utils import builder
# import torch.backends.cudnn as cudnn
# cudnn.deterministic = True
# cudnn.benchmark = False
def train_fp16(epoch, end_epoch, args, model, train_loader, optimizer, scheduler, logger, log_frequency):
scaler = torch.cuda.amp.GradScaler()
rank = torch.distributed.get_rank()
model.train()
losses = AverageMeter()
print('FP16 Train mode!')
for i, (pcds_xyzi, pcds_coord, pcds_sphere_coord, pcds_target) in tqdm.tqdm(enumerate(train_loader)):
# pdb.set_trace()
with torch.cuda.amp.autocast():
loss = model(pcds_xyzi, pcds_coord, pcds_sphere_coord, pcds_target)
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
scheduler.step()
torch.distributed.reduce(loss, 0)
if rank == 0:
losses.update(loss.item() / torch.distributed.get_world_size())
if i % log_frequency == 0 or i == len(train_loader) - 1:
string = 'Epoch: [{}]/[{}]; Iteration: [{}]/[{}]; lr: {}'.format(epoch, end_epoch, \
i, len(train_loader),
optimizer.state_dict()['param_groups'][
0]['lr'])
string = string + '; loss: {:.6f} / {:.6f}'.format(losses.val, losses.avg)
logger.info(string)
def train(epoch, end_epoch, args, model, train_loader, optimizer, scheduler, logger, log_frequency):
rank = torch.distributed.get_rank()
model.train()
losses = AverageMeter()
for i, (pcds_xyzi, pcds_coord, pcds_sphere_coord, pcds_target) in tqdm.tqdm(enumerate(train_loader)):
# pdb.set_trace()
loss = model(pcds_xyzi, pcds_coord, pcds_sphere_coord, pcds_target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
torch.distributed.reduce(loss, 0)
if rank == 0:
losses.update(loss.item() / torch.distributed.get_world_size())
if i % log_frequency == 0 or i == len(train_loader) - 1:
string = 'Epoch: [{}]/[{}]; Iteration: [{}]/[{}]; lr: {}'.format(epoch, end_epoch, \
i, len(train_loader),
optimizer.state_dict()['param_groups'][
0]['lr'])
string = string + '; loss: {:.6f} / {:.6f}'.format(losses.val, losses.avg)
logger.info(string)
def main(args, config):
# parsing cfg
pGen, pDataset, pModel, pOpt = config.get_config()
prefix = pGen.name
save_path = os.path.join("experiments", prefix)
model_prefix = os.path.join(save_path, "checkpoint")
os.system('mkdir -p {}'.format(model_prefix))
# start logging
shutil.copyfile('config' + '/' + prefix + ".py", save_path + '/' + prefix + ".py")
config_logger(os.path.join(save_path, "log.txt"))
logger = logging.getLogger()
# reset dist
device = torch.device('cuda:{}'.format(args.local_rank))
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
world_size = torch.distributed.get_world_size()
rank = torch.distributed.get_rank()
# reset random seed
seed = rank * pDataset.Train.num_workers + 50051
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# define dataloader
train_dataset = eval('datasets.{}.DataloadTrain'.format(pDataset.Train.data_src))(pDataset.Train)
train_sampler = DistributedSampler(train_dataset)
train_loader = DataLoader(train_dataset,
batch_size=pGen.batch_size_per_gpu,
shuffle=(train_sampler is None),
num_workers=pDataset.Train.num_workers,
sampler=train_sampler,
pin_memory=True)
print("rank: {}/{}; batch_size: {}".format(rank, world_size, pGen.batch_size_per_gpu))
# define model
base_net = eval(pModel.prefix)(pModel)
base_net = nn.SyncBatchNorm.convert_sync_batchnorm(base_net)
model = torch.nn.parallel.DistributedDataParallel(base_net.to(device),
device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)
# define optimizer
optimizer = builder.get_optimizer(pOpt, model)
# define scheduler
per_epoch_num_iters = len(train_loader)
scheduler = builder.get_scheduler(optimizer, pOpt, per_epoch_num_iters)
if rank == 0:
logger.info(model)
logger.info(optimizer)
logger.info(scheduler)
# load pretrain model
pretrain_model = os.path.join(model_prefix, '{}-model.pth'.format(pModel.pretrain.pretrain_epoch))
if os.path.exists(pretrain_model):
checkpoint = torch.load(pretrain_model, map_location='cpu')
model.module.load_state_dict(checkpoint['model_state_dict'], strict=True)
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
pOpt.schedule.begin_epoch = checkpoint['epoch'] + 1
# base_net.load_state_dict(torch.load(pretrain_model, map_location='cpu'))
logger.info("Load model from {}".format(pretrain_model))
# start training
for epoch in range(pOpt.schedule.begin_epoch, pOpt.schedule.end_epoch):
train_sampler.set_epoch(epoch)
if pGen.fp16:
train_fp16(epoch, pOpt.schedule.end_epoch, args, model, train_loader, optimizer, scheduler, logger,
pGen.log_frequency)
else:
train(epoch, pOpt.schedule.end_epoch, args, model, train_loader, optimizer, scheduler, logger,
pGen.log_frequency)
# save model
if rank == 0:
save_dict = {
'epoch': epoch, # after training one epoch, the start_epoch should be epoch+1
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'model_state_dict': model.module.state_dict()
}
torch.save(save_dict, os.path.join(model_prefix, '{}-model.pth'.format(epoch)))
# torch.save(model.module.state_dict(), os.path.join(model_prefix, '{}-model.pth'.format(epoch)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='lidar segmentation')
parser.add_argument('--config', help='config file path', default='config/wce.py', type=str)
parser.add_argument('--local_rank', type=int, default=0)
args = parser.parse_args()
config = importlib.import_module(args.config.replace('.py', '').replace('/', '.'))
main(args, config)