forked from patienceFromZhou/simpleHand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
334 lines (259 loc) · 11.8 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
import argparse
import os
import subprocess
import time
from loguru import logger
import numpy as np
import torch
import torch.distributed as dist
import torch.functional as F
from torch.cuda.amp.grad_scaler import GradScaler
from torch import Tensor
from torch.utils.tensorboard import SummaryWriter
import random
import warnings
warnings.filterwarnings("ignore", category=ResourceWarning)
from hand_net import HandNet
from cfg import _CONFIG
from dataset import build_train_loader
from utils import GPUMemoryMonitor, get_log_model_dir
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
def get_learning_rate(epoch, step, base_lr, minibatch_per_epoch, warmup_epoch, stop_epoch):
final_lr = 0.0
warmup_iter = minibatch_per_epoch * warmup_epoch
warmup_lr_schedule = np.linspace(0, base_lr, warmup_iter)
decay_iter = minibatch_per_epoch * (stop_epoch - warmup_epoch)
if epoch < warmup_epoch:
cur_lr = warmup_lr_schedule[step + epoch*minibatch_per_epoch]
else:
if epoch < stop_epoch // 2:
return base_lr
return base_lr / 10
return cur_lr
class Trainer:
def __init__(self, cfg, args):
self.cfg = cfg
self.args = args
self.log_model_dir = get_log_model_dir(cfg['NAME'])
ngpus_per_node = torch.cuda.device_count()
global_world_size = 1 * ngpus_per_node
self.args.world_size = global_world_size
self.args.gpu = int(os.environ['LOCAL_RANK'])
self.args.local_rank = int(os.environ['LOCAL_RANK'])
self.args.rank = self.args.local_rank
self.rank = self.args.rank
self.local_rank = self.args.local_rank
def set_random_seed(self, seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def train(self):
self.before_train()
try:
self.train_in_epoch()
except Exception:
raise
finally:
self.after_train()
def after_train(self):
pass
def before_train(self):
logger.info(f"init group {self.rank}")
if self.rank == 0:
logger.add("./train_log/out.log", backtrace=True, diagnose=True)
self.set_random_seed(self.rank)
self.max_epoch = self.cfg["TRAIN"]["MAX_EPOCH"]
self.warmup_epoch = self.cfg["TRAIN"]["WARMUP_EPOCH"]
self.base_lr = self.cfg["TRAIN"]["BASE_LR"]
self.weight_decay = self.cfg["TRAIN"]["WEIGHT_DECAY"]
self.dump_epoch_interval = self.cfg["TRAIN"]["DUMP_EPOCH_INTERVAL"]
args = self.args
if args.local_rank != -1:
args.distributed = True
torch.cuda.set_device(args.local_rank)
args.dist_backend = 'nccl'
print('| distributed init world_size {}, (rank {}): , gpu {}'.format(
args.world_size, args.rank, args.local_rank), flush=True)
dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
dist.barrier()
if args.rank in [-1, 0]:
self.writer = SummaryWriter(os.path.join("./train_log", 'train'))
model = HandNet(self.cfg)
model.cuda(args.local_rank)
decay = []
no_decay = []
bn_wd_skip = True
for name, param in model.named_parameters():
if ('bn' in name or 'bias' in name) and bn_wd_skip:
no_decay.append(param)
else:
decay.append(param)
per_param_args = [{'params': decay},
{'params': no_decay, 'weight_decay': 0.0}]
self.optimizer = torch.optim.AdamW(per_param_args, lr=self.base_lr, weight_decay=self.weight_decay)
self.grad_scaler = GradScaler()
model = self.resume_train(model)
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
broadcast_buffers=True, find_unused_parameters=True)
self.train_loader = build_train_loader(self.cfg["TRAIN"]["DATALOADER"]["MINIBATCH_SIZE_PER_DIVICE"])
self.max_iter = self.cfg["TRAIN"]["DATALOADER"]["MINIBATCH_PER_EPOCH"]
self.gpu_monitor = GPUMemoryMonitor()
self.model = model
self.model.train()
if self.rank == 0:
logger.info("Training start...")
logger.info("\n{}".format(model))
def train_in_epoch(self):
for self.epoch in range(self.start_epoch, self.max_epoch):
self.before_epoch()
self.train_in_iter()
self.after_epoch()
def profile(self):
self.before_train()
self.before_epoch()
self.epoch = 0
log_path = "./train_log/log"
wait = 2
warmup = 3
active = 10
repeat = 2
schedule = torch.profiler.schedule(wait=wait, warmup=warmup, active=active, repeat=repeat)
tb_handler = torch.profiler.tensorboard_trace_handler(log_path)
with torch.profiler.profile(
schedule=schedule, on_trace_ready=tb_handler,
record_shapes=True, profile_memory=True, with_stack=True
) as prof:
for self.iter in range(self.max_iter):
if self.iter >= (wait + warmup + active) * repeat:
break
batch_data = next(self.train_loader)
self.train_one_iter(batch_data)
prof.step()
def before_epoch(self):
self.train_loader = build_train_loader(self.cfg["TRAIN"]["DATALOADER"]["MINIBATCH_SIZE_PER_DIVICE"])
self.model.train()
def after_epoch(self):
self.save_ckpt()
def train_in_iter(self):
for self.iter in range(self.max_iter):
batch_data = next(self.train_loader)
self.train_one_iter(batch_data)
def stat_mem(self):
mem_alloc = torch.cuda.memory_allocated(self.local_rank) / 1024**2
mem_reserved = torch.cuda.memory_reserved(self.local_rank) / 1024**2
mem_used = self.gpu_monitor.get_device_mem_info(self.local_rank).used / 1024**2
mem_info = f"rank:{self.local_rank}, Mem: ({mem_alloc:.2f}/{mem_reserved:.2f}/{mem_used:.2f}) MB"
print(mem_info)
def train_one_iter(self, batch_data):
iter_start_time = time.time()
lr = get_learning_rate(self.epoch, self.iter, self.base_lr, self.max_iter, self.warmup_epoch, self.max_epoch)
for param_group in self.optimizer.param_groups:
param_group["lr"] = lr
image = Tensor(batch_data['img']).cuda(self.local_rank).float()
del batch_data['img']
for k in batch_data:
batch_data[k] = Tensor(batch_data[k]).cuda(self.local_rank).float()
tdata = time.time() - iter_start_time
self.optimizer.zero_grad()
with torch.autocast(device_type='cuda', dtype=torch.float16):
losses = self.model(image, batch_data)
loss = losses['total_loss']
self.grad_scaler.scale(loss).backward()
if self.cfg['TRAIN'].get("CLIP_GRAD", None) is not None:
self.grad_scaler.unscale_(self.optimizer)
grad_norm = torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.cfg['TRAIN']['CLIP_GRAD']['MAX_NORM'])
else:
grad_norm = 0.0
self.grad_scaler.step(self.optimizer)
self.grad_scaler.update()
cur_time = time.time()
if self.rank == 0 and self.iter % 10 == 0:
ttrain = cur_time - iter_start_time
mb_per_second = 1 / (cur_time - iter_start_time)
result_str = f"rank {self.rank}/ {self.args.world_size}, e: {self.epoch}[{self.iter}/{self.max_iter}], {mb_per_second:.2f}mb/s,"
result_str += f"lr: {lr :6f}, grad_norm: {grad_norm.item() :.6f}, "
mem_alloc = torch.cuda.memory_allocated(self.local_rank) / 1024**2
mem_reserved = torch.cuda.memory_reserved(self.local_rank) / 1024**2
mem_used = self.gpu_monitor.get_device_mem_info(self.local_rank).used / 1024**2
mem_info = f"Mem: ({mem_alloc:.2f}/{mem_reserved:.2f}/{mem_used:.2f}) MB"
result_str += mem_info
for k,v in losses.items():
result_str +=f' {k} : {v.item():.5f}, '
if tdata/ttrain > .05:
result_str += f"dp/tot: {tdata/ttrain:.2g}"
cur_step = self.iter + self.max_iter * self.epoch
if self.rank == 0:
self.writer.add_scalar("lr", lr, cur_step)
for k, v in losses.items():
self.writer.add_scalar(f"losses/{k}", v.item(), cur_step)
logger.info(result_str)
dist.barrier()
def resume_train(self, model):
self.start_epoch = 0
if self.args.resume:
logger.info("resume training")
ckpt_file = os.path.join(self.log_model_dir, "latest")
if os.path.exists(ckpt_file):
loc = 'cuda:{}'.format(self.local_rank)
checkpoint = torch.load(open(ckpt_file, "rb"), map_location=loc)
model.load_state_dict(checkpoint["state_dict"])
self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
self.grad_scaler.load_state_dict(checkpoint["scaler_state_dict"])
start_epoch = checkpoint["epoch"]
self.start_epoch = start_epoch
logger.info(
"loaded checkpoint '{}' (epoch {})".format(
self.args.resume, self.start_epoch
)
)
return model
def save_ckpt(self):
if self.rank == 0:
logger.info("Dump model begin...")
model = self.model
model_to_save = model.module if hasattr(model, "module") else model
checkpoint = {
"epoch": self.epoch + 1,
"state_dict": model_to_save.state_dict(),
"optimizer_state_dict": self.optimizer.state_dict(),
"scaler_state_dict": self.grad_scaler.state_dict(),
}
if not os.path.exists(self.log_model_dir):
os.mkdir(self.log_model_dir)
latest_ckp_path = os.path.join(
self.log_model_dir, "latest")
with open(latest_ckp_path, 'wb') as fobj:
torch.save(checkpoint, fobj)
if (self.epoch + 1) % self.dump_epoch_interval == 0:
ckp_path = os.path.join(self.log_model_dir, f"epoch_{self.epoch + 1}")
with open(ckp_path, 'wb') as fobj:
torch.save(checkpoint, fobj)
logger.info("Dump model Done !")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--resume", action="store_true")
parser.add_argument('--dist_backend', default='nccl',
type=str, help='distributed backend')
parser.add_argument('--seed', default=0, type=int,
help='seed for initializing training. ')
parser.add_argument('--world_size', default=1, type=int)
parser.add_argument('--distributed', default=False, type=bool)
parser.add_argument('--dist_url', default='env://',
help='url used to set up distributed training')
args = parser.parse_args()
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
if args.world_size > 1:
torch.cuda.manual_seed_all(args.seed)
trainer = Trainer(_CONFIG, args)
trainer.train()
# trainer.profile()
if __name__ == "__main__":
main()