-
Notifications
You must be signed in to change notification settings - Fork 79
/
train.py
182 lines (140 loc) · 6.15 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
'''
PointGroup train.py
Written by Li Jiang
'''
import torch
import torch.optim as optim
import time, sys, os, random
from tensorboardX import SummaryWriter
import numpy as np
from util.config import cfg
from util.log import logger
import util.utils as utils
def init():
# copy important files to backup
backup_dir = os.path.join(cfg.exp_path, 'backup_files')
os.makedirs(backup_dir, exist_ok=True)
os.system('cp train.py {}'.format(backup_dir))
os.system('cp {} {}'.format(cfg.model_dir, backup_dir))
os.system('cp {} {}'.format(cfg.dataset_dir, backup_dir))
os.system('cp {} {}'.format(cfg.config, backup_dir))
# log the config
logger.info(cfg)
# summary writer
global writer
writer = SummaryWriter(cfg.exp_path)
# random seed
random.seed(cfg.manual_seed)
np.random.seed(cfg.manual_seed)
torch.manual_seed(cfg.manual_seed)
torch.cuda.manual_seed_all(cfg.manual_seed)
def train_epoch(train_loader, model, model_fn, optimizer, epoch):
iter_time = utils.AverageMeter()
data_time = utils.AverageMeter()
am_dict = {}
model.train()
start_epoch = time.time()
end = time.time()
for i, batch in enumerate(train_loader):
data_time.update(time.time() - end)
torch.cuda.empty_cache()
##### adjust learning rate
utils.step_learning_rate(optimizer, cfg.lr, epoch - 1, cfg.step_epoch, cfg.multiplier)
##### prepare input and forward
loss, _, visual_dict, meter_dict = model_fn(batch, model, epoch)
##### meter_dict
for k, v in meter_dict.items():
if k not in am_dict.keys():
am_dict[k] = utils.AverageMeter()
am_dict[k].update(v[0], v[1])
##### backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
##### time and print
current_iter = (epoch - 1) * len(train_loader) + i + 1
max_iter = cfg.epochs * len(train_loader)
remain_iter = max_iter - current_iter
iter_time.update(time.time() - end)
end = time.time()
remain_time = remain_iter * iter_time.avg
t_m, t_s = divmod(remain_time, 60)
t_h, t_m = divmod(t_m, 60)
remain_time = '{:02d}:{:02d}:{:02d}'.format(int(t_h), int(t_m), int(t_s))
sys.stdout.write(
"epoch: {}/{} iter: {}/{} loss: {:.4f}({:.4f}) data_time: {:.2f}({:.2f}) iter_time: {:.2f}({:.2f}) remain_time: {remain_time}\n".format
(epoch, cfg.epochs, i + 1, len(train_loader), am_dict['loss'].val, am_dict['loss'].avg,
data_time.val, data_time.avg, iter_time.val, iter_time.avg, remain_time=remain_time))
if (i == len(train_loader) - 1): print()
logger.info("epoch: {}/{}, train loss: {:.4f}, time: {}s".format(epoch, cfg.epochs, am_dict['loss'].avg, time.time() - start_epoch))
utils.checkpoint_save(model, cfg.exp_path, cfg.config.split('/')[-1][:-5], epoch, cfg.save_freq, use_cuda)
for k in am_dict.keys():
if k in visual_dict.keys():
writer.add_scalar(k+'_train', am_dict[k].avg, epoch)
def eval_epoch(val_loader, model, model_fn, epoch):
logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
am_dict = {}
with torch.no_grad():
model.eval()
start_epoch = time.time()
for i, batch in enumerate(val_loader):
##### prepare input and forward
loss, preds, visual_dict, meter_dict = model_fn(batch, model, epoch)
##### meter_dict
for k, v in meter_dict.items():
if k not in am_dict.keys():
am_dict[k] = utils.AverageMeter()
am_dict[k].update(v[0], v[1])
##### print
sys.stdout.write("\riter: {}/{} loss: {:.4f}({:.4f})".format(i + 1, len(val_loader), am_dict['loss'].val, am_dict['loss'].avg))
if (i == len(val_loader) - 1): print()
logger.info("epoch: {}/{}, val loss: {:.4f}, time: {}s".format(epoch, cfg.epochs, am_dict['loss'].avg, time.time() - start_epoch))
for k in am_dict.keys():
if k in visual_dict.keys():
writer.add_scalar(k + '_eval', am_dict[k].avg, epoch)
if __name__ == '__main__':
##### init
init()
##### get model version and data version
exp_name = cfg.config.split('/')[-1][:-5]
model_name = exp_name.split('_')[0]
data_name = exp_name.split('_')[-1]
##### model
logger.info('=> creating model ...')
if model_name == 'pointgroup':
from model.pointgroup.pointgroup import PointGroup as Network
from model.pointgroup.pointgroup import model_fn_decorator
else:
print("Error: no model - " + model_name)
exit(0)
model = Network(cfg)
use_cuda = torch.cuda.is_available()
logger.info('cuda available: {}'.format(use_cuda))
assert use_cuda
model = model.cuda()
# logger.info(model)
logger.info('#classifier parameters: {}'.format(sum([x.nelement() for x in model.parameters()])))
##### optimizer
if cfg.optim == 'Adam':
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=cfg.lr)
elif cfg.optim == 'SGD':
optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=cfg.lr, momentum=cfg.momentum, weight_decay=cfg.weight_decay)
##### model_fn (criterion)
model_fn = model_fn_decorator()
##### dataset
if cfg.dataset == 'scannetv2':
if data_name == 'scannet':
import data.scannetv2_inst
dataset = data.scannetv2_inst.Dataset()
dataset.trainLoader()
dataset.valLoader()
else:
print("Error: no data loader - " + data_name)
exit(0)
##### resume
start_epoch = utils.checkpoint_restore(model, cfg.exp_path, cfg.config.split('/')[-1][:-5], use_cuda) # resume from the latest epoch, or specify the epoch to restore
##### train and val
for epoch in range(start_epoch, cfg.epochs + 1):
train_epoch(dataset.train_data_loader, model, model_fn, optimizer, epoch)
if utils.is_multiple(epoch, cfg.save_freq) or utils.is_power2(epoch):
eval_epoch(dataset.val_data_loader, model, model_fn, epoch)