-
Notifications
You must be signed in to change notification settings - Fork 34
/
train.py
148 lines (123 loc) · 4.78 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
import os
import sys
import logging
import time
import shutil
import argparse
import numpy as np
import pandas as pd
from PIL import Image
from tqdm import tqdm
from collections import defaultdict
import torch
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
import dataloading as dl
import model as mdl
logger_py = logging.getLogger(__name__)
# Fix seeds
np.random.seed(42)
torch.manual_seed(42)
# Arguments
parser = argparse.ArgumentParser(
description='Training of UNISURF model'
)
parser.add_argument('config', type=str, help='Path to config file.')
parser.add_argument('--no-cuda', action='store_true', help='Do not use cuda.')
parser.add_argument('--exit-after', type=int, default=-1,
help='Checkpoint and exit after specified number of '
'seconds with exit code 2.')
args = parser.parse_args()
cfg = dl.load_config(args.config, 'configs/default.yaml')
is_cuda = (torch.cuda.is_available() and not args.no_cuda)
device = torch.device("cuda" if is_cuda else "cpu")
# params
out_dir = cfg['training']['out_dir']
backup_every = cfg['training']['backup_every']
exit_after = args.exit_after
batch_size = cfg['training']['batch_size']
n_workers = cfg['dataloading']['n_workers']
lr = cfg['training']['learning_rate']
# init dataloader
train_loader = dl.get_dataloader(cfg, mode='train')
test_loader = dl.get_dataloader(cfg, mode='test')
iter_test = iter(test_loader)
data_test = next(iter_test)
# init network
model_cfg = cfg['model']
model = mdl.NeuralNetwork(model_cfg)
print(model)
# init renderer
rendering_cfg = cfg['rendering']
renderer = mdl.Renderer(model, rendering_cfg, device=device)
# init optimizer
weight_decay = cfg['training']['weight_decay']
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
# init training
training_cfg = cfg['training']
trainer = mdl.Trainer(renderer, optimizer, training_cfg, device=device)
# init checkpoints and load
checkpoint_io = mdl.CheckpointIO(out_dir, model=model, optimizer=optimizer)
try:
load_dict = checkpoint_io.load('model.pt')
except FileExistsError:
load_dict = dict()
epoch_it = load_dict.get('epoch_it', -1)
it = load_dict.get('it', -1)
scheduler = optim.lr_scheduler.MultiStepLR(
optimizer, cfg['training']['scheduler_milestones'],
gamma=cfg['training']['scheduler_gamma'], last_epoch=epoch_it)
logger = SummaryWriter(os.path.join(out_dir, 'logs'))
# init training output
print_every = cfg['training']['print_every']
checkpoint_every = cfg['training']['checkpoint_every']
visualize_every = cfg['training']['visualize_every']
render_path = os.path.join(out_dir, 'rendering')
if visualize_every > 0:
visualize_skip = cfg['training']['visualize_skip']
visualize_path = os.path.join(out_dir, 'visualize')
if not os.path.exists(visualize_path):
os.makedirs(visualize_path)
# Print model
nparameters = sum(p.numel() for p in model.parameters())
logger_py.info(model)
logger_py.info('Total number of parameters: %d' % nparameters)
t0b = time.time()
while True:
epoch_it += 1
for batch in train_loader:
it += 1
loss_dict = trainer.train_step(batch, it)
loss = loss_dict['loss']
metric_val_best = loss
# Print output
if print_every > 0 and (it % print_every) == 0:
print('[Epoch %02d] it=%03d, loss=%.4f, time=%.4f'
% (epoch_it, it, loss, time.time() - t0b))
logger_py.info('[Epoch %02d] it=%03d, loss=%.4f, time=%.4f'
% (epoch_it, it, loss, time.time() - t0b))
t0b = time.time()
for l, num in loss_dict.items():
logger.add_scalar('train/'+l, num.detach().cpu(), it)
if visualize_every > 0 and (it % visualize_every)==0:
logger_py.info("Rendering")
out_render_path = os.path.join(render_path, '%04d_vis' % it)
if not os.path.exists(out_render_path):
os.makedirs(out_render_path)
val_rgb = trainer.render_visdata(
data_test,
cfg['training']['vis_resolution'],
it, out_render_path)
#logger.add_image('rgb', val_rgb, it)
# Save checkpoint
if (checkpoint_every > 0 and (it % checkpoint_every) == 0):
logger_py.info('Saving checkpoint')
print('Saving checkpoint')
checkpoint_io.save('model.pt', epoch_it=epoch_it, it=it,
loss_val_best=metric_val_best)
# Backup if necessary
if (backup_every > 0 and (it % backup_every) == 0):
logger_py.info('Backup checkpoint')
checkpoint_io.save('model_%d.pt' % it, epoch_it=epoch_it, it=it,
loss_val_best=metric_val_best)
scheduler.step()