-
Notifications
You must be signed in to change notification settings - Fork 23
/
train.py
286 lines (236 loc) · 9.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
os.environ["OMP_NUM_THREADS"] = "1" # noqa
os.environ["MKL_NUM_THREADS"] = "1" # noqa
from utils.util import get_timestamp, make_source_code_snapshot
import torch
from collections import defaultdict
from torch.utils.data import DataLoader
from datasets import dataset_dict
from omegaconf import OmegaConf
# models
from models.nerf_model import ObjectNeRF
from models.embedding_helper import EmbeddingVoxel, Embedding
from models.rendering import render_rays
from models.code_library import CodeLibrary
# optimizer, scheduler, visualization
from utils import get_optimizer, get_scheduler, get_learning_rate
from utils.train_helper import visualize_val_image
# losses
from models.losses import get_loss
# metrics
from utils.metrics import psnr
# pytorch-lightning
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.loggers import TensorBoardLogger
class ObjectNeRFSystem(LightningModule):
def __init__(self, config):
super(ObjectNeRFSystem, self).__init__()
self.config = config
self.loss = get_loss(config)
self.use_voxel_embedding = self.config.model.get("use_voxel_embedding", True)
if self.use_voxel_embedding:
self.embedding_xyz = EmbeddingVoxel(
channels=config.model.N_scn_voxel_size + config.model.N_obj_voxel_size,
N_freqs=config.model.N_freq_voxel,
max_voxels=config.model.N_max_voxels,
dataset_extra_config=config.dataset_extra,
)
else:
self.embedding_xyz = Embedding(3, self.config.model.N_freq_xyz)
self.embedding_dir = Embedding(3, self.config.model.N_freq_dir)
self.embeddings = {"xyz": self.embedding_xyz, "dir": self.embedding_dir}
self.nerf_coarse = ObjectNeRF(self.config.model)
self.models = {"coarse": self.nerf_coarse}
if config.model.N_importance > 0:
self.nerf_fine = ObjectNeRF(self.config.model)
self.models["fine"] = self.nerf_fine
self.code_library = CodeLibrary(config.model)
self.models_to_train = [
self.models,
self.code_library,
self.embedding_xyz,
]
def forward(self, rays, extra=dict()):
"""Do batched inference on rays using chunk."""
B = rays.shape[0]
results = defaultdict(list)
for i in range(0, B, self.config.train.chunk):
extra_chunk = dict()
for k, v in extra.items():
if isinstance(v, torch.Tensor):
extra_chunk[k] = v[i : i + self.config.train.chunk]
else:
extra_chunk[k] = v
rendered_ray_chunks = render_rays(
models=self.models,
embeddings=self.embeddings,
rays=rays[i : i + self.config.train.chunk],
N_samples=self.config.model.N_samples,
use_disp=self.config.model.use_disp,
perturb=self.config.model.perturb,
noise_std=self.config.model.noise_std,
N_importance=self.config.model.N_importance,
chunk=self.config.train.chunk, # chunk size is effective in val mode
white_back=self.train_dataset.white_back
if self.training
else self.val_dataset.white_back,
**extra_chunk,
)
for k, v in rendered_ray_chunks.items():
results[k] += [v]
for k, v in results.items():
results[k] = torch.cat(v, 0)
return results
def setup(self, stage):
dataset = dataset_dict[self.config.dataset_name]
kwargs = {
"img_wh": tuple(self.config.img_wh),
}
kwargs["dataset_extra"] = self.config.dataset_extra
self.train_dataset = dataset(split="train", **kwargs)
self.val_dataset = dataset(split="val", **kwargs)
def configure_optimizers(self):
self.optimizer = get_optimizer(self.config.train, self.models_to_train)
scheduler = get_scheduler(self.config.train, self.optimizer)
return [self.optimizer], [scheduler]
def train_dataloader(self):
batch_size = self.config.train.batch_size
return DataLoader(
self.train_dataset,
shuffle=True,
num_workers=6,
batch_size=batch_size,
pin_memory=True,
)
def val_dataloader(self):
return DataLoader(
self.val_dataset,
shuffle=False,
num_workers=4,
batch_size=1, # validate one image (H*W rays) at a time
pin_memory=True,
)
def on_epoch_start(self):
if self.config.train.progressive_train and self.use_voxel_embedding:
if self.current_epoch > 2:
self.embedding_xyz.self_pruning_empty_voxels(self.models["fine"])
if self.current_epoch == 5:
self.embedding_xyz.voxel_subdivision()
def training_step(self, batch, batch_nb):
rays, rgbs = batch["rays"], batch["rgbs"]
rays = rays.squeeze() # (H*W, 3)
rgbs = rgbs.squeeze() # (H*W, 3)
# get mask for psnr evaluation
mask = batch["valid_mask"].view(-1, 1).repeat(1, 3) # (H*W, 3)
extra_info = dict()
extra_info["is_eval"] = False
# extra_info["instance_mask"] = batch["instance_mask"]
extra_info["pass_through_mask"] = batch["pass_through_mask"]
extra_info["rays_in_bbox"] = getattr(
self.train_dataset, "is_rays_in_bbox", lambda _: False
)()
extra_info["frustum_bound_th"] = (
self.config.model.frustum_bound
/ self.config["dataset_extra"]["scale_factor"]
)
extra_info.update(self.code_library(batch))
results = self(rays, extra_info)
loss_sum, loss_dict = self.loss(results, batch, self.current_epoch)
with torch.no_grad():
typ = "fine" if "rgb_fine" in results else "coarse"
psnr_ = psnr(results[f"rgb_{typ}"], rgbs, mask)
self.log("lr", get_learning_rate(self.optimizer))
self.log("train/loss", loss_sum)
for k, v in loss_dict.items():
self.log(f"train/{k}", v)
self.log("train/psnr", psnr_, prog_bar=True)
return loss_sum
def validation_step(self, batch, batch_nb):
rays, rgbs = batch["rays"], batch["rgbs"]
# get mask for psnr evaluation
if "instance_mask" in batch:
mask = (
(batch["valid_mask"] * batch["instance_mask"]).view(-1, 1).repeat(1, 3)
) # (H*W, 3)
else:
mask = None
rays = rays.squeeze() # (H*W, 3)
rgbs = rgbs.squeeze() # (H*W, 3)
extra_info = dict()
extra_info["is_eval"] = True
extra_info["rays_in_bbox"] = getattr(
self.val_dataset, "is_rays_in_bbox", lambda _: False
)()
extra_info["frustum_bound_th"] = (
self.config.model.frustum_bound
/ self.config["dataset_extra"]["scale_factor"]
)
extra_info.update(self.code_library(batch))
results = self(rays, extra_info)
loss_sum, loss_dict = self.loss(results, batch)
for k, v in loss_dict.items():
self.log(f"val/{k}", v)
log = {"val_loss": loss_sum}
log.update(loss_dict)
typ = "fine" if "rgb_fine" in results else "coarse"
if batch_nb == 0:
stack_image = visualize_val_image(
self.config.img_wh, batch, results, typ=typ
)
self.logger.experiment.add_images(
"val/GT_pred_depth", stack_image, self.global_step
)
psnr_ = psnr(results[f"rgb_{typ}"], rgbs, mask)
log["val_psnr"] = psnr_
return log
def validation_epoch_end(self, outputs):
mean_loss = torch.stack([x["val_loss"] for x in outputs]).mean()
mean_psnr = torch.stack([x["val_psnr"] for x in outputs]).mean()
self.log("val/loss", mean_loss)
self.log("val/psnr", mean_psnr, prog_bar=True)
def main(config):
exp_name = get_timestamp() + "_" + config.exp_name
print(f"Start with exp_name: {exp_name}.")
log_path = f"logs/{exp_name}"
config["log_path"] = log_path
system = ObjectNeRFSystem(config)
checkpoint_callback = ModelCheckpoint(
dirpath=log_path,
filename="{epoch:d}",
monitor="val/psnr",
mode="max",
# save_top_k=5,
save_top_k=-1,
save_last=True,
every_n_epochs=1,
save_on_train_epoch_end=True,
)
logger = TensorBoardLogger(save_dir="logs", name=exp_name)
trainer = Trainer(
max_epochs=config.train.num_epochs,
callbacks=[checkpoint_callback],
resume_from_checkpoint=config.ckpt_path,
logger=logger,
enable_model_summary=False,
gpus=config.train.num_gpus,
accelerator="ddp" if config.train.num_gpus > 1 else None,
num_sanity_val_steps=1,
benchmark=True,
profiler="simple" if config.train.num_gpus == 1 else None,
val_check_interval=0.25,
limit_train_batches=config.train.limit_train_batches,
)
make_source_code_snapshot(f"logs/{exp_name}")
OmegaConf.save(config=config, f=os.path.join(log_path, "run_config_snapshot.yaml"))
trainer.fit(system)
if __name__ == "__main__":
conf_cli = OmegaConf.from_cli()
conf_dataset = OmegaConf.load(conf_cli.dataset_config)
conf_default = OmegaConf.load("config/default_conf.yml")
# merge conf with the priority
conf_merged = OmegaConf.merge(conf_default, conf_dataset, conf_cli)
print("-" * 40)
print(OmegaConf.to_yaml(conf_merged))
print("-" * 40)
main(config=conf_merged)