Lens
Abstract base class for every lens model in DeepLens. Lens defines the shared
interface — psf(), psf_rgb(), render(), sensor configuration, and file
I/O — that GeoLens, HybridLens,
DiffractiveLens, PSFNetLens, and
DefocusLens all inherit.
deeplens.Lens
Lens(dtype=torch.float32, device=None, primary_wvln=DEFAULT_WAVE, wvln_rgb=WAVE_RGB, obj_depth=DEPTH)
Bases: DeepObj
Initialize a lens class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
dtype
|
Data type. Defaults to torch.float32. |
float32
|
device
|
str
|
Device to run the lens. Defaults to None. |
None
|
primary_wvln
|
float
|
Primary design wavelength [µm].
Used as fallback when a method is called without an explicit
|
DEFAULT_WAVE
|
wvln_rgb
|
sequence of float
|
Three wavelengths used for
RGB (polychromatic) computations, ordered |
WAVE_RGB
|
obj_depth
|
float
|
Default object depth [mm] used as
fallback when a method is called without an explicit
|
DEPTH
|
Source code in deeplens-src/deeplens/lens.py
read_lens_json
write_lens_json
set_sensor
Set sensor size and resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensor_size
|
tuple
|
Sensor size (w, h) in [mm]. |
required |
sensor_res
|
tuple
|
Sensor resolution (W, H) in [pixels]. |
required |
Source code in deeplens-src/deeplens/lens.py
set_sensor_res
Set sensor resolution (and aspect ratio) while keeping sensor radius unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensor_res
|
tuple
|
Sensor resolution (W, H) in [pixels]. |
required |
Source code in deeplens-src/deeplens/lens.py
calc_fov
Compute FoV (radian) of the lens.
Reference
[1] https://en.wikipedia.org/wiki/Angle_of_view_(photography)
Source code in deeplens-src/deeplens/lens.py
psf
Compute the monochromatic PSF for one or more point sources.
Subclasses must override this method with a differentiable implementation. Three computation models are common in practice: geometric ray binning, coherent ray-wave, and Huygens spherical-wave integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point source coordinates, shape |
required |
wvln
|
float
|
Wavelength in micrometers. When |
None
|
ks
|
int
|
Output PSF kernel size in pixels. Defaults
to |
PSF_KS
|
**kwargs
|
Additional keyword arguments forwarded to the underlying
PSF computation (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: PSF intensity map, shape |
|
|
point or |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This base implementation must be overridden. |
Notes
The method is differentiable with respect to all optimisable lens parameters so it can be used directly inside a training loop.
Example
point = torch.tensor([0.0, 0.0, -10000.0]) psf = lens.psf(points=point, ks=64, model="geometric") print(psf.shape) # torch.Size([64, 64])
Source code in deeplens-src/deeplens/lens.py
psf_rgb
Compute the RGB (tri-chromatic) PSF by stacking three wavelength calls.
Calls psf three times for the RGB primary wavelengths stored
in self.wvln_rgb and stacks the results along the channel axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point source coordinates, shape |
required |
ks
|
int
|
PSF kernel size. Defaults to |
PSF_KS
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: RGB PSF, shape |
|
|
or |
Source code in deeplens-src/deeplens/lens.py
point_source_grid
Generate point source grid for PSF calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
float
|
Depth of the point source. |
required |
grid
|
tuple
|
Grid size (grid_w, grid_h). Defaults to (9, 9), meaning 9x9 grid. |
(9, 9)
|
normalized
|
bool
|
Return normalized object source coordinates. Defaults to True, meaning object sources xy coordinates range from [-1, 1]. |
True
|
quater
|
bool
|
Use quater of the sensor plane to save memory. Defaults to False. |
False
|
center
|
bool
|
Use center of each patch. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
point_source |
Normalized object source coordinates. Shape of [grid_h, grid_w, 3], [-1, 1], [-1, 1], [-Inf, 0]. |
Source code in deeplens-src/deeplens/lens.py
psf_map
Compute monochrome PSF map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid size (grid_w, grid_h). Defaults to (5, 5), meaning 5x5 grid. |
(5, 5)
|
wvln
|
float
|
Wavelength in µm. When |
None
|
depth
|
float
|
Depth of the object. When |
None
|
ks
|
int
|
Kernel size. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map |
Shape of [grid_h, grid_w, 3, ks, ks]. |
Source code in deeplens-src/deeplens/lens.py
psf_map_rgb
Compute RGB PSF map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid size (grid_w, grid_h). Defaults to (5, 5), meaning 5x5 grid. |
(5, 5)
|
ks
|
int
|
Kernel size. Defaults to PSF_KS, meaning PSF_KS x PSF_KS kernel size. |
PSF_KS
|
depth
|
float
|
Depth of the object. When |
None
|
**kwargs
|
Additional arguments for psf_map(). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map |
Shape of [grid_h, grid_w, 3, ks, ks]. |
Source code in deeplens-src/deeplens/lens.py
draw_psf_map
draw_psf_map(grid=(7, 7), ks=PSF_KS, depth=None, log_scale=False, save_name='./psf_map.png', show=False)
Draw RGB PSF map of the lens.
Source code in deeplens-src/deeplens/lens.py
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 393 394 395 396 397 398 399 400 401 402 403 404 | |
point_source_radial
Generate radial point sources from center to edge of the field.
Produces grid evenly-spaced points along a chosen radial direction
(diagonal, meridional, or sagittal) in normalized or physical object-space
coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
float
|
Object depth (z-coordinate) in mm. |
required |
grid
|
int
|
Number of sample points. Defaults to 9. |
9
|
center
|
bool
|
If |
False
|
direction
|
str
|
Sampling direction —
|
'diagonal'
|
normalized
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Point source positions, shape |
Source code in deeplens-src/deeplens/lens.py
draw_psf_radial
Draw radial PSF (45 deg). Will draw M PSFs, each of size ks x ks.
Source code in deeplens-src/deeplens/lens.py
render
Differentiable image simulation for a 2D (flat) scene.
Performs only the optical component of image simulation and is fully differentiable.
For incoherent imaging the intensity PSF is convolved with the object-space image. For coherent imaging the complex PSF is convolved with the complex object image before squaring for intensity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_obj
|
Tensor
|
Input image in linear (raw) space,
shape |
required |
depth
|
float
|
Object depth in mm (negative value).
When |
None
|
method
|
str
|
Rendering method. One of:
|
'psf_patch'
|
**kwargs
|
Method-specific keyword arguments:
|
{}
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Rendered image, shape |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If method is |
Exception
|
If method is not recognised. |
References
[1] "Optical Aberration Correction in Postprocessing using Imaging Simulation", TOG 2021. [2] "Efficient depth- and spatially-varying image simulation for defocus deblur", ICCVW 2025.
Example
img_rendered = lens.render(img, depth=-10000.0, method="psf_patch", ... patch_center=(0.3, 0.0), psf_ks=64)
Source code in deeplens-src/deeplens/lens.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 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 | |
render_psf
Render image patch using PSF convolution. Better not use this function to avoid confusion.
Source code in deeplens-src/deeplens/lens.py
render_psf_patch
Render an image patch using PSF convolution, and return positional encoding channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_obj
|
tensor
|
Input image object in raw space. Shape of [B, C, H, W]. |
required |
depth
|
float
|
Depth of the object. When |
None
|
patch_center
|
tensor
|
Center of the image patch. Shape of [2] or [B, 2]. |
(0, 0)
|
psf_ks
|
int
|
PSF kernel size. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
img_render |
Rendered image. Shape of [B, C, H, W]. |
Source code in deeplens-src/deeplens/lens.py
render_psf_map
Render image using PSF block convolution.
Note
Larger psf_grid and psf_ks are typically better for more accurate rendering, but slower.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_obj
|
tensor
|
Input image object in raw space. Shape of [B, C, H, W]. |
required |
depth
|
float
|
Depth of the object. When |
None
|
psf_grid
|
int
|
PSF grid size. |
7
|
psf_ks
|
int
|
PSF kernel size. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
img_render |
Rendered image. Shape of [B, C, H, W]. |
Source code in deeplens-src/deeplens/lens.py
render_rgbd
Render RGBD image.
TODO: add obstruction-aware image simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_obj
|
tensor
|
Object image. Shape of [B, C, H, W]. |
required |
depth_map
|
tensor
|
Depth map [mm]. Shape of [B, 1, H, W]. Values should be positive. |
required |
method
|
str
|
Image simulation method. Defaults to "psf_patch". |
'psf_patch'
|
**kwargs
|
Additional arguments for different methods. - interp_mode (str): "depth" or "disparity". Defaults to "depth". |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
img_render |
Rendered image. Shape of [B, C, H, W]. |
Reference
[1] "Aberration-Aware Depth-from-Focus", TPAMI 2023. [2] "Efficient Depth- and Spatially-Varying Image Simulation for Defocus Deblur", ICCVW 2025.
Source code in deeplens-src/deeplens/lens.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 | |