-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathvolume_rendering_main.py
More file actions
379 lines (305 loc) · 10.4 KB
/
volume_rendering_main.py
File metadata and controls
379 lines (305 loc) · 10.4 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import os
import warnings
import hydra
import numpy as np
import torch
import tqdm
import imageio
from omegaconf import DictConfig
from PIL import Image
from pytorch3d.renderer import (
PerspectiveCameras,
look_at_view_transform
)
import matplotlib.pyplot as plt
from implicit import implicit_dict
from sampler import sampler_dict
from renderer import renderer_dict
from ray_utils import (
sample_images_at_xy,
get_pixels_from_image,
get_random_pixels_from_image,
get_rays_from_pixels
)
from data_utils import (
dataset_from_config,
create_surround_cameras,
vis_grid,
vis_rays,
)
from dataset import (
get_nerf_datasets,
trivial_collate,
)
# Model class containing:
# 1) Implicit volume defining the scene
# 2) Sampling scheme which generates sample points along rays
# 3) Renderer which can render an implicit volume given a sampling scheme
class Model(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
# Get implicit function from config
self.implicit_fn = implicit_dict[cfg.implicit_function.type](
cfg.implicit_function
)
# Point sampling (raymarching) scheme
self.sampler = sampler_dict[cfg.sampler.type](
cfg.sampler
)
# Initialize volume renderer
self.renderer = renderer_dict[cfg.renderer.type](
cfg.renderer
)
def forward(
self,
ray_bundle
):
# Call renderer with
# a) Implicit volume
# b) Sampling routine
return self.renderer(
self.sampler,
self.implicit_fn,
ray_bundle
)
def render_images(
model,
cameras,
image_size,
save=False,
file_prefix=''
):
all_images = []
device = list(model.parameters())[0].device
for cam_idx, camera in enumerate(cameras):
print(f'Rendering image {cam_idx}')
torch.cuda.empty_cache()
camera = camera.to(device)
xy_grid = get_pixels_from_image(image_size, camera) # TODO (Q1.3): implement in ray_utils.py
ray_bundle = get_rays_from_pixels(xy_grid, image_size, camera) # TODO (Q1.3): implement in ray_utils.py
# TODO (Q1.3): Visualize xy grid using vis_grid
if cam_idx == 0 and file_prefix == '':
pass
# TODO (Q1.3): Visualize rays using vis_rays
if cam_idx == 0 and file_prefix == '':
pass
# TODO (Q1.4): Implement point sampling along rays in sampler.py
pass
# TODO (Q1.4): Visualize sample points as point cloud
if cam_idx == 0 and file_prefix == '':
pass
# TODO (Q1.5): Implement rendering in renderer.py
out = model(ray_bundle)
# Return rendered features (colors)
image = np.array(
out['feature'].view(
image_size[1], image_size[0], 3
).detach().cpu()
)
all_images.append(image)
# TODO (Q1.5): Visualize depth
if cam_idx == 2 and file_prefix == '':
pass
# Save
if save:
plt.imsave(
f'{file_prefix}_{cam_idx}.png',
image
)
return all_images
def render(
cfg,
):
# Create model
model = Model(cfg)
model = model.cuda(); model.eval()
# Render spiral
cameras = create_surround_cameras(3.0, n_poses=20)
all_images = render_images(
model, cameras, cfg.data.image_size
)
imageio.mimsave('images/part_1.gif', [np.uint8(im * 255) for im in all_images], loop=0)
def train(
cfg
):
# Create model
model = Model(cfg)
model = model.cuda(); model.train()
# Create dataset
train_dataset = dataset_from_config(cfg.data)
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=1,
shuffle=True,
num_workers=0,
collate_fn=lambda batch: batch,
)
image_size = cfg.data.image_size
# Create optimizer
optimizer = torch.optim.Adam(
model.parameters(),
lr=cfg.training.lr
)
# Render images before training
cameras = [item['camera'] for item in train_dataset]
render_images(
model, cameras, image_size,
save=True, file_prefix='images/part_2_before_training'
)
# Train
t_range = tqdm.tqdm(range(cfg.training.num_epochs))
for epoch in t_range:
for iteration, batch in enumerate(train_dataloader):
image, camera, camera_idx = batch[0].values()
image = image.cuda()
camera = camera.cuda()
# Sample rays
xy_grid = get_random_pixels_from_image(cfg.training.batch_size, image_size, camera) # TODO (Q2.1): implement in ray_utils.py
ray_bundle = get_rays_from_pixels(xy_grid, image_size, camera)
rgb_gt = sample_images_at_xy(image, xy_grid)
# Run model forward
out = model(ray_bundle)
# TODO (Q2.2): Calculate loss
loss = None
# Backprop
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch % 10) == 0:
t_range.set_description(f'Epoch: {epoch:04d}, Loss: {loss:.06f}')
t_range.refresh()
# Print center and side lengths
print("Box center:", tuple(np.array(model.implicit_fn.sdf.center.data.detach().cpu()).tolist()[0]))
print("Box side lengths:", tuple(np.array(model.implicit_fn.sdf.side_lengths.data.detach().cpu()).tolist()[0]))
# Render images after training
render_images(
model, cameras, image_size,
save=True, file_prefix='images/part_2_after_training'
)
all_images = render_images(
model, create_surround_cameras(3.0, n_poses=20), image_size, file_prefix='part_2'
)
imageio.mimsave('images/part_2.gif', [np.uint8(im * 255) for im in all_images], loop=0)
def create_model(cfg):
# Create model
model = Model(cfg)
model.cuda(); model.train()
# Load checkpoints
optimizer_state_dict = None
start_epoch = 0
checkpoint_path = os.path.join(
hydra.utils.get_original_cwd(),
cfg.training.checkpoint_path
)
if len(cfg.training.checkpoint_path) > 0:
# Make the root of the experiment directory.
checkpoint_dir = os.path.split(checkpoint_path)[0]
os.makedirs(checkpoint_dir, exist_ok=True)
# Resume training if requested.
if cfg.training.resume and os.path.isfile(checkpoint_path):
print(f"Resuming from checkpoint {checkpoint_path}.")
loaded_data = torch.load(checkpoint_path)
model.load_state_dict(loaded_data["model"])
start_epoch = loaded_data["epoch"]
print(f" => resuming from epoch {start_epoch}.")
optimizer_state_dict = loaded_data["optimizer"]
# Initialize the optimizer.
optimizer = torch.optim.Adam(
model.parameters(),
lr=cfg.training.lr,
)
# Load the optimizer state dict in case we are resuming.
if optimizer_state_dict is not None:
optimizer.load_state_dict(optimizer_state_dict)
optimizer.last_epoch = start_epoch
# The learning rate scheduling is implemented with LambdaLR PyTorch scheduler.
def lr_lambda(epoch):
return cfg.training.lr_scheduler_gamma ** (
epoch / cfg.training.lr_scheduler_step_size
)
lr_scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer, lr_lambda, last_epoch=start_epoch - 1, verbose=False
)
return model, optimizer, lr_scheduler, start_epoch, checkpoint_path
def train_nerf(
cfg
):
# Create model
model, optimizer, lr_scheduler, start_epoch, checkpoint_path = create_model(cfg)
# Load the training/validation data.
train_dataset, val_dataset, _ = get_nerf_datasets(
dataset_name=cfg.data.dataset_name,
image_size=[cfg.data.image_size[1], cfg.data.image_size[0]],
)
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=1,
shuffle=True,
num_workers=0,
collate_fn=trivial_collate,
)
# Run the main training loop.
for epoch in range(start_epoch, cfg.training.num_epochs):
t_range = tqdm.tqdm(enumerate(train_dataloader))
for iteration, batch in t_range:
image, camera, camera_idx = batch[0].values()
image = image.cuda().unsqueeze(0)
camera = camera.cuda()
# Sample rays
xy_grid = get_random_pixels_from_image(
cfg.training.batch_size, cfg.data.image_size, camera
)
ray_bundle = get_rays_from_pixels(
xy_grid, cfg.data.image_size, camera
)
rgb_gt = sample_images_at_xy(image, xy_grid)
# Run model forward
out = model(ray_bundle)
# TODO (Q3.1): Calculate loss
loss = None
# Take the training step.
optimizer.zero_grad()
loss.backward()
optimizer.step()
t_range.set_description(f'Epoch: {epoch:04d}, Loss: {loss:.06f}')
t_range.refresh()
# Adjust the learning rate.
lr_scheduler.step()
# Checkpoint.
if (
epoch % cfg.training.checkpoint_interval == 0
and len(cfg.training.checkpoint_path) > 0
and epoch > 0
):
print(f"Storing checkpoint {checkpoint_path}.")
data_to_store = {
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"epoch": epoch,
}
torch.save(data_to_store, checkpoint_path)
# Render
if (
epoch % cfg.training.render_interval == 0
and epoch > 0
):
with torch.no_grad():
test_images = render_images(
model, create_surround_cameras(4.0, n_poses=20, up=(0.0, 0.0, 1.0), focal_length=2.0),
cfg.data.image_size, file_prefix='nerf'
)
imageio.mimsave('images/part_3.gif', [np.uint8(im * 255) for im in test_images], loop=0)
@hydra.main(config_path='./configs', config_name='sphere')
def main(cfg: DictConfig):
os.chdir(hydra.utils.get_original_cwd())
if cfg.type == 'render':
render(cfg)
elif cfg.type == 'train':
train(cfg)
elif cfg.type == 'train_nerf':
train_nerf(cfg)
if __name__ == "__main__":
main()