-
Notifications
You must be signed in to change notification settings - Fork 7
/
Trainer.py
182 lines (129 loc) · 6.4 KB
/
Trainer.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
import json
from collections import defaultdict
from itertools import repeat
import numpy as np
import torch
from matplotlib import pyplot as plt
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.data.dataset import ConcatDataset
from tqdm import trange
from Globals import args, output_path, device, StatsMgr
from Mesher import handle_meshes
from Modules import OReXNet
class Trainer:
def __init__(self, csl):
self.csl = csl
self.module = OReXNet()
self.bce_loss = nn.BCEWithLogitsLoss()
self.data_loader = None
self.optimizer = torch.optim.Adam(self.module.parameters(), lr=args.lr, weight_decay=args.weight_decay)
self.lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=args.scheduler_gamma)
self.total_epochs = 0
self.train_losses = defaultdict(list)
def _get_loss_parts(self, xyzs, labels):
xyzs.requires_grad_(True)
pred_iterations = self._get_iterations_predictions(xyzs)
grad_xyzs = torch.autograd.grad(pred_iterations[-1].sum(), [xyzs], create_graph=True)[0]
loss_parts = {}
# bce_loss has a sigmoid layer build in
loss_parts['BCE'] = sum(map(self.bce_loss, pred_iterations, repeat(labels))) / len(pred_iterations)
if args.eikonal_hinge_lambda > 0:
norms = grad_xyzs.norm(dim=-1)
norm_loss = torch.maximum(norms - args.hinge_alpha, torch.zeros_like(norms)).mean()
loss_parts['eikonal_hinge'] = norm_loss * args.eikonal_hinge_lambda
return loss_parts
def _train_epoch(self):
running_loss = defaultdict(int)
for xyzs, labels in self.data_loader:
xyzs, labels = xyzs.to(device), labels.to(device)
loss_parts = self._get_loss_parts(xyzs, labels)
self.optimizer.zero_grad()
loss = sum(loss_parts.values())
loss.backward()
self.optimizer.step()
# update running loss
d = {}
for k in loss_parts.keys():
d[k] = running_loss[k] + loss_parts[k].item()
running_loss = d
if self.total_epochs > 0 and self.total_epochs % args.scheduler_step == 0:
self.lr_scheduler.step()
# log batch loss
for k, v in running_loss.items():
self.train_losses[k].append(v / len(self.data_loader.dataset))
def _get_iterations_predictions(self, xyzs):
return self.module(xyzs, args.n_iterations)
def _get_batch_predictions(self, xyzs):
return self._get_iterations_predictions(xyzs)[-1]
def _update_used_dataset(self, data_sets, refinement_level):
new_dataset = ConcatDataset(data_sets[0:refinement_level] if args.n_used_datasets is None \
else data_sets[-args.n_used_datasets:])
self.data_loader = DataLoader(new_dataset, batch_size=2 ** args.batch_size_exp, sampler=None,
shuffle=True, pin_memory=True, num_workers=4)
def _train_refinement_level(self, epochs, refinement_level):
self.module.train()
for _ in trange(epochs,
desc=f'Refinement level {refinement_level}/{len(args.epochs_batches) - 1} dataset={len(self.data_loader.dataset)}'):
self._train_epoch()
self.total_epochs += 1
def log_train_losses(self):
assert 'total' not in self.train_losses
# update train_losses with the total loss
self.train_losses['total'] = list(map(sum, zip(*self.train_losses.values())))
for k, v in self.train_losses.items():
if len(v) > 0:
plt.bar(range(len(v)), np.clip(v, 0, 2 * np.percentile(v, 95)))
plt.savefig(output_path + f"losses_{k}.png", dpi=500)
# plt.show()
plt.close()
plt.cla()
plt.clf()
for k, v in self.train_losses.items():
StatsMgr.setitem(f'last_loss_{k}', v[-1])
with open(output_path + 'losses.json', 'w') as f:
f.write(json.dumps(self.train_losses, default=lambda o: o.__dict__, indent=4))
@torch.no_grad()
def get_predictions(self, xyzs):
self.module.eval()
data_loader = DataLoader(xyzs, batch_size=2 ** args.batch_size_exp, shuffle=False,
num_workers=4, pin_memory=True)
label_pred = np.empty(0, dtype=float)
for xyzs_batch in data_loader:
xyzs_batch = xyzs_batch.to(device)
batch_labels = self._get_batch_predictions(xyzs_batch)
label_pred = np.concatenate((label_pred, batch_labels.detach().cpu().numpy().reshape(-1)))
return label_pred
def grad_wrt_input(self, xyzs):
self.module.eval()
data_loader = DataLoader(xyzs, batch_size=2 ** args.batch_size_exp, shuffle=False,
num_workers=4, pin_memory=True)
grads = np.empty((0, 3), dtype=float)
for xyzs_batch in data_loader:
xyzs_batch = xyzs_batch.to(device)
xyzs_batch.requires_grad_(True)
self.module.zero_grad()
pred = self._get_batch_predictions(xyzs_batch)
grads_batch = torch.autograd.grad(pred.mean(), [xyzs_batch])[0].detach().cpu().numpy()
grads = np.concatenate((grads, grads_batch))
return grads
def load_model(self, path):
self.module.load_state_dict(torch.load(path, map_location=torch.device('cpu')))
self.module.to(device)
self.module.eval()
def save_model(self, path):
torch.save(self.module.state_dict(), path)
def train_cycle(self, data_sets_promises):
data_sets = []
for refinement_level, epochs in enumerate(args.epochs_batches):
data_sets.append(data_sets_promises[refinement_level].get())
data_sets[-1].to_ply(f'{output_path}/datasets/gen{refinement_level}.ply')
self._update_used_dataset(data_sets, refinement_level)
with StatsMgr.timer('train', refinement_level):
self._train_refinement_level(epochs, refinement_level)
StatsMgr['dataset_size'][refinement_level] = len(data_sets[refinement_level])
self.save_model(output_path + f"/models/trained_model_{refinement_level}.pt")
try:
handle_meshes(self, refinement_level)
except Exception as exept:
print(exept)