-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path1_autolens.py
More file actions
311 lines (269 loc) · 10.4 KB
/
Copy path1_autolens.py
File metadata and controls
311 lines (269 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
"""Automated lens design from scratch using RMS spot size with curriculum learning.
Technical Paper:
Xinge Yang, Qiang Fu and Wolfgang Heidrich, "Curriculum learning for ab
initio deep learned refractive optics," Nature Communications 2024.
Usage:
python 1_autolens.py
"""
import json
import logging
import math
import os
import random
import string
from datetime import datetime
import torch
from tqdm import tqdm
from deeplens import GeoLens
from deeplens.geolens_pkg import create_lens
from deeplens.utils_deeplens import (
create_video_from_images,
record_losses,
save_loss_curve,
set_logger,
set_seed,
)
# ── Default lens design configuration ────────────────────────────────────────
CAMERA_CONFIG = {
"foclen": 85.0,
"fov": 40.0,
"fnum": 2.8,
"bfl": 18.0,
"thickness": 120.0,
"surf_list": [
["Spheric", "Spheric"],
["Spheric", "Spheric"],
["Spheric", "Spheric", "Spheric"],
["Aperture"],
["Spheric", "Spheric", "Spheric"],
["Spheric", "Aspheric"],
["Spheric", "Aspheric"],
],
"lrs": [1e-3, 1e-3, 1e-2, 1e-4],
"curriculum_iters": 2000,
"refine_iters": 3000,
"seed": 42,
}
MOBILE_CONFIG = {
"foclen": 5.0,
"fov": 90.0,
"fnum": 1.8,
"bfl": 2.0,
"thickness": 10.0,
"surf_list": [
["Aperture"],
["Aspheric", "Aspheric"],
["Aspheric", "Aspheric"],
["Aspheric", "Aspheric"],
["Aspheric", "Aspheric"],
["Aspheric", "Aspheric"],
["Aspheric", "Aspheric"],
],
"lrs": [1e-4, 1e-3, 1e-2, 1e-4],
"curriculum_iters": 2000,
"refine_iters": 3000,
"seed": 42,
}
# ── Curriculum design ─────────────────────────────────────────────────────────
def curriculum_design(
self,
lrs=[1e-3, 1e-3, 1e-2, 1e-4],
iterations=5000,
test_per_iter=100,
optim_mat=False,
shape_control=True,
centroid=True,
depth=None,
wvln_list=None,
num_ring=8,
num_arm=1,
spp=512,
result_dir="./results",
):
"""Optimise the lens from scratch using curriculum aperture growth.
Gradually increases the aperture from 25% to full size over the
training schedule, transforming a hard global optimisation into a
sequence of easier subproblems.
Args:
lrs (list, optional): Learning rates for [d, c, k, ai].
iterations (int, optional): Total training iterations.
test_per_iter (int, optional): Evaluate and save every N iterations.
optim_mat (bool, optional): Optimise material parameters.
shape_control (bool, optional): Correct surface shapes at each evaluation.
centroid (bool, optional): Use geometric centroid as the PSF reference
centre (True) or pinhole ideal (False). Defaults to True.
depth (float, optional): Object distance in mm. Defaults to DEPTH.
wvln_list (list[float], optional): Wavelengths in micrometers for
the polychromatic RMS loss. Defaults to the lens RGB
wavelengths.
num_ring (int, optional): Number of radial rings in the ring-arm
field grid. Defaults to 8.
num_arm (int, optional): Number of azimuthal arms. Defaults to 1.
spp (int, optional): Rays per field point per wavelength.
Defaults to 1024.
result_dir (str, optional): Directory to save results.
"""
depth = self.obj_depth if depth is None else depth
wvln_list = self.wvln_ls if wvln_list is None else wvln_list
assert len(wvln_list) == 3, "wvln_list must contain 3 wavelengths"
aper_start = self.surfaces[self.aper_idx].r * 0.5
aper_final = self.surfaces[self.aper_idx].r
os.makedirs(result_dir, exist_ok=True)
if not logging.getLogger().hasHandlers():
logger = logging.getLogger()
logger.setLevel("DEBUG")
fmt = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s", "%Y-%m-%d %H:%M:%S")
sh = logging.StreamHandler()
sh.setFormatter(fmt)
sh.setLevel("INFO")
fh = logging.FileHandler(f"{result_dir}/output.log")
fh.setFormatter(fmt)
fh.setLevel("INFO")
logger.addHandler(sh)
logger.addHandler(fh)
logging.info(
f"lr:{lrs}, iterations:{iterations}, spp:{spp}, "
f"num_ring:{num_ring}, num_arm:{num_arm}."
)
optimizer = self.get_optimizer(lrs, optim_mat=optim_mat)
scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(
optimizer, T_0=iterations // 4, T_mult=1
)
pbar = tqdm(
total=iterations + 1, desc="Curriculum", postfix={"loss_rms": 0, "loss_reg": 0}
)
loss_history: dict = {}
for i in range(iterations + 1):
# === Evaluate ===
if i % test_per_iter == 0:
with torch.no_grad():
progress = 0.5 * (1 + math.cos(math.pi * (1 - i / iterations)))
aper_r = min(
aper_start + (aper_final - aper_start) * progress,
aper_final,
)
self.surfaces[self.aper_idx].update_r(aper_r)
self.calc_pupil()
if i > 0:
if shape_control:
self.correct_shape()
self.write_lens_json(f"{result_dir}/iter{i}.json")
self.analysis(f"{result_dir}/iter{i}")
save_loss_curve(loss_history, result_dir)
rays_backup = []
for wv in wvln_list:
ray = self.sample_ring_arm_rays(
num_ring=num_ring,
num_arm=num_arm,
depth=depth,
spp=spp,
wvln=wv,
scale_pupil=1.05,
)
rays_backup.append(ray)
pinhole_ref = -self.psf_center(
points_obj=ray.o[:, :, 0, :], method="pinhole"
)
# === Compute RMS loss ===
loss_rms, loss_distortion = self.loss_rms_from_ray(
rays_backup, pinhole_ref, centroid=centroid
)
w_reg = 0.1
# Activate the material (n, Abbe V) constraint only while materials
# are being optimised; with frozen materials the gradient is zero
# and w_mat would just add noise to the logged loss.
loss_reg, loss_dict = self.loss_reg(w_mat=1.0 if optim_mat else 0.0)
L_total = loss_rms + w_reg * (loss_reg + loss_distortion)
optimizer.zero_grad()
L_total.backward()
self._sanitize_optimizer_grads(optimizer)
optimizer.step()
scheduler.step()
record_losses(loss_history, i, {
"loss_rms": loss_rms.item(),
"loss_reg": loss_reg.item(),
"loss_distortion": loss_distortion.item(),
**loss_dict,
})
pbar.set_postfix(
loss_rms=loss_rms.item(),
loss_reg=loss_reg.item(),
loss_distortion=loss_distortion.item(),
**loss_dict,
)
pbar.update(1)
save_loss_curve(loss_history, result_dir)
pbar.close()
GeoLens.curriculum_design = curriculum_design
# ── Main ──────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
cfg = MOBILE_CONFIG.copy()
# Setup
seed = cfg["seed"] if cfg["seed"] is not None else random.randint(0, 100000)
set_seed(seed)
tag = "".join(random.choice(string.ascii_letters + string.digits) for _ in range(4))
result_dir = f"./results/{datetime.now().strftime('%m%d-%H%M%S')}-AutoLens-{tag}"
os.makedirs(result_dir, exist_ok=True)
set_logger(result_dir)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logging.info(f"Device: {device}")
# Save config
with open(f"{result_dir}/config.json", "w") as f:
json.dump(cfg, f, indent=2)
# Create lens
logging.info(
f"==> Design target: foclen={cfg['foclen']}, FoV={cfg['fov']}deg, F/{cfg['fnum']}"
)
lens = create_lens(
foclen=cfg["foclen"],
fov=cfg["fov"],
fnum=cfg["fnum"],
bfl=cfg["bfl"],
thickness=cfg["thickness"],
surf_list=cfg["surf_list"],
save_dir=result_dir,
)
lens.set_target_fov_fnum(rfov_eff=cfg["fov"] / 2 / 57.3, fnum=cfg["fnum"])
# Stage 1: Curriculum learning (optimize material, then match to catalog)
curriculum_iters = cfg["curriculum_iters"]
logging.info(f"==> Stage 1: Curriculum learning ({curriculum_iters} iters)")
lens.curriculum_design(
lrs=[float(lr) for lr in cfg["lrs"]],
iterations=curriculum_iters,
test_per_iter=max(curriculum_iters // 50, 1),
optim_mat=True,
shape_control=True,
centroid=True,
result_dir=result_dir,
)
lens.match_materials()
lens.set_fnum(cfg["fnum"])
lens.write_lens_json(f"{result_dir}/curriculum_final.json")
lens.analysis(save_name=f"{result_dir}/curriculum_final.png")
# Stage 2: Fine-tuning with centroid PSF center
# Centroid-based RMS only helps at wide FoV where off-axis PSFs are
# asymmetric enough that the geometric centroid diverges from the chief
# ray. Threshold ~30 deg half-diagonal (0.52 rad).
use_centroid = lens.rfov_eff > 0.52
refine_iters = cfg["refine_iters"]
logging.info(
f"==> Stage 2: Fine-tuning centroid={use_centroid} ({refine_iters} iters)"
)
lens = GeoLens(filename=f"{result_dir}/curriculum_final.json")
lens.optimize(
lrs=[0.5 * float(lr) for lr in cfg["lrs"]],
iterations=refine_iters,
test_per_iter=max(refine_iters // 50, 1),
centroid=use_centroid,
optim_mat=False,
shape_control=True,
sample_more_off_axis=True,
result_dir=f"{result_dir}/refine",
)
# Final result
lens.prune_surf(expand_factor=0.05)
lens.post_computation()
logging.info(f"Final: FoV={lens.rfov_eff:.4f} rad, F/{lens.fnum:.2f}, r_sensor={lens.r_sensor:.4f}")
lens.write_lens_json(f"{result_dir}/final_lens.json")
lens.analysis(save_name=f"{result_dir}/final_lens")
create_video_from_images(f"{result_dir}", f"{result_dir}/autolens.mp4", fps=10)