HybridLens
Combines a GeoLens with a diffractive optical element (DOE).
HybridLens performs coherent ray tracing to the DOE plane, then Angular
Spectrum Method (ASM) propagation to the sensor — a hybrid ray–wave model for
refractive lenses with DOE or metasurface phase elements.
deeplens.HybridLens
HybridLens(
filename=None,
device=None,
dtype=torch.float64,
primary_wvln=DEFAULT_WAVE,
wvln_rgb=WAVE_RGB,
obj_depth=DEPTH,
)
Bases: Lens
Hybrid refractive-diffractive lens using a differentiable ray-wave model.
Combines a GeoLens (refractive module) with a diffractive optical element
(DOE) placed behind it. The pipeline is: (1) coherent ray tracing through the
embedded GeoLens to obtain a complex wavefront at the DOE plane (including
all geometric aberrations); (2) DOE phase modulation applied to the
wavefront; (3) Angular Spectrum Method (ASM) propagation from the DOE to the
sensor plane to produce the final intensity PSF.
This enables end-to-end gradient flow from image-quality metrics back to both
refractive surface parameters and the DOE phase profile. Operates in
torch.float64 by default for numerical stability of the wave-propagation
step.
Attributes:
| Name | Type | Description |
|---|---|---|
geolens |
GeoLens
|
Embedded refractive module. The DOE plane is appended
to its surface list as a |
doe |
Binary2 or Pixel2D or Fresnel or Zernike or Grating
|
Diffractive optical element behind the refractive group. |
foclen |
float
|
Focal length [mm], copied from the embedded |
Reference
Xinge Yang et al., "End-to-End Hybrid Refractive-Diffractive Lens Design with Differentiable Ray-Wave Model," SIGGRAPH Asia 2024.
Initialize a hybrid refractive-diffractive lens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the lens configuration JSON file. Defaults to None. |
None
|
device
|
str
|
Computation device ('cpu' or 'cuda'). Defaults to None. |
None
|
dtype
|
dtype
|
Data type for computations. Defaults to |
float64
|
primary_wvln
|
float
|
Primary design wavelength [µm].
Used as fallback when a method is called without an explicit
|
DEFAULT_WAVE
|
wvln_rgb
|
list of float
|
Three wavelengths [µm] used
for RGB computations, ordered [R, G, B]. Defaults to |
WAVE_RGB
|
obj_depth
|
float
|
Default object depth [mm], used
when a method is called without an explicit |
DEPTH
|
Source code in deeplens-src/deeplens/hybridlens.py
read_lens_json
Read the lens configuration from a JSON file.
Loads a GeoLens and associated DOE from the specified file. A Plane
surface is appended to the GeoLens surface list as a placeholder for the
DOE plane, matching the DOE aperture (square vs circular). Also sets
self.foclen and the sensor size/resolution from the loaded GeoLens.
Supported DOE types: binary2, pixel2d, fresnel, zernike, grating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the JSON configuration file. Must contain a "DOE" key with a "type" field. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the DOE type in the file is not supported. |
Source code in deeplens-src/deeplens/hybridlens.py
write_lens_json
Write the lens configuration to a JSON file.
Serialises the GeoLens surfaces (excluding the DOE placeholder) and the
DOE configuration into a single JSON file that can be reloaded with
read_lens_json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lens_path
|
str
|
Output file path. |
required |
Source code in deeplens-src/deeplens/hybridlens.py
analysis
Run a quick visual analysis of the hybrid lens.
Generates two figures: the 2D lens layout (saved to save_name) and the
DOE phase map (saved to <save_name>_doe.png).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_name
|
str
|
Base file path for the layout image. The
DOE phase-map image path is formed by appending |
'./test.png'
|
Source code in deeplens-src/deeplens/hybridlens.py
double
Convert the GeoLens and DOE to float64 precision.
Double precision is required for numerically stable phase accumulation
during coherent ray tracing and ASM propagation. Called automatically by
__init__.
Source code in deeplens-src/deeplens/hybridlens.py
refocus
Refocus the hybrid lens to a given object distance.
Delegates to GeoLens.refocus, which adjusts the sensor distance; the
DOE remains fixed relative to the refractive group (it is physically
cemented to the lens barrel).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foc_dist
|
float
|
Target focus distance in [mm] (negative, towards the object). |
required |
Source code in deeplens-src/deeplens/hybridlens.py
calc_scale
Calculate the object-to-image magnification scale factor.
Delegates to the embedded GeoLens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
float
|
Object distance in [mm] (negative, towards the object). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scale |
float
|
Magnification factor (object height / image height), computed as \(-\text{depth} / \text{foclen}\). |
Source code in deeplens-src/deeplens/hybridlens.py
doe_field
Compute the complex wave field at the DOE plane via coherent ray tracing.
Similar to GeoLens.pupil_field, but evaluates the field at the last
surface (DOE plane) instead of the exit pupil. The returned wavefront
encodes amplitude, phase, and all diffraction-order information needed
for subsequent DOE modulation and ASM propagation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
Tensor
|
Point source position, shape [3] or [1, 3] as [x, y, z]. x/y are in normalised sensor coordinates [-1, 1]; z is depth in [mm]. |
required |
wvln
|
float
|
Wavelength [µm]. When None (default), falls
back to |
None
|
spp
|
int
|
Number of rays to sample. Must be at least
1,000,000 for accurate coherent simulation. Defaults to
|
SPP_COHERENT
|
upsample_factor
|
int or None
|
Field upsampling factor to
meet the Nyquist sampling constraint. The field is sampled on a
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
wavefront |
Tensor
|
Complex wavefront at the DOE plane, shape
[H, W] where H = W = |
psf_center |
list of float
|
Estimated PSF centre on the sensor in normalised coordinates [x, y]. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in deeplens-src/deeplens/hybridlens.py
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 | |
psf
Compute a single-point monochromatic PSF using the ray-wave model.
The returned PSF includes all diffraction orders with physically correct
diffraction efficiencies. The pipeline is: (1) coherent ray tracing
through the GeoLens to obtain the complex wavefront at the DOE plane;
(2) DOE phase modulation applied to the wavefront; (3) ASM propagation to
the sensor, intensity calculation, cropping, and normalisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list or Tensor
|
[x, y, z] point source coordinates. x, y are in normalised sensor coordinates [-1, 1]; z is depth in [mm]. When None (default), uses [0.0, 0.0, -10000.0]. |
None
|
wvln
|
float
|
Wavelength [µm]. When None (default), falls
back to |
None
|
ks
|
int or None
|
Output PSF patch size. When None, the
centre half of the field is returned instead. Defaults to
|
PSF_KS
|
**kwargs
|
Model-specific options. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
Normalised PSF patch (sums to 1), shape [ks, ks]
(or roughly half the field per side when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the default dtype is not |
Source code in deeplens-src/deeplens/hybridlens.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
draw_layout
Draw the hybrid-lens layout with ray paths and wave-propagation arcs.
Renders the refractive elements via GeoLens.draw_lens_2d, traces rays
at three field angles (on-axis, 0.707x, 0.99x full field), and overlays
concentric arcs between the DOE and sensor to illustrate the
wave-propagation region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_name
|
str
|
File path to save the figure (used only
when |
'./DOELens.png'
|
depth
|
float
|
Object depth [mm] for the traced rays. Defaults to -10000.0. |
-10000.0
|
ax
|
Axes
|
Pre-existing axes to draw into. If None, a new figure is created and saved. |
None
|
fig
|
Figure
|
Pre-existing figure.
Required when |
None
|
dpi
|
int
|
Resolution used when saving a new figure. Defaults to 600. |
600
|
Returns:
| Name | Type | Description |
|---|---|---|
ax |
Axes
|
The axes, returned only when |
fig |
Figure
|
The figure, returned only when |
Source code in deeplens-src/deeplens/hybridlens.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
get_optimizer
Build an Adam optimiser for joint lens + DOE design.
Collects trainable parameters from both the GeoLens (surface
thicknesses, curvatures, conic constants, aspheric coefficients) and the
DOE phase profile into a single optimiser with per-group learning rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_lr
|
float
|
Learning rate for DOE phase parameters. Defaults to 1e-4. |
0.0001
|
lens_lr
|
list of float
|
Per-parameter-group learning rates for the GeoLens, ordered as [thickness_d, curvature_c, conic_k, aspheric_a]. Defaults to [1e-4, 1e-4, 1e-2, 1e-5]. |
[0.0001, 0.0001, 0.01, 1e-05]
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer |
Adam
|
Configured optimiser over all trainable parameters. |
Source code in deeplens-src/deeplens/hybridlens.py
DOE Models
The diffractive element is configured by the DOE block in the lens JSON; its
type field selects one of the phase parameterizations below. All subclass
DiffractiveSurface, which defines the shared phase / propagation interface.
deeplens.diffractive_surface.DiffractiveSurface
DiffractiveSurface(
d_next,
res,
fab_ps=0.001,
fab_step=16,
wvln0=0.55,
mat="fused_silica",
design_ps=None,
is_square=True,
device="cpu",
)
Bases: DeepObj
Base class for diffractive optical elements (DOEs).
A diffractive surface modulates the phase of an incident wave field; its
optical behavior is simulated with wave optics. The phase profile is defined
by phase_func in subclasses and converted into a wrapped, quantized phase
map for the design wavelength. By default the DOE is designed for 0.55um,
i.e. it has the highest 1st-order diffraction efficiency at 0.55um.
Attributes:
| Name | Type | Description |
|---|---|---|
d_next |
Tensor
|
Axial thickness to the next plane. [mm] |
res |
tuple
|
DOE resolution as (H, W). [pixel] |
ps |
float
|
Pixel size of the phase map (design pixel size if given, otherwise the fabrication pixel size). [mm] |
w |
float
|
Physical width of the DOE. [mm] |
h |
float
|
Physical height of the DOE. [mm] |
is_square |
bool
|
Whether the aperture is treated as square. |
r |
float
|
Aperture radius (half-diagonal / circumscribed-circle radius). [mm] |
mat |
Material
|
DOE material. |
wvln0 |
float
|
Design wavelength. [um] |
n0 |
float
|
Refractive index of the material at |
fab_ps |
float
|
Fabrication pixel size. [mm] |
fab_step |
int
|
Number of fabrication (quantization) levels. |
x |
Tensor
|
x-coordinates of the grid. [H, W]. [mm] |
y |
Tensor
|
y-coordinates of the grid. [H, W]. [mm] |
Initialize a diffractive surface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
res
|
tuple or int
|
Resolution of the DOE as (H, W); an int is expanded to (res, res). [pixel] |
required |
fab_ps
|
float
|
Fabrication pixel size. [mm]. Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication (quantization) levels. Defaults to 16. |
16
|
wvln0
|
float
|
Design wavelength. [um]. Defaults to 0.55. |
0.55
|
mat
|
str
|
Material name of the DOE. Defaults to "fused_silica". |
'fused_silica'
|
design_ps
|
float or None
|
Design pixel size; if None the fabrication pixel size is used as the phase-map pixel size. [mm]. Defaults to None. |
None
|
is_square
|
bool
|
Whether the aperture is square. Defaults to True. |
True
|
device
|
str
|
Device to place the DOE tensors on. Defaults to "cpu". |
'cpu'
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
init_from_dict
classmethod
Initialize a DOE from a dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Dictionary of DOE parameters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
doe |
DiffractiveSurface
|
The constructed DOE instance. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
phase_func
Compute the raw phase profile (no wrapping, no quantization) at the design wavelength.
Returns:
| Name | Type | Description |
|---|---|---|
phase |
Tensor
|
Raw, unwrapped phase profile at the design wavelength. [H, W]. [rad] |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
get_phase_map0
Compute the phase map at the design wavelength with wrapping and quantization.
The raw phase from phase_func is wrapped into \([0, 2\pi)\) and then
quantized to fab_step levels. The wrapped phase is equivalent to a
height map whose maximum height corresponds to \(2\pi\) at the design
wavelength.
Returns:
| Name | Type | Description |
|---|---|---|
phase0 |
Tensor
|
Wrapped, quantized phase map at the design wavelength. [H, W], range \([0, 2\pi)\). [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
get_phase_map
Compute the phase map at the given wavelength.
The phase map is first computed at the design wavelength, then scaled to the requested wavelength accounting for the wavelength ratio and the material dispersion \((n - 1) / (n_0 - 1)\), and finally resampled (nearest) to the DOE resolution if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wvln
|
float
|
Wavelength. [um] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
phase_map |
Tensor
|
Phase map at the given wavelength. [H, W]. [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
forward
Apply phase modulation, then propagate by this surface's d_next.
The input wave field may have a different pixel size and physical extent than the DOE; the phase map is resampled (nearest) to match the wave pixel size, then center-cropped or zero-padded to match the wave resolution before being applied as \(u \cdot e^{i\phi}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
ComplexWave
|
Input complex wave field, with field |
required |
Returns:
| Name | Type | Description |
|---|---|---|
wave |
ComplexWave
|
Output complex wave field after propagation and
phase modulation, field |
Reference
[1] https://github.com/vsitzmann/deepoptics function phaseshifts_from_height_map
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
__call__
Apply the DOE to a wave field (alias for forward).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wave
|
ComplexWave
|
Input complex wave field. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
wave |
ComplexWave
|
Output complex wave field. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
quantize_phase_map
Quantize the design-wavelength phase map to a given number of levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bits
|
int
|
Number of quantization levels. Defaults to 16. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
pmap_q |
Tensor
|
Quantized phase map. [H, W], range \([0, 2\pi)\). [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
export_fab_phase_map
Generate a fabrication-resolution quantized phase map and save a checkpoint.
The phase map is upsampled from the design pixel size to the fabrication
pixel size (bilinear) and quantized to bits levels. The DOE checkpoint
is saved to save_path; the DOE object itself is left unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bits
|
int
|
Number of quantization levels. Defaults to 16. |
16
|
save_path
|
str or None
|
Checkpoint save path; if None a name encoding the fabrication resolution, pixel size, and bit depth is generated. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
pmap_q |
Tensor
|
Fabrication-resolution quantized phase map. [H_fab, W_fab], range \([0, 2\pi)\). [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
activate_grad
Enable or disable gradients on the phase-map parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activate
|
bool
|
Whether to require gradients. Defaults to True. |
True
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
get_optimizer_params
Build optimizer parameter groups for the phase-map parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float or None
|
Learning rate. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
params |
list
|
List of parameter group dicts for an optimizer. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Must be implemented by subclasses. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
get_optimizer
Create an Adam optimizer for the DOE phase-map parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float or None
|
Learning rate passed to
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer |
Adam
|
Optimizer over the DOE phase-map parameters. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
loss_quantization
Compute the mean phase quantization error of the DOE.
Returns the mean absolute difference between the continuous phase map
and its quantization to bits levels, used as a quantization-aware
regularization loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bits
|
int
|
Number of quantization levels. Defaults to 16. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar mean absolute quantization error. [rad] |
Reference
Quantization-aware Deep Optics for Diffractive Snapshot Hyperspectral Imaging.
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
draw_phase_map
Save the design-wavelength phase map as a normalized image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bits
|
int or None
|
Number of quantization levels; if given the phase map is quantized first, otherwise the continuous map is used. Defaults to None. |
None
|
save_name
|
str
|
Path to save the image. Defaults to "./DOE_phase_map.png". |
'./DOE_phase_map.png'
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
draw_phase_map3d
Save a 3D scatter plot of the design-wavelength phase map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bits
|
int or None
|
Number of quantization levels; if given the phase map is quantized first, otherwise the continuous map is used. Defaults to None. |
None
|
save_name
|
str
|
Path to save the image. Defaults to "./DOE_phase_map3d.png". |
'./DOE_phase_map3d.png'
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
draw_phase_map_fab
Save side-by-side images of the continuous and 16-level quantized phase maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_name
|
str
|
Path to save the figure. Defaults to "./DOE_phase_map.png". |
'./DOE_phase_map.png'
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
draw_cross_section
Save a plot of the phase map along its main diagonal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_name
|
str
|
Path to save the figure. Defaults to "./DOE_cross_section.png". |
'./DOE_cross_section.png'
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
draw_widget
Draw a 2D Fresnel-style cross-section of the DOE in a layout plot.
Plots the cross-section along the x-axis at y=0. For a square aperture
the half-extent is the half-side (w/2); for a circular aperture it is
the full radius r (= half-diagonal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes to draw on. |
required |
color
|
str
|
Line color. Defaults to "orange". |
'orange'
|
linestyle
|
str
|
Line style. Defaults to "-". |
'-'
|
d
|
float
|
Derived global vertex position [mm]. Defaults to 0. |
0.0
|
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
surf_dict
Serialize the DOE surface parameters into a dict.
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Surface parameters (type, size, thickness, design wavelength, resolution, fabrication pixel size, and aperture shape flag) suitable for saving or reconstruction. |
Source code in deeplens-src/deeplens/diffractive_surface/base_diffractive.py
Polynomial (Binary-2) rotationally-symmetric phase profile.
deeplens.diffractive_surface.Binary2
Binary2(
d_next,
res=(2000, 2000),
mat="fused_silica",
wvln0=0.55,
fab_ps=0.001,
fab_step=16,
is_square=True,
device="cpu",
)
Bases: DiffractiveSurface
Binary2 (Zemax-style) rotationally symmetric DOE surface.
Parameterizes the design-wavelength phase as an even polynomial in the
radial coordinate, \(\phi(r) = \pi \sum_{i=1}^{5} \alpha_{2i}\, r^{2i}\),
with coefficients alpha2, alpha4, alpha6, alpha8, alpha10. The
radial grid is cached so only the five scalar coefficients are optimized.
Attributes:
| Name | Type | Description |
|---|---|---|
alpha2 |
Tensor
|
Coefficient of \(r^2\). Scalar tensor, shape [1]. |
alpha4 |
Tensor
|
Coefficient of \(r^4\). Scalar tensor, shape [1]. |
alpha6 |
Tensor
|
Coefficient of \(r^6\). Scalar tensor, shape [1]. |
alpha8 |
Tensor
|
Coefficient of \(r^8\). Scalar tensor, shape [1]. |
alpha10 |
Tensor
|
Coefficient of \(r^{10}\). Scalar tensor, shape [1]. |
x |
Tensor
|
Pixel x-coordinates. [H, W]. [mm] |
y |
Tensor
|
Pixel y-coordinates. [H, W]. [mm] |
r2 |
Tensor
|
Cached squared radius \(x^2 + y^2\). [H, W]. [mm^2] |
Initialize a Binary2 DOE with small random polynomial coefficients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
res
|
tuple or int
|
Resolution as (H, W); an int is expanded to (res, res). [pixel]. Defaults to (2000, 2000). |
(2000, 2000)
|
mat
|
str
|
DOE material name. Defaults to "fused_silica". |
'fused_silica'
|
wvln0
|
float
|
Design wavelength. [um]. Defaults to 0.55. |
0.55
|
fab_ps
|
float
|
Fabrication pixel size. [mm]. Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication quantization levels. Defaults to 16. |
16
|
is_square
|
bool
|
Whether the aperture is square. Defaults to True. |
True
|
device
|
str
|
Device to store tensors on. Defaults to "cpu". |
'cpu'
|
Source code in deeplens-src/deeplens/diffractive_surface/binary2.py
init_from_dict
classmethod
Initialize a Binary2 DOE from a serialized surface dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Surface dict. Requires keys "d_next" and "res"; optional keys "mat", "wvln0", "fab_ps", "fab_step", "is_square" fall back to the constructor defaults. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
doe |
Binary2
|
The constructed Binary2 surface. |
Source code in deeplens-src/deeplens/diffractive_surface/binary2.py
phase_func
Compute the raw (unwrapped) phase at the design wavelength.
Evaluates \(\phi(r) = \pi\,(\alpha_2 r^2 + \alpha_4 r^4 + \alpha_6 r^6 + \alpha_8 r^8 + \alpha_{10} r^{10})\) via Horner's method on the cached \(r^2\) grid.
Returns:
| Name | Type | Description |
|---|---|---|
phase |
Tensor
|
Raw phase map. [H, W]. [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/binary2.py
get_optimizer_params
Enable gradients and build per-coefficient optimizer parameter groups.
Higher-order coefficients use progressively larger learning rates
(lr, 10x, 100x, 1000x, 10000x for alpha2 through alpha10) to
compensate for their smaller magnitude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Base learning rate for |
0.001
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer_params |
list
|
List of parameter-group dicts, one per coefficient, each with keys "params" and "lr". |
Source code in deeplens-src/deeplens/diffractive_surface/binary2.py
surf_dict
Serialize the surface to a dict, including the polynomial coefficients.
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Base surface dict extended with the five rounded coefficients "alpha2", "alpha4", "alpha6", "alpha8", "alpha10". |
Source code in deeplens-src/deeplens/diffractive_surface/binary2.py
Free-form, per-pixel phase map.
deeplens.diffractive_surface.Pixel2D
Pixel2D(
d_next,
phase_map_path=None,
res=(2000, 2000),
mat="fused_silica",
wvln0=0.55,
fab_ps=0.001,
fab_step=16,
device="cpu",
)
Bases: DiffractiveSurface
Pixel2D DOE parameterization with a direct, per-pixel phase map.
Each pixel of the phase map is an independent optimizable parameter, giving
the most general (and highest-dimensional) DOE parameterization. The phase
map is stored at the design wavelength wvln0.
Attributes:
| Name | Type | Description |
|---|---|---|
phase_map |
Tensor
|
Per-pixel phase at the design wavelength. [H, W]. [rad] |
Initialize a Pixel2D DOE where each pixel is an independent parameter.
If phase_map_path is None the phase map is initialized to small random
values (torch.randn * 1e-3); otherwise it is loaded from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
phase_map_path
|
str or None
|
Path to a saved phase-map tensor to load. If None, the phase map is randomly initialized. Defaults to None. |
None
|
res
|
tuple or int
|
Resolution of the DOE as (H, W); an int is expanded to (res, res). [pixel]. Defaults to (2000, 2000). |
(2000, 2000)
|
mat
|
str
|
Material of the DOE. Defaults to "fused_silica". |
'fused_silica'
|
wvln0
|
float
|
Design wavelength. [um]. Defaults to 0.55. |
0.55
|
fab_ps
|
float
|
Fabrication pixel size. [mm]. Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication quantization levels. Defaults to 16. |
16
|
device
|
str
|
Device to run the DOE. Defaults to "cpu". |
'cpu'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in deeplens-src/deeplens/diffractive_surface/pixel2d.py
init_from_dict
classmethod
Initialize a Pixel2D DOE from a dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Surface dict with keys "d_next" and "res" required and optional keys "mat", "fab_ps", "fab_step", "phase_map_path", "wvln0". |
required |
Returns:
| Name | Type | Description |
|---|---|---|
doe |
Pixel2D
|
The constructed Pixel2D DOE. |
Source code in deeplens-src/deeplens/diffractive_surface/pixel2d.py
phase_func
Return the raw per-pixel phase map at the design wavelength.
Returns:
| Name | Type | Description |
|---|---|---|
phase_map |
Tensor
|
Per-pixel phase at the design wavelength. [H, W]. [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/pixel2d.py
get_optimizer_params
Get optimizer parameter groups for the phase map.
Enables gradients on the phase map and returns it as a single Adam-style parameter group with the given learning rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Learning rate for the phase map. Defaults to 0.01. |
0.01
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer_params |
list
|
List with one parameter-group dict {"params": [phase_map], "lr": lr}. |
Source code in deeplens-src/deeplens/diffractive_surface/pixel2d.py
surf_dict
Return a serializable surface dict and save the phase map to disk.
Extends the base surface dict with the phase-map path, and writes the
detached CPU phase-map tensor to phase_map_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phase_map_path
|
str
|
Path to which the phase-map tensor is saved and which is recorded in the returned dict. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Surface dict including the "phase_map_path" entry. |
Source code in deeplens-src/deeplens/diffractive_surface/pixel2d.py
Fresnel-lens (quadratic) phase profile.
deeplens.diffractive_surface.Fresnel
Fresnel(
d_next,
f0=None,
wvln0=0.55,
res=(2000, 2000),
mat="fused_silica",
fab_ps=0.001,
fab_step=16,
device="cpu",
)
Bases: DiffractiveSurface
Phase-Fresnel diffractive lens surface.
A diffractive Fresnel lens with an ideal quadratic (thin-lens) phase profile.
It exhibits inverse dispersion compared to a refractive lens, and its only
free parameter is the design-wavelength focal length f0.
Attributes:
| Name | Type | Description |
|---|---|---|
f0 |
Tensor
|
Design-wavelength focal length, scalar. [mm] |
r2 |
Tensor
|
Cached squared radial coordinate grid \(x^2 + y^2\), shape [H, W]. [mm^2] |
Initialize a phase-Fresnel diffractive lens.
The lens applies an ideal thin-lens quadratic phase set by f0. It shows
inverse dispersion compared to a refractive lens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
f0
|
float or None
|
Design-wavelength focal length. [mm] If None, initialized to a random near-infinite value. Defaults to None. |
None
|
wvln0
|
float
|
Design wavelength. [um] Defaults to 0.55. |
0.55
|
res
|
tuple or int
|
Resolution of the DOE, [w, h]. [pixel] Defaults to (2000, 2000). |
(2000, 2000)
|
mat
|
str
|
Material of the DOE. Defaults to "fused_silica". |
'fused_silica'
|
fab_ps
|
float
|
Fabrication pixel size. [mm] Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication quantization steps. Defaults to 16. |
16
|
device
|
str
|
Device to run the DOE. Defaults to "cpu". |
'cpu'
|
Source code in deeplens-src/deeplens/diffractive_surface/fresnel.py
init_from_dict
classmethod
Initialize a Fresnel DOE from a dictionary of surface parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Surface parameters. Requires "d_next" and "res"; optionally "f0", "wvln0", "mat", "fab_ps", "fab_step". |
required |
Returns:
| Name | Type | Description |
|---|---|---|
doe |
Fresnel
|
The constructed Fresnel DOE. |
Source code in deeplens-src/deeplens/diffractive_surface/fresnel.py
phase_func
Compute the raw (unwrapped) quadratic phase at the design wavelength.
Applies the ideal thin-lens phase
where \(\lambda_0\) is the design wavelength converted to mm. Emits a one-time warning if the phase is undersampled on the current grid.
Returns:
| Name | Type | Description |
|---|---|---|
phase |
Tensor
|
Raw unwrapped phase, shape [H, W]. [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/fresnel.py
get_optimizer_params
Build optimizer parameter groups for the focal length f0.
Enables gradients on f0 and returns it as a single parameter group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Learning rate for |
0.001
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer_params |
list
|
List with one parameter group dict for |
Source code in deeplens-src/deeplens/diffractive_surface/fresnel.py
surf_dict
Serialize the surface to a dictionary, including f0 and wvln0.
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Base surface parameters plus "f0" [mm], with "wvln0" [um] overwritten by the unrounded value. |
Source code in deeplens-src/deeplens/diffractive_surface/fresnel.py
Phase parameterized by Zernike polynomials.
deeplens.diffractive_surface.Zernike
Zernike(
d_next,
z_coeff=None,
zernike_order=37,
res=(2000, 2000),
mat="fused_silica",
fab_ps=0.001,
fab_step=16,
wvln0=0.55,
device="cpu",
)
Bases: DiffractiveSurface
Diffractive optical element parameterized by Zernike polynomials.
The DOE surface phase is represented as a weighted sum of the first 37
Zernike polynomials (OSA/ANSI ordering) over the unit disk. The learnable
coefficients z_coeff are the only optimized parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
zernike_order |
int
|
Number of Zernike terms (fixed at 37). |
z_coeff |
Tensor
|
Zernike coefficients, shape (zernike_order,). |
Initialize a Zernike-parameterized DOE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
z_coeff
|
Tensor or None
|
Zernike coefficients of shape (zernike_order,). If None, initialized to random values scaled by 1e-3. Defaults to None. |
None
|
zernike_order
|
int
|
Number of Zernike coefficients. Only 37 is currently supported. Defaults to 37. |
37
|
res
|
tuple
|
DOE resolution as (H, W) in pixels. Defaults to (2000, 2000). |
(2000, 2000)
|
mat
|
str
|
DOE substrate material. Defaults to "fused_silica". |
'fused_silica'
|
fab_ps
|
float
|
Fabrication pixel size. [mm] Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication quantization levels. Defaults to 16. |
16
|
wvln0
|
float
|
Design wavelength. [um] Defaults to 0.55. |
0.55
|
device
|
str
|
Computation device. Defaults to "cpu". |
'cpu'
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If zernike_order is not 37. |
Source code in deeplens-src/deeplens/diffractive_surface/zernike.py
init_from_dict
classmethod
Initialize a Zernike DOE from a serialized surface dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Surface parameters. Requires "d_next" and "res"; optional keys "mat", "fab_ps", "fab_step", "z_coeff", "zernike_order", "wvln0" fall back to their defaults when absent. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
zernike |
Zernike
|
The constructed Zernike DOE. |
Source code in deeplens-src/deeplens/diffractive_surface/zernike.py
phase_func
Compute the DOE phase map at the design wavelength.
Returns:
| Name | Type | Description |
|---|---|---|
phase |
Tensor
|
Phase map of shape (res[0], res[0]) in radians, evaluated from the Zernike coefficients over the unit disk. |
Source code in deeplens-src/deeplens/diffractive_surface/zernike.py
get_optimizer_params
Build optimizer parameter groups for the Zernike coefficients.
Sets z_coeff to require gradients as a side effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Learning rate for the coefficients. Defaults to 0.01. |
0.01
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer_params |
list
|
A single parameter group dict with keys
"params" (the |
Source code in deeplens-src/deeplens/diffractive_surface/zernike.py
surf_dict
Serialize the DOE surface to a dict.
Extends the base surface dict with the Zernike coefficients (moved to CPU and detached) and the Zernike order.
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Surface parameters including "z_coeff" and "zernike_order". |
Source code in deeplens-src/deeplens/diffractive_surface/zernike.py
Linear / blazed grating phase.
deeplens.diffractive_surface.Grating
Grating(
d_next,
res=(2000, 2000),
mat="fused_silica",
wvln0=0.55,
fab_ps=0.001,
fab_step=16,
theta=0.0,
alpha=0.0,
device="cpu",
)
Bases: DiffractiveSurface
Linear grating diffractive optical element.
A grating introduces a linear phase gradient across the surface, which diffracts light into multiple diffraction orders. The phase profile is
where \(\theta\) is the angle from the y-axis to the grating vector,
\(\alpha\) is the grating slope (phase-gradient strength), and norm_radii
normalizes the coordinates.
Attributes:
| Name | Type | Description |
|---|---|---|
theta |
Tensor
|
Angle from the y-axis to the grating vector. [rad] |
alpha |
Tensor
|
Grating slope (phase-gradient strength). [rad] |
norm_radii |
float
|
Coordinate normalization radius (half the DOE width). [mm] |
Initialize a grating DOE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_next
|
float
|
Axial thickness to the next plane. [mm] |
required |
res
|
tuple or int
|
Resolution of the DOE as (H, W); an int is expanded to (res, res). [pixel]. Defaults to (2000, 2000). |
(2000, 2000)
|
mat
|
str
|
Material name of the DOE. Defaults to "fused_silica". |
'fused_silica'
|
wvln0
|
float
|
Design wavelength. [um]. Defaults to 0.55. |
0.55
|
fab_ps
|
float
|
Fabrication pixel size. [mm]. Defaults to 0.001. |
0.001
|
fab_step
|
int
|
Number of fabrication (quantization) levels. Defaults to 16. |
16
|
theta
|
float
|
Angle from the y-axis to the grating vector. [rad]. Defaults to 0.0. |
0.0
|
alpha
|
float
|
Grating slope (phase-gradient strength). [rad]. Defaults to 0.0. |
0.0
|
device
|
str
|
Device to place the DOE tensors on. Defaults to "cpu". |
'cpu'
|
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
init_from_dict
classmethod
Initialize a grating DOE from a parameter dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doe_dict
|
dict
|
Dictionary of DOE parameters. Requires keys "d_next" and "res"; "mat", "wvln0", "fab_ps", "fab_step", "theta", and "alpha" are optional and fall back to defaults. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
grating |
Grating
|
The constructed grating DOE instance. |
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
phase_func
Compute the raw grating phase profile at the design wavelength.
The phase is a linear function of position:
Returns:
| Name | Type | Description |
|---|---|---|
phase |
Tensor
|
Raw, unwrapped phase profile at the design wavelength. [H, W]. [rad] |
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
get_optimizer_params
Build optimizer parameter groups for the grating parameters.
Enables gradients on theta and alpha. The alpha group uses a
learning rate scaled by 10x relative to lr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lr
|
float
|
Base learning rate for the grating parameters. Defaults to 0.001. |
0.001
|
Returns:
| Name | Type | Description |
|---|---|---|
optimizer_params |
list
|
List of parameter-group dicts for the optimizer. |
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
surf_dict
Return a serializable dict of the grating surface parameters.
Extends the base surface dict with the grating-specific theta,
alpha, and norm_radii entries.
Returns:
| Name | Type | Description |
|---|---|---|
surf_dict |
dict
|
Dictionary of surface parameters. |
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
save_ckpt
Save the grating DOE parameters to a checkpoint file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_path
|
str
|
Path to write the checkpoint to. Defaults to "./grating_doe.pth". |
'./grating_doe.pth'
|
Source code in deeplens-src/deeplens/diffractive_surface/grating.py
load_ckpt
Load the grating DOE parameters from a checkpoint file.
Restores theta and alpha onto the current device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_path
|
str
|
Path to read the checkpoint from. Defaults to "./grating_doe.pth". |
'./grating_doe.pth'
|