PSFNetLens
Neural surrogate that wraps a GeoLens with an MLP to predict
PSFs. It provides fast, differentiable PSF evaluation during end-to-end training,
trading a one-time fitting cost for cheap inference. See also
Surrogate Networks.
deeplens.PSFNetLens
PSFNetLens(lens_path, in_chan=3, psf_chan=3, model_name='mlp_conv', kernel_size=64, dtype=torch.float32, primary_wvln=DEFAULT_WAVE, wvln_rgb=WAVE_RGB, obj_depth=DEPTH)
Bases: Lens
Neural surrogate lens that predicts PSFs via a small MLP/MLPConv network.
Wraps a GeoLens with a neural network
trained to predict RGB PSFs from (fov, depth, focus_distance) inputs.
After training, PSF prediction is ~100× faster than ray tracing, making
it suitable for real-time applications and large-scale optimisation.
Attributes:
| Name | Type | Description |
|---|---|---|
lens |
GeoLens
|
The underlying refractive lens (used for training data generation and for sensor metadata). |
psfnet |
Module
|
Neural network for PSF prediction. |
pixel_size |
float
|
Pixel pitch [mm] (copied from the embedded lens). |
rfov |
float
|
Half-diagonal field of view [radians]. |
Notes
Use train_psfnet to train the surrogate from ray-traced PSF
samples. Use load_net to load pre-trained weights.
Initialize a PSF network lens.
In the default settings, the PSF network takes (fov, depth, foc_dist) as input and outputs RGB PSF on y-axis at (fov, depth, foc_dist).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lens_path
|
str
|
Path to the lens file. |
required |
in_chan
|
int
|
Number of input channels. |
3
|
psf_chan
|
int
|
Number of output channels. |
3
|
model_name
|
str
|
Name of the model. |
'mlp_conv'
|
kernel_size
|
int
|
Kernel size. |
64
|
dtype
|
dtype
|
Data type for computations. Defaults to torch.float32. |
float32
|
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 computations, ordered |
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/psfnetlens.py
set_sensor_res
Set sensor resolution for both PSFNetLens and the embedded GeoLens.
Updates the pixel size accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensor_res
|
tuple
|
New sensor resolution as |
required |
Source code in deeplens-src/deeplens/psfnetlens.py
init_net
Initialize a PSF network.
PSF network
Input: [B, 3], (fov, depth, foc_dist). fov from [0, pi/2], depth from [-20000, -100], foc_dist from [-20000, -500] Output: psf kernel [B, 3, ks, ks]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_chan
|
int
|
number of input channels |
2
|
psf_chan
|
int
|
number of output channels |
3
|
kernel_size
|
int
|
kernel size |
64
|
model_name
|
str
|
name of the network architecture |
'mlpconv'
|
Returns:
| Name | Type | Description |
|---|---|---|
psfnet |
Module
|
network |
Source code in deeplens-src/deeplens/psfnetlens.py
load_net
Load pretrained network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_path
|
str
|
path to load the network |
required |
Source code in deeplens-src/deeplens/psfnetlens.py
save_psfnet
Save the PSF network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psfnet_path
|
str
|
path to save the PSF network |
required |
Source code in deeplens-src/deeplens/psfnetlens.py
train_psfnet
train_psfnet(iters=100000, bs=128, lr=5e-05, evaluate_every=500, spp=16384, concentration_factor=2.0, result_dir='./results/psfnet')
Train the PSF surrogate network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iters
|
int
|
number of training iterations |
100000
|
bs
|
int
|
batch size |
128
|
lr
|
float
|
learning rate |
5e-05
|
evaluate_every
|
int
|
evaluate every how many iterations |
500
|
spp
|
int
|
number of samples per pixel |
16384
|
concentration_factor
|
float
|
concentration factor for training data sampling |
2.0
|
result_dir
|
str
|
directory to save the results |
'./results/psfnet'
|
Source code in deeplens-src/deeplens/psfnetlens.py
sample_training_data
Sample training data for PSF surrogate network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_points
|
int
|
number of training points |
512
|
concentration_factor
|
float
|
concentration factor for training data sampling |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
sample_input |
tensor
|
[B, 3] tensor, (fov, depth, foc_dist). - fov from [0, rfov] on 0y-axis, [radians] - depth from [d_far, d_close], [mm] - foc_dist from [foc_d_far, foc_d_close], [mm] - We use absolute fov and depth. |
sample_psf |
tensor
|
[B, 3, ks, ks] tensor |
Source code in deeplens-src/deeplens/psfnetlens.py
eval
Switch the PSF surrogate network to evaluation mode.
Disables dropout and batch normalisation updates in the internal
psfnet module. Call this before inference.
points2input
Convert points to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
tensor
|
[N, 3] tensor, [-1, 1] * [-1, 1] * [depth_min, depth_max] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
input |
tensor
|
[N, 3] tensor, (fov, depth, foc_dist). - fov from [0, rfov] on y-axis, [radians] - depth/1000.0 from [d_far, d_close], [mm] - foc_dist/1000.0 from [foc_d_far, foc_d_close], [mm] |
Source code in deeplens-src/deeplens/psfnetlens.py
refocus
Refocus the lens to a given object distance.
Delegates to the embedded GeoLens and stores the focus
distance for subsequent PSF predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foc_dist
|
float
|
Focus distance in [mm] (negative, towards the object). |
required |
Source code in deeplens-src/deeplens/psfnetlens.py
psf_rgb
Calculate RGB PSF using the PSF network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
tensor
|
[N, 3] tensor, [-1, 1] * [-1, 1] * [depth_min, depth_max] |
required |
foc_dist
|
float
|
focus distance |
required |
Returns:
| Name | Type | Description |
|---|---|---|
psf |
tensor
|
[N, 3, ks, ks] tensor |
Source code in deeplens-src/deeplens/psfnetlens.py
psf_map_rgb
Compute monochrome PSF map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid size. Defaults to (11, 11), meaning 11x11 grid. |
(11, 11)
|
wvln
|
float
|
Wavelength. Defaults to DEFAULT_WAVE. |
required |
depth
|
float
|
Depth of the object. When |
None
|
ks
|
int
|
Kernel size. Defaults to PSF_KS, meaning PSF_KS x PSF_KS kernel size. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map |
Shape of [grid, grid, 3, ks, ks]. |
Source code in deeplens-src/deeplens/psfnetlens.py
render_rgbd
Render image with aif image and depth map. Receive [N, C, H, W] image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
tensor
|
[1, C, H, W] |
required |
depth
|
tensor
|
[1, H, W], depth map, unit in mm, range from [-20000, -200] |
required |
foc_dist
|
tensor
|
[1], unit in mm, range from [-20000, -200] |
required |
ks
|
int
|
kernel size |
64
|
high_res
|
bool
|
whether to use high resolution rendering |
False
|
chunk_size
|
int
|
tile size used when |
256
|
Returns:
| Name | Type | Description |
|---|---|---|
render |
tensor
|
[1, C, H, W] |