-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path4_caustics_design.py
392 lines (328 loc) · 16.1 KB
/
4_caustics_design.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import copy
import drjit
import imageio
import math
import matplotlib.pyplot as plt
import mitsuba as mi
from mitsuba.scalar_rgb import Transform4f as T
import numpy as np
from pathlib import Path
from umbra import MeshViewer
import torch
from tqdm import tqdm
from typing import List
from NURBSDiff.surf_eval import SurfEval
from drpg import generate_grid_faces, save_mesh, compute_lsig_matrix, create_planar_grid, write_quad_mesh, ParametricPatches, SurfaceTessellator, SurfaceType
# This code is heavily inspired by the Mitsuba 3 caustics optimization tutorial:
# (https://github.com/mitsuba-renderer/mitsuba-tutorials/blob/master/inverse_rendering/caustics_optimization.ipynb)
def load_ref_image(path, resolution, output_dir):
b = mi.Bitmap(str(path))
b = b.convert(mi.Bitmap.PixelFormat.RGB, mi.Bitmap.Float32, False)
if b.size() != resolution:
b = b.resample(resolution)
mi.util.write_bitmap(str(output_dir / 'out_ref.exr'), b)
print('[i] Loaded reference image from:', path)
return mi.TensorXf(b).torch()
def create_emitter(emitter_type):
if emitter_type == 'gray':
return {
'type':'directionalarea',
'radiance': {
'type': 'spectrum',
'value': 0.8
},
}
elif emitter_type == 'bayer':
bayer = drjit.zeros(mi.TensorXf, (32, 32, 3))
bayer[ ::2, ::2, 2] = 2.2
bayer[ ::2, 1::2, 1] = 2.2
bayer[1::2, 1::2, 0] = 2.2
return {
'type':'directionalarea',
'radiance': {
'type': 'bitmap',
'bitmap': mi.Bitmap(bayer),
'raw': True,
'filter_type': 'nearest'
},
}
def create_integrator():
return {
'type': 'ptracer',
'samples_per_pass': 256,
'max_depth': 4,
'hide_emitters': False,
}
def create_sensor(resolution):
# Looking at the receiving plane, not looking through the lens
sensor_to_world = mi.ScalarTransform4f.look_at(
target=[0, -20, 0],
origin=[0, -4.65, 0],
up=[0, 0, 1]
)
return {
'type': 'perspective',
'near_clip': 1,
'far_clip': 1000,
'fov': 45,
'to_world': sensor_to_world,
'sampler': {
'type': 'independent',
'sample_count': 512 # Not really used
},
'film': {
'type': 'hdrfilm',
'width': resolution[0],
'height': resolution[1],
'pixel_format': 'rgb',
'rfilter': {
# Important: smooth reconstruction filter with a footprint larger than 1 pixel.
'type': 'gaussian'
}
},
}
def create_scene(integrator, sensor, emitter, lens_filename):
return {
'type': 'scene',
'sensor': sensor,
'integrator': integrator,
# Glass BSDF
'simple-glass': {
'type': 'dielectric',
'id': 'simple-glass-bsdf',
'ext_ior': 'air',
'int_ior': 1.5,
'specular_reflectance': { 'type': 'spectrum', 'value': 0 },
},
'white-bsdf': {
'type': 'diffuse',
'id': 'white-bsdf',
'reflectance': { 'type': 'rgb', 'value': (1, 1, 1) },
},
'black-bsdf': {
'type': 'diffuse',
'id': 'black-bsdf',
'reflectance': { 'type': 'spectrum', 'value': 0 },
},
# Receiving plane
'receiving-plane': {
'type': 'obj',
'id': 'receiving-plane',
'filename': 'rectangle.obj',
'to_world': \
mi.ScalarTransform4f.look_at(
target=[0, 1, 0],
origin=[0, -7, 0],
up=[0, 0, 1]
).scale((5, 5, 5)),
'bsdf': {'type': 'ref', 'id': 'white-bsdf'},
},
# Glass slab, excluding the 'exit' face (added separately below)
'slab': {
'type': 'obj',
'id': 'slab',
'filename': 'slab.obj',
'to_world': mi.ScalarTransform4f.rotate(axis=(1, 0, 0), angle=90),
'bsdf': {'type': 'ref', 'id': 'simple-glass'},
},
# Glass rectangle, to be optimized
'lens': {
'type': 'obj' if 'obj' in lens_filename else 'ply',
'filename': lens_filename,
'id': 'lens',
'bsdf': {'type': 'ref', 'id': 'simple-glass'},
},
# Directional area emitter placed behind the glass slab
'focused-emitter-shape': {
'type': 'obj',
'filename': 'rectangle.obj',
'to_world': mi.ScalarTransform4f.look_at(
target=[0, 0, 0],
origin=[0, 5, 0],
up=[0, 0, 1]
),
'bsdf': {'type': 'ref', 'id': 'black-bsdf'},
'focused-emitter': emitter,
},
}
def scale_independent_loss(img_opt, img_ref):
"""Brightness-independent L2 loss function."""
img_opt = img_opt / torch.mean(img_opt.detach())
img_ref = img_ref / torch.mean(img_ref)
return torch.mean((img_opt - img_ref)**2)
def run_caustics_design(image_path: Path, output_dir: Path, num_iterations=500, degree=4, n=20, m=20, render_resolution=(128, 128), uv_resolution=(128, 128), emitter_type='gray', refinement_iterations: List[int] = [0, 100, 400, 2000], large_steps_lambdas: List[float] = [5., 5., 0., 0.], step_sizes: List[float] = [0.1, 0.01, 0.001, 0.0005], output_interval: int = 50, save_intermediate_lenses: bool = False, display_plot: bool = False, viewer: MeshViewer=None, data_dir: Path = Path("./data")):
device = torch.device('cuda:0')
# Create a NURBS control mesh for the side of the lens that is optimized
patches = ParametricPatches(n=n, m=m, device=device)
patches.add(create_planar_grid(n=n, m=m, position=[0, 0, 0], normal=[0, -1, 0], tangent=[1, 0, 0], scale=1, device=device))
# Create the initial tessellation
tessellator = SurfaceTessellator(uv_resolution, SurfaceType.NURBS)
with torch.no_grad():
v, f, _, _ = tessellator.tessellate(patches, degree=degree)
f = f.to(device, dtype=torch.int32)
if viewer:
viewer.set_points(patches.V.reshape(-1, 3).cpu().numpy(), c=[0, 0, 1], object_name="control_points")
viewer.set_mesh(v.cpu().numpy(), f=f.cpu().numpy(), c=[0, 1, 0], object_name="surface_points")
# Make the data and output directory visible to the Mitsuba file resolver
mi.set_variant('cuda_ad_rgb')
mi.Thread.thread().file_resolver().append(str(data_dir.absolute()))
mi.Thread.thread().file_resolver().append(str(output_dir.absolute()))
# Assemble the Mitsuba scene
emitter = create_emitter(emitter_type)
integrator = create_integrator()
sensor = create_sensor(render_resolution)
# Passing the initial lens as file to Mitsuba
# allows setting custom material parameters for the mesh
# TODO: Is this detour still necessary?
lens_filename = "lens.obj"
save_mesh(output_dir / lens_filename, v.cpu().numpy(), f=f.cpu().numpy())
scene = create_scene(integrator, sensor, emitter, lens_filename)
scene = mi.load_dict(scene)
# Define the function for rendering the caustics
params = mi.traverse(scene)
@drjit.wrap_ad(source='torch', target='drjit')
def render_caustics(vertices, spp=256, seed=1):
params["lens.vertex_positions"] = mi.Float(drjit.ravel(vertices))
params["lens.faces"] = mi.UInt(mi.Int(f.ravel()))
params.update()
return mi.render(scene, params, seed=seed, spp=2*spp, spp_grad=spp)
# Render the initial state and save the slab and lens
spp = 32
img = render_caustics(v, spp=spp, seed=0)
imageio.imwrite(output_dir / "initial.png", mi.util.convert_to_bitmap(img))
lens_mesh = [m for m in scene.shapes() if m.id() == 'lens'][0]
lens_mesh.write_ply(str(output_dir / "lens.ply"))
slab_mesh = [m for m in scene.shapes() if m.id() == 'slab'][0]
slab_mesh.write_ply(str(output_dir / "slab.ply"))
# Load the reference image and resize it to match the sensor
sensor = scene.sensors()[0]
crop_size = sensor.film().crop_size()
img_ref = load_ref_image(image_path, crop_size, output_dir=output_dir)
imageio.imwrite(output_dir/"image_reference.png", img_ref.cpu())
# Create output directories
lenses_output_dir = output_dir / "lenses"
render_output_dir = output_dir / "renderings"
lenses_output_dir.mkdir(parents=True, exist_ok=True)
render_output_dir.mkdir(parents=True, exist_ok=True)
# Optimize only the y-coordinate of the control points
y_opt = torch.zeros_like(patches.V[..., 0])
use_large_steps = True
factor = n/40
refinements = {it: (lambd, factor*step_size) for (it, lambd, step_size) in zip(refinement_iterations, large_steps_lambdas, step_sizes)}
def setup_optimizer(y_opt, lr, lambda_=1):
y_opt = y_opt.detach()
if use_large_steps:
# Optimize vertices with gradient preconditioning
from largesteps.parameterize import to_differential, from_differential
from largesteps.optimize import AdamUniform
# Compute the system matrix
F = generate_grid_faces(n, m, quads=True).to(device, dtype=torch.long)
M = compute_lsig_matrix(y_opt.reshape(-1, 1), F, lambda_=lambda_)
# Parameterize
u_opt = to_differential(M, y_opt.reshape(-1, 1))
u_opt.requires_grad = True
optimizer = AdamUniform([u_opt], lr=lr)
else:
y_opt.requires_grad = True
optimizer = torch.optim.Adam([y_opt], lr=lr)
def get_variables():
if use_large_steps:
from largesteps.parameterize import from_differential
return from_differential(M, u_opt, 'Cholesky').reshape_as(patches.V[..., 0])
return y_opt
return optimizer, get_variables
# NOTE: For the default parameters, this setup is a no-op because
# iteration 0 is marked as refinement iteration and triggers
# a setup inside of the optimization loop
optimizer, get_variables = setup_optimizer(y_opt, lr=0.1)
losses = []
progress_range = tqdm(range(num_iterations))
for it in progress_range:
if it in refinements:
optimizer, get_variables = setup_optimizer(y_opt, lr=refinements[it][1], lambda_=refinements[it][0])
y_opt = get_variables()
patches_opt = copy.deepcopy(patches)
patches_opt.V[..., 1] = y_opt.clamp(max=0.05)
# Pin the border
patches_opt.V[patches_opt.F[:, :, 0], 1] = 0
patches_opt.V[patches_opt.F[:, :, -1], 1] = 0
patches_opt.V[patches_opt.F[:, 0, :], 1] = 0
patches_opt.V[patches_opt.F[:, -1, :], 1] = 0
# Tessellate the lens and render the caustics image
v_opt, _, _, _ = tessellator.tessellate(patches_opt, degree=degree)
img_opt = render_caustics(v_opt, spp=spp, seed=it)
loss = scale_independent_loss(img_opt, img_ref)
if it % output_interval == 0:
imageio.imwrite(output_dir / f"optimized_{it}.png", mi.util.convert_to_bitmap(img_opt))
frame_index = it // output_interval
imageio.imwrite(render_output_dir / f"{frame_index}.png", mi.util.convert_to_bitmap(img_opt))
if save_intermediate_lenses:
frame_output_dir = lenses_output_dir / str(frame_index)
frame_output_dir.mkdir(parents=True, exist_ok=True)
P_opt = patches_opt.V[patches_opt.F]
F = generate_grid_faces(P_opt.shape[1], P_opt.shape[2], quads=True)
write_quad_mesh(frame_output_dir / f"controlmesh_0.obj", P_opt.reshape(-1, 3).cpu(), F)
save_mesh(frame_output_dir / f"surface_0.obj", v_opt, f)
if viewer:
P_opt = patches_opt.V[patches_opt.F]
viewer.set_mesh(v_opt.detach().cpu().numpy(), f=f.detach().cpu().numpy(), c=[0, 1, 0], object_name="surface_points")
viewer.set_points(P_opt.reshape(-1, 3).detach().cpu().numpy(), c=[0, 0, 1], object_name="control_points")
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses += [ float(loss.item()) ]
progress_range.set_postfix({"loss": f"{losses[-1]:0.6f}"})
# Save the control points of the lens
np.save(output_dir / "control_points.npy", {
'P': patches_opt.V[patches_opt.F].detach().cpu().numpy(),
'degree': degree,
})
# Save the tessellated lens and the final rendered caustics
save_mesh(output_dir / "optimized.obj", v_opt, f)
imageio.imwrite(output_dir / "optimized.png", mi.util.convert_to_bitmap(img_opt))
plt.plot(losses)
if display_plot:
plt.show()
else:
plt.savefig(output_dir / "loss.pdf")
return
def render_caustics(lens_path, render_resolution=(256, 256), emitter_type='gray', viewer: MeshViewer=None):
lens_path = Path(lens_path)
mi.set_variant('cuda_ad_rgb')
mi.Thread.thread().file_resolver().append("./data")
mi.Thread.thread().file_resolver().append(str(lens_path.parent))
emitter = create_emitter(emitter_type)
integrator = create_integrator()
sensor = create_sensor(render_resolution)
scene = create_scene(integrator, sensor, emitter, lens_path.name)
scene = mi.load_dict(scene)
spp = 128
img = mi.render(scene, spp=spp)
imageio.imwrite(lens_path.parent / "rendering.png", mi.util.convert_to_bitmap(img))
if __name__ == "__main__":
parser = ArgumentParser("Lens Design with Caustics", formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("image_path", type=Path, help="Path to the target image.")
parser.add_argument("output_dir", type=Path, help="Path to the output directory.")
parser.add_argument("--degree", type=int, default=4, help="Degree of the NURBS patch.")
parser.add_argument("--resolution_nm", type=int, default=[60, 60], nargs='+', help="Resolution of the control mesh.")
parser.add_argument("--resolution_uv", type=int, default=[128, 128], nargs='+', help="Resolution of the tessellation.")
parser.add_argument("--resolution_render", type=int, default=[128, 128], nargs='+', help="Resolution of the rendered images.")
parser.add_argument("--viewer", default=False, action="store_true", help="Show an interactive viewer")
parser.add_argument("--num_iterations", type=int, default=1000, help="Number of optimization iterations.")
parser.add_argument("--display_plot", default=False, action="store_true", help="Display the loss plot instead of saving it.")
parser.add_argument("--refinement_iterations", type=int, default=[0, 100, 400, 2000], nargs='+', help="Iterations at which to perform a refinement of LS lambda and LR.")
parser.add_argument("--large_steps_lambdas", type=float, default=[5., 5., 0., 0.], nargs='+', help="Large steps lambdas for the refinement iterations")
parser.add_argument("--step_sizes", type=float, default=[0.1, 0.01, 0.001, 0.0005], nargs='+', help="Gradient descent step sizes for the refinement iterations")
parser.add_argument("--output_interval", type=int, default=50, help="Interval for outputs.")
parser.add_argument("--save_intermediate_lenses", default=False, action="store_true", help="Save intermediate lenses as OBJ files.")
args = parser.parse_args()
output_dir: Path = args.output_dir
output_dir.mkdir(parents=True, exist_ok=True)
n, m = args.resolution_nm
u, v = args.resolution_uv
viewer = MeshViewer() if args.viewer else None
run_caustics_design(image_path=args.image_path, output_dir=output_dir, degree=args.degree, n=n, m=m, uv_resolution=args.resolution_uv, num_iterations=args.num_iterations, viewer=viewer, render_resolution=args.resolution_render,
refinement_iterations=args.refinement_iterations, large_steps_lambdas=args.large_steps_lambdas, step_sizes=args.step_sizes,
output_interval=args.output_interval, display_plot=args.display_plot, save_intermediate_lenses=args.save_intermediate_lenses)
render_caustics(output_dir / "optimized.obj", render_resolution=(512, 512))