-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.py
executable file
·359 lines (293 loc) · 13.2 KB
/
algorithms.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
"""
Various algorithms for LF/RGBD/RGB supervision.
Any questions about the code can be addressed to Suyeon Choi ([email protected])
This code and data is released under the Creative Commons Attribution-NonCommercial 4.0 International license (CC BY-NC.) In a nutshell:
# The license is only for non-commercial use (commercial licenses can be obtained from Stanford).
# The material is provided as-is, with no warranties whatsoever.
# If you publish any code, data, or scientific work based on this, please cite our work.
Technical Paper:
Time-multiplexed Neural Holography:
A Flexible Framework for Holographic Near-eye Displays with Fast Heavily-quantized Spatial Light Modulators
S. Choi*, M. Gopakumar*, Y. Peng, J. Kim, Matthew O'Toole, G. Wetzstein.
SIGGRAPH 2022
"""
import imageio
from PIL import Image, ImageDraw
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms.functional as TF
import numpy as np
from tqdm import tqdm
from copy import deepcopy
import utils
from holo2lf import holo2lf
def load_alg(alg_type, mem_eff=False):
if 'sgd' in alg_type.lower():
if mem_eff:
algorithm = efficient_gradient_descent
else:
algorithm = gradient_descent
else:
raise ValueError(f"Algorithm {alg_type} is not supported!")
return algorithm
def gradient_descent(init_phase, target_amp, target_mask=None, target_idx=None, forward_prop=None, num_iters=1000, roi_res=None,
border_margin=None, loss_fn=nn.MSELoss(), lr=0.01, out_path_idx='./results',
citl=False, camera_prop=None, writer=None, quantization=None,
time_joint=True, flipud=False, reg_lf_var=0.0, *args, **kwargs):
"""
Gradient-descent based method for phase optimization.
:param init_phase:
:param target_amp:
:param target_mask:
:param forward_prop:
:param num_iters:
:param roi_res:
:param loss_fn:
:param lr:
:param out_path_idx:
:param citl:
:param camera_prop:
:param writer:
:param quantization:
:param time_joint:
:param flipud:
:param args:
:param kwargs:
:return:
"""
print("Naive gradient descent")
assert forward_prop is not None
dev = init_phase.device
h, w = init_phase.shape[-2], init_phase.shape[-1] # total energy = h*w
init_amp = torch.ones_like(init_phase) * 0.5
init_amp_logits = torch.log(init_amp / (1 - init_amp)) # convert to inverse sigmoid
slm_phase = init_phase.requires_grad_(True) # phase at the slm plane
slm_amp_logits = init_amp_logits.requires_grad_(True) # amplitude at the slm plane
optvars = [{'params': slm_phase}]
if kwargs["optimize_amp"]:
optvars.append({'params': slm_amp_logits})
#if "opt_s" in reg_loss_fn_type:
# s = torch.tensor(1.0).requires_grad_(True) # initial s value
# optvars.append({'params': s})
#else:
# s = None
s = torch.tensor(1.0)
optimizer = optim.Adam(optvars, lr=lr)
loss_vals = []
psnr_vals = []
loss_vals_quantized = []
best_loss = 1e10
best_iter = 0
best_amp = None
lf_supervision = len(target_amp.shape) > 4
print("target amp shape", target_amp.shape)
if target_mask is not None:
target_amp = target_amp * target_mask
nonzeros = target_mask > 0
if roi_res is not None:
target_amp = utils.crop_image(target_amp, roi_res, stacked_complex=False, lf=lf_supervision)
if target_mask is not None:
target_mask = utils.crop_image(target_mask, roi_res, stacked_complex=False, lf=lf_supervision)
nonzeros = target_mask > 0
if border_margin is not None:
# make borders of target black
mask = torch.zeros_like(target_amp)
mask[:, :, border_margin:-border_margin, border_margin:-border_margin] = 1
target_amp = target_amp * mask
for t in tqdm(range(num_iters)):
optimizer.zero_grad()
if quantization is not None:
quantized_phase = quantization(slm_phase, t/num_iters)
else:
quantized_phase = slm_phase
if flipud:
quantized_phase_f = quantized_phase.flip(dims=[2])
else:
quantized_phase_f = quantized_phase
field_input = torch.exp(1j * quantized_phase_f)
recon_field = forward_prop(field_input)
recon_field = utils.crop_image(recon_field, roi_res, pytorch=True, stacked_complex=False) # here, also record an uncropped image
if lf_supervision:
recon_amp_t = holo2lf(recon_field, n_fft=kwargs['n_fft'], hop_length=kwargs['hop_len'],
win_length=kwargs['win_len'], device=dev, impl='torch').sqrt()
else:
recon_amp_t = recon_field.abs()
if time_joint: # time-multiplexed forward model
recon_amp = (recon_amp_t**2).mean(dim=0, keepdims=True).sqrt()
else:
recon_amp = recon_amp_t
if citl: # surrogate gradients for CITL
captured_amp = camera_prop(slm_phase, 1)
captured_amp = utils.crop_image(captured_amp, roi_res,
stacked_complex=False)
recon_amp_sim = recon_amp.clone() # simulated reconstructed image
recon_amp = recon_amp + captured_amp - recon_amp.detach() # reconstructed image with surrogate gradients
# clip to range
if target_mask is not None:
final_amp = torch.zeros_like(recon_amp)
final_amp[nonzeros] += (recon_amp[nonzeros] * target_mask[nonzeros])
else:
final_amp = recon_amp
# also track gradient of s
with torch.no_grad():
s = (final_amp * target_amp).mean(dim=(-1, -2), keepdims=True) / (final_amp ** 2).mean(dim=(-1, -2), keepdims=True) # scale minimizing MSE btw recon and target
loss_val = loss_fn(s * final_amp, target_amp)
mse_loss = ((s * final_amp - target_amp)**2).mean().item()
psnr_val = 20 * np.log10(1 / np.sqrt(mse_loss))
# loss term for having even emission at in-focus points (STFT-based regularization described in Supplementary)
if reg_lf_var > 0.0:
recon_amp_lf = holo2lf(recon_field, n_fft=kwargs['n_fft'], hop_length=kwargs['hop_len'],
win_length=kwargs['win_len'], device=dev, impl='torch')
recon_amp_lf = s * recon_amp_lf.mean(dim=0, keepdims=True).sqrt()
loss_lf_var = torch.mean(torch.var(recon_amp_lf, (-2, -1)))
loss_val += reg_lf_var * loss_lf_var
loss_val.backward()
optimizer.step()
with torch.no_grad():
if loss_val.item() < best_loss:
best_phase = slm_phase
best_loss = loss_val.item()
best_amp = s * final_amp # fits target image.
best_iter = t + 1
psnr = 20 * torch.log10(1 / torch.sqrt(((s * final_amp - target_amp)**2).mean()))
psnr_vals.append(psnr.item())
return {'loss_vals': loss_vals,
'psnr_vals': psnr_vals,
'loss_vals_q': loss_vals_quantized,
'best_iter': best_iter,
'best_loss': best_loss,
'recon_amp': best_amp,
'target_amp': target_amp,
'final_phase': best_phase
}
def efficient_gradient_descent(init_phase, target_amp, target_mask=None, target_idx=None, forward_prop=None, num_iters=1000, roi_res=None,
loss_fn=nn.MSELoss(), lr=0.01, out_path_idx='./results',
citl=False, camera_prop=None, writer=None, quantization=None,
time_joint=True, flipud=False, *args, **kwargs):
"""
Gradient-descent based method for phase optimization.
:param init_phase:
:param target_amp:
:param target_mask:
:param forward_prop:
:param num_iters:
:param roi_res:
:param loss_fn:
:param lr:
:param out_path_idx:
:param citl:
:param camera_prop:
:param writer:
:param quantization:
:param time_joint:
:param flipud:
:param args:
:param kwargs:
:return:
"""
print("Memory efficient gradient descent")
assert forward_prop is not None
dev = init_phase.device
num_frames = init_phase.shape[0]
slm_phase = init_phase.requires_grad_(True) # phase at the slm plane
optvars = [{'params': slm_phase}]
optimizer = optim.Adam(optvars, lr=lr)
loss_vals = []
loss_vals_quantized = []
best_loss = 10.
lf_supervision = len(target_amp.shape) > 4
if target_mask is not None:
target_amp = target_amp * target_mask
nonzeros = target_mask > 0
if roi_res is not None:
target_amp = utils.crop_image(target_amp, roi_res, stacked_complex=False, lf=lf_supervision)
if target_mask is not None:
target_mask = utils.crop_image(target_mask, roi_res, stacked_complex=False, lf=lf_supervision)
nonzeros = target_mask > 0
for t in tqdm(range(num_iters)):
optimizer.zero_grad() # zero grad
# amplitude reconstruction without graph
with torch.no_grad():
if quantization is not None:
quantized_phase = quantization(slm_phase, t/num_iters)
else:
quantized_phase = slm_phase
if flipud:
quantized_phase_f = quantized_phase.flip(dims=[2])
else:
quantized_phase_f = quantized_phase
recon_field = forward_prop(quantized_phase_f) # just sample one depth plane
recon_field = utils.crop_image(recon_field, roi_res, stacked_complex=False)
if lf_supervision:
recon_amp_t = holo2lf(recon_field, n_fft=kwargs['n_fft'], hop_length=kwargs['hop_len'],
win_length=kwargs['win_len'], device=dev, impl='torch').sqrt()
else:
recon_amp_t = recon_field.abs()
if citl: # surrogate gradients for CITL
captured_amp = camera_prop(slm_phase)
captured_amp = utils.crop_image(captured_amp, roi_res,
stacked_complex=False)
total_loss_val = 0
# insert single frame's graph and accumulate gradient
for f in range(num_frames):
slm_phase_sf = slm_phase[f:f+1, ...]
if quantization is not None:
quantized_phase_sf = quantization(slm_phase_sf, t/num_iters)
else:
quantized_phase_sf = slm_phase_sf
if flipud:
quantized_phase_f_sf = quantized_phase_sf.flip(dims=[2])
else:
quantized_phase_f_sf = quantized_phase_sf
recon_field_sf = forward_prop(quantized_phase_f_sf)
recon_field_sf = utils.crop_image(recon_field_sf, roi_res, stacked_complex=False)
if lf_supervision:
recon_amp_t_sf = holo2lf(recon_field_sf, n_fft=kwargs['n_fft'], hop_length=kwargs['hop_len'],
win_length=kwargs['win_len'], device=dev, impl='torch').sqrt()
else:
recon_amp_t_sf = recon_field_sf.abs()
### insert graph from single frame ###
recon_amp_t_with_grad = recon_amp_t.clone().detach()
recon_amp_t_with_grad[f:f+1,...] = recon_amp_t_sf
if time_joint: # time-multiplexed forward model
recon_amp = (recon_amp_t_with_grad**2).mean(dim=0, keepdims=True).sqrt()
else:
recon_amp = recon_amp_t_with_grad
if citl:
recon_amp = recon_amp + captured_amp / (num_frames) - recon_amp.detach()
if target_mask is not None:
final_amp = torch.zeros_like(recon_amp)
final_amp[nonzeros] += recon_amp[nonzeros] * target_mask[nonzeros]
else:
final_amp = recon_amp
with torch.no_grad():
s = (final_amp * target_amp).mean() / \
(final_amp ** 2).mean() # scale minimizing MSE btw recon and
loss_val = loss_fn(s * final_amp, target_amp)
loss_val.backward(retain_graph=False)
total_loss_val += loss_val.item()
if t % 10 == 0:
pass
#writer.add_scalar("loss", total_loss_val, t)
#writer.add_scalar("recon loss", recon_loss.item(), t)
#writer.add_scalar("light eff loss", reg_loss.item(), t)
#writer.add_scalar("s", s.item(), t)
#writer.add_image("recon", torch.clamp(s*final_amp[0], 0, 1), t)
# update phase variables
optimizer.step()
with torch.no_grad():
if total_loss_val < best_loss:
best_phase = slm_phase
best_loss = total_loss_val
best_amp = s * recon_amp
best_iter = t + 1
print(total_loss_val)
return {'loss_vals': loss_vals,
'loss_vals_q': loss_vals_quantized,
'best_iter': best_iter,
'best_loss': best_loss,
'recon_amp': best_amp,
'target_amp': target_amp,
'final_phase': best_phase,
's': s.item()}