forked from vLAR-group/DM-NeRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload_train_scannet.py
183 lines (154 loc) · 7.46 KB
/
reload_train_scannet.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
183
import os
import torch
import numpy as np
from networks.render import dm_nerf
from networks.tester import render_test, render_test_scannet
from config import initial, create_nerf
from networks.penalizer import ins_penalizer
from datasets.loader_scannet import load_data
from networks.helpers import get_select_crop, z_val_sample
from networks.evaluator import ins_criterion, img2mse, mse2psnr
import time
from tqdm.auto import tqdm
np.random.seed(0)
torch.cuda.manual_seed(4)
def train():
start_time = time.time()
model_fine.train()
model_coarse.train()
N_iters = 400000 + 1
# N_iters = 1 + 1
num_img = i_train.shape[0]
z_val_coarse = z_val_sample(args.N_train, args.near, args.far, args.N_samples)
for i in tqdm(range(iteration, N_iters)):
img_i = i % num_img
# img_i = np.random.choice(i_train)
gt_rgb = images[img_i].to(args.device)
pose = poses[img_i, :3, :4].to(args.device)
gt_label = gt_labels[img_i].to(args.device)
ins_index = ins_indices[img_i]
target_c, target_i, batch_rays, args.N_ins = get_select_crop(gt_rgb, pose, K, gt_label, ins_index, crop_mask,
args.N_train)
all_info = dm_nerf(batch_rays, position_embedder, view_embedder, model_coarse, model_fine, z_val_coarse, args)
# coarse losses
rgb_loss_coarse = img2mse(all_info['rgb_coarse'], target_c)
psnr_coarse = mse2psnr(rgb_loss_coarse)
ins_loss_coarse, valid_ce_coarse, invalid_ce_coarse, valid_siou_coarse = \
ins_criterion(all_info['ins_coarse'], target_i, args.ins_num)
# fine losses
rgb_loss_fine = img2mse(all_info['rgb_fine'], target_c)
psnr_fine = mse2psnr(rgb_loss_fine)
ins_loss_fine, valid_ce_fine, invalid_ce_fine, valid_siou_fine = \
ins_criterion(all_info['ins_fine'], target_i, args.ins_num)
# without penalize loss
ins_loss = ins_loss_fine + ins_loss_coarse
rgb_loss = rgb_loss_fine + rgb_loss_coarse
if img_i in i_train_label_sparse:
total_loss = ins_loss + rgb_loss
else:
total_loss = 0. + rgb_loss
# use penalize
if args.penalize:
emptiness_coarse = ins_penalizer(all_info['raw_coarse'], all_info['z_vals_coarse'],
all_info['depth_coarse'], batch_rays[1], args)
emptiness_fine = ins_penalizer(all_info['raw_fine'], all_info['z_vals_fine'],
all_info['depth_fine'], batch_rays[1], args)
emptiness_loss = emptiness_fine + emptiness_coarse
total_loss = total_loss + emptiness_loss
# optimizing
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
# losses decay
### update learning rate ###
decay_rate = 0.1
decay_steps = args.lrate_decay * 1000
new_lrate = args.lrate * (decay_rate ** ((i) / decay_steps))
for param_group in optimizer.param_groups:
param_group['lr'] = new_lrate
###################################
if i % args.i_print == 0:
print(f"[TRAIN] Iter: {i} PSNR: {psnr_fine.item()} Total_Loss: {total_loss.item()} RGB_Loss: {rgb_loss.item()} Ins_Loss: {ins_loss.item()}")
if i % args.i_save == 0:
path = os.path.join(args.basedir, args.expname, args.log_time, '{:06d}.tar'.format(i))
save_model = {
'iteration': i,
'network_coarse_state_dict': model_coarse.state_dict(),
'network_fine_state_dict': model_fine.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}
torch.save(save_model, path)
# if i % args.i_test == 0 and args.i_test != -1:
if i == 0:
# if i % args.i_test == 0:
model_coarse.eval()
model_fine.eval()
args.is_train = False
selected_indices = np.random.choice(len(i_test), size=[10], replace=False)
selected_i_test = i_test[selected_indices]
testsavedir = os.path.join(args.basedir, args.expname, args.log_time, 'testset_{:06d}'.format(i))
matched_file = os.path.join(testsavedir, 'matching_log.txt')
os.makedirs(testsavedir, exist_ok=True)
with torch.no_grad():
test_poses = torch.Tensor(poses[selected_i_test].to(args.device))
test_imgs = images[selected_i_test]
test_gt_labels = gt_labels[selected_i_test].to(args.device)
render_test(position_embedder, view_embedder, model_coarse, model_fine, test_poses, hwk, args,
gt_imgs=test_imgs, gt_labels=test_gt_labels, ins_rgbs=ins_rgbs, savedir=testsavedir,
matched_file=matched_file, crop_mask=crop_mask)
# render_test()
# render_test_scannet
print('Training model saved!')
args.is_train = True
model_coarse.train()
model_fine.train()
end_time = time.time()
print(f"training time : {end_time - start_time}")
if args.i_test == -1:
model_coarse.eval()
model_fine.eval()
args.is_train = False
# selected_indices = np.random.choice(len(i_test), size=[10], replace=False)
# selected_i_test = i_test[selected_indices]
selected_i_test = i_test
testsavedir = os.path.join(args.basedir, args.expname, args.log_time, 'testset_{:06d}'.format(i))
matched_file = os.path.join(testsavedir, 'matching_log.txt')
os.makedirs(testsavedir, exist_ok=True)
with torch.no_grad():
test_poses = torch.Tensor(poses[selected_i_test].to(args.device))
test_imgs = images[selected_i_test]
test_gt_labels = gt_labels[selected_i_test].to(args.device)
render_test_scannet(position_embedder, view_embedder, model_coarse, model_fine, test_poses, hwk, args,
gt_imgs=test_imgs, gt_labels=test_gt_labels, ins_rgbs=ins_rgbs, savedir=testsavedir,
matched_file=matched_file, crop_mask=crop_mask)
# render_test
print('Training model saved!')
args.is_train = True
model_coarse.train()
model_fine.train()
if __name__ == '__main__':
args = initial()
# load data
images, poses, hwk, i_split, gt_labels, ins_rgbs, args.ins_num, ins_indices, crop_mask = load_data(args)
print('Load data from', args.datadir)
i_train, i_test = i_split
H, W, K = hwk
# Create nerf model
position_embedder, view_embedder, model_coarse, model_fine, args = create_nerf(args)
# Create optimizer
grad_vars = list(model_coarse.parameters()) + list(model_fine.parameters())
optimizer = torch.optim.Adam(params=grad_vars, lr=args.lrate, betas=(0.9, 0.999))
# move data to gpu
images = torch.Tensor(images).cpu()
gt_labels = torch.Tensor(gt_labels).type(torch.int16).cpu()
poses = torch.Tensor(poses).cpu()
sparse_inv = args.label_sparse_inv
i_train_label_sparse = i_train[::sparse_inv]
print("sparse label num:",i_train_label_sparse.shape[0])
ckpt_path = os.path.join(args.basedir, args.expname, args.log_time, args.test_model)
ckpt = torch.load(ckpt_path)
iteration = ckpt['iteration']
# Load model
model_coarse.load_state_dict(ckpt['network_coarse_state_dict'])
model_fine.load_state_dict(ckpt['network_fine_state_dict'])
train()