-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
166 lines (134 loc) · 6.39 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
import sys
import os
import argparse
from tqdm import tqdm
import time
import random
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.utils import data
# import warnings
# warnings.filterwarnings("ignore")
from engine import Engine
from models import build_model
from datasets import build_dataset
from optimizer import build_optimizer, build_scheduler
from utils.pyt_utils import load_model, all_reduce_tensor, get_main_flag
from utils.logger import get_logger
from mmengine.config import Config
def get_parser():
parser = argparse.ArgumentParser(description="recognition")
parser.add_argument("--cfg", type=str, default=None)
parser.add_argument("--ckpt-name", type=str, default='ckpt')
parser.add_argument("--resume", type=str, default=None)
parser.add_argument("--start-iter", type=int, default=0)
parser.add_argument("--save-log", action="store_true")
return parser
def main():
parser = get_parser()
with Engine(custom_parser=parser) as engine:
main_flag = get_main_flag()
args = parser.parse_args()
cfg = Config.fromfile(args.cfg)
cfg.snapshot_dir = os.path.join(cfg.snapshot_dir, args.ckpt_name)
if not os.path.exists(cfg.snapshot_dir):
if main_flag:
os.makedirs(cfg.snapshot_dir)
time.sleep(1)
if args.save_log:
logger = get_logger(log_file=os.path.join(cfg.snapshot_dir, 'log.txt'))
else:
logger = get_logger()
if main_flag:
logger.info('Start Train')
logger.info('Running with argument: \n{}'.format(
'\n'.join('{}:{}'.format(k,v) for k,v in vars(args).items())))
logger.info('Running with config: \n{}'.format(cfg._text))
cudnn.benchmark = True
seed = cfg.random_seed
if engine.distributed:
seed = cfg.random_seed + engine.local_rank
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
# data loader
train_dataset = build_dataset(cfg.train_data_cfg)
train_loader, train_sampler = engine.get_train_loader(train_dataset, cfg.train_data_cfg)
val_dataset = build_dataset(cfg.val_data_cfg)
val_loader, val_sampler = engine.get_test_loader(val_dataset, cfg.val_data_cfg)
if main_flag:
logger.info('Total {} samples for training'.format(train_dataset.__len__()))
# model
model = build_model(cfg.model_cfg)
if args.resume:
load_model(model, args.resume,ignore_prefix='module.')
if main_flag:
logger.info('resume from {}'.format(args.resume))
optimizer = build_optimizer(cfg.optim_cfg, model)
scheduler = build_scheduler(cfg.optim_cfg, optimizer)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model = engine.data_parallel(model)
model.train()
run = True
global_iteration = args.start_iter
best_accuracy = -100
while run:
epoch = global_iteration // len(train_loader)
if engine.distributed:
train_sampler.set_epoch(epoch)
for idx, data in enumerate(train_loader):
global_iteration += 1
optimizer.zero_grad()
loss = model(data, global_iteration, 'train')
reduce_loss = all_reduce_tensor(loss)
loss.backward() #scaler.scale(loss).backward()
optimizer.step() #scaler.step(optimizer)
if global_iteration % cfg.log_interval == 0 and main_flag:
print_str = 'Epoch{}/Iters{}'.format(epoch, global_iteration) \
+ ' Iter{}/{}:'.format(idx + 1, len(train_loader)) \
+ ' lr=%.2e' % optimizer.param_groups[0]['lr']\
+ ' loss=%.2f' % reduce_loss.item()
logger.info(print_str)
# validation part
if (global_iteration % cfg.val_interval == 0 or global_iteration == 1 or global_iteration >= cfg.total_iter):
if cfg.val:
model.eval()
with torch.no_grad():
outputs = []
for val_iter, batch in enumerate(val_loader):
outputs.append(model(batch, val_iter, 'val'))
if engine.distributed:
val_acc, val_dict = model.module.validation_epoch_end(outputs)
else:
val_acc, val_dict = model.validation_epoch_end(outputs)
model.train()
if main_flag:
logger.info('*******************')
logger.info('Validation: ')
print_str = 'Epoch{}/Iters{}'.format(epoch, global_iteration) \
+ ' lr=%.2e' % optimizer.param_groups[0]['lr']
for k,v in val_dict.items():
print_str += ' %s=%.4f' % (k,v)
logger.info(print_str)
if val_acc > best_accuracy:
best_accuracy = val_acc
torch.save(model.state_dict(), os.path.join(cfg.snapshot_dir,'best_accuracy.pth'))
logger.info('best_acc=%.2f' % val_acc.item())
logger.info('*******************')
if main_flag:
logger.info('taking snapshot ...')
torch.save(model.state_dict(), os.path.join(cfg.snapshot_dir, 'latest.pth'))
if global_iteration >= cfg.start_save_iter:
torch.save(model.state_dict(), os.path.join(cfg.snapshot_dir, 'iter_'+str(global_iteration) + '.pth'))
if global_iteration >= cfg.total_iter:
run = False
break
if scheduler is not None:
scheduler.step(global_iteration)
if __name__ == '__main__':
main()