-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_helpers.py
29 lines (20 loc) · 878 Bytes
/
ml_helpers.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
from tqdm import tqdm
from rendering import rendering
import torch
def training(model, optimizer, scheduler, tn, tf, nb_bins, nb_epochs, data_loader, device='cpu'):
training_loss = []
for epoch in (range(nb_epochs)):
for batch in tqdm(data_loader):
o = batch[:, :3].to(device)
d = batch[:, 3:6].to(device)
target = batch[:, 6:].to(device)
prediction = rendering(model, o, d, tn, tf, nb_bins=nb_bins, device=device)
loss = ((prediction - target)**2).mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
training_loss.append(loss.item())
scheduler.step()
torch.save(model.cpu(), 'model_nerf')
model.to(device)
return training_loss