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="mlpconv",
kernel_size=128,
dtype=torch.float32,
primary_wvln=DEFAULT_WAVE,
wvln_rgb=WAVE_RGB,
obj_depth=DEPTH,
)
Bases: Lens
Neural surrogate lens that predicts PSFs via an MLP/MLPConv network.
Wraps a GeoLens with a neural network trained to predict 3-channel RGB
PSFs from (fov, depth, foc_dist) inputs. After training, PSF prediction
is much faster than ray tracing, making it suitable for real-time
applications and large-scale optimization.
Use train_psfnet to train the surrogate from ray-traced PSF samples, or
load_net to load pre-trained weights.
Attributes:
| Name | Type | Description |
|---|---|---|
lens |
GeoLens
|
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. |
foclen |
float
|
Focal length [mm], copied from the embedded lens. |
rfov |
float
|
Real half-diagonal field of view [radians]. |
kernel_size |
int
|
Side length of the network's native PSF kernel [pixels]. |
d_close |
float
|
Near object depth bound for training [mm] (negative). |
d_far |
float
|
Far object depth bound for training [mm] (negative). |
foc_d_close |
float
|
Near focus-distance bound [mm] (negative). |
foc_d_far |
float
|
Far focus-distance bound [mm] (negative). |
foc_dist |
float
|
Current focus distance [mm] (negative). |
Initialize a PSF network lens.
Loads the embedded GeoLens, builds the PSF network, and focuses the
lens to infinity. In the default settings, the network takes
(fov, depth, foc_dist) as input and outputs a 3-channel RGB PSF along
the y-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lens_path
|
str
|
Path to the lens file. |
required |
in_chan
|
int
|
Number of input channels. Defaults to 3. |
3
|
psf_chan
|
int
|
Number of output PSF channels. Defaults to 3. |
3
|
model_name
|
str
|
Network architecture, "mlp" or "mlpconv". Defaults to "mlpconv". |
'mlpconv'
|
kernel_size
|
int
|
Side length of the predicted PSF kernel [pixels]. Defaults to 128. |
128
|
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 [R, G, B] in µm. Defaults to WAVE_RGB. |
WAVE_RGB
|
obj_depth
|
float
|
Default object depth [mm], used when a method is called without an explicit depth. Defaults to DEPTH. |
DEPTH
|
Source code in deeplens-src/deeplens/psfnetlens.py
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 | |
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 and return a PSF network.
The network maps an input of shape [B, in_chan] (the scaled
(fov, depth, foc_dist) features) to a PSF kernel of shape
[B, psf_chan, kernel_size, kernel_size].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_chan
|
int
|
Number of input channels. Defaults to 2. |
2
|
psf_chan
|
int
|
Number of output PSF channels. Defaults to 3. |
3
|
kernel_size
|
int
|
Side length of the PSF kernel [pixels]. Defaults to 64. |
64
|
model_name
|
str
|
Network architecture, "mlp" or "mlpconv". Defaults to "mlpconv". |
'mlpconv'
|
Returns:
| Name | Type | Description |
|---|---|---|
psfnet |
Module
|
The constructed PSF network. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Source code in deeplens-src/deeplens/psfnetlens.py
load_net
Load pretrained PSF network weights from disk.
Prints the pixel size and lens path stored in the checkpoint alongside the
current values so a mismatch can be spotted, then loads the weights into
self.psfnet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_path
|
str
|
Path to the saved checkpoint file. |
required |
trusted
|
bool
|
Permit unrestricted legacy pickle loading. Defaults to False. Only enable for artifacts with controlled, verified provenance. |
False
|
Source code in deeplens-src/deeplens/psfnetlens.py
save_psfnet
Save the PSF network and its metadata to disk.
Stores the network weights along with the model name, channel counts, kernel size, pixel size, and lens path so the checkpoint is self-describing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psfnet_path
|
str
|
Path to save the checkpoint file. |
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.
Samples ray-traced PSFs as supervision, optimizes the network with an
L1 loss and AdamW under a cosine schedule with warmup, and periodically
saves a GT/prediction comparison figure and the latest checkpoint to
result_dir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iters
|
int
|
Number of training iterations. Defaults to 100000. |
100000
|
bs
|
int
|
Batch size. Defaults to 128. |
128
|
lr
|
float
|
Learning rate. Defaults to 5e-5. |
5e-05
|
evaluate_every
|
int
|
Evaluate and checkpoint every this many iterations. Defaults to 500. |
500
|
spp
|
int
|
Samples per pixel (currently unused). Defaults to 16384. |
16384
|
concentration_factor
|
float
|
Controls how tightly depths are sampled around the focus distance during data generation. Defaults to 2.0. |
2.0
|
result_dir
|
str
|
Directory to save figures and checkpoints. Defaults to "./results/psfnet". |
'./results/psfnet'
|
Source code in deeplens-src/deeplens/psfnetlens.py
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
sample_training_data
Sample a batch of training data for the PSF surrogate network.
Draws one focus distance per call (beta-biased towards the near bound),
samples fovs (beta-biased towards rfov), and samples depths
concentrated around the focus distance, then ray-traces the RGB PSF for
each sampled point. Depth and focus distance in the returned input are
scaled by 1/1000 (i.e. expressed in metres).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_points
|
int
|
Number of training points (batch size). Defaults to 512. |
512
|
concentration_factor
|
float
|
Controls how tightly depths are sampled around the focus distance; larger values sample more tightly. Defaults to 2.0. |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
sample_input |
Tensor
|
Shape [num_points, 3], columns
|
sample_psf |
Tensor
|
Ray-traced RGB PSFs, shape [num_points, 3, kernel_size, kernel_size]. |
Source code in deeplens-src/deeplens/psfnetlens.py
eval
Switch the PSF surrogate network to evaluation mode.
Disables dropout and batch-norm updates in the internal psfnet
module. Call this before inference.
points2input
Convert point-source coordinates to the network input tensor.
Maps normalized sensor-plane coordinates to a field angle, pairs them with depth and the current focus distance, and scales depth and focus distance by 1/1000 (i.e. into metres) to match the training inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Shape [N, 3]. Columns are normalized x and y in [-1, 1] (fraction of the half sensor size) and depth [mm]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
network_inp |
Tensor
|
Shape [N, 3], columns
|
Source code in deeplens-src/deeplens/psfnetlens.py
refocus
Refocus the lens to a given object distance.
Delegates to the embedded GeoLens and caches the focus distance in
self.foc_dist for subsequent PSF predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foc_dist
|
float
|
Focus distance [mm] (negative, towards the object). |
required |
Source code in deeplens-src/deeplens/psfnetlens.py
psf
Compute the monochromatic PSF from the RGB surrogate network.
PSFNetLens is RGB-native: the network predicts a 3-channel PSF in a
single pass, so the monochromatic PSF returns the RGB channel whose
design wavelength (self.wvln_rgb) is closest to wvln.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point source coordinates, shape [N, 3] or [3]. |
required |
wvln
|
float
|
Wavelength [µm]. When None (default), falls
back to |
None
|
ks
|
int
|
Output kernel size [pixels]. Defaults to PSF_KS. |
PSF_KS
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
PSF, shape [ks, ks] for a single point or [N, ks, ks] for a batch. |
Source code in deeplens-src/deeplens/psfnetlens.py
psf_rgb
Compute the RGB PSF for a batch of point sources via the network.
The network predicts PSFs along the y-axis; each predicted PSF is rotated
by atan2(x, y) to the point's azimuth and then center-cropped to ks if
ks is smaller than the network's native kernel size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Shape [N, 3]. Columns are normalized x and y in [-1, 1] (fraction of the half sensor size) and depth [mm]. |
required |
ks
|
int
|
Output kernel size [pixels]. Defaults to PSF_KS. |
PSF_KS
|
**kwargs
|
Accepted for API compatibility; unused. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
RGB PSFs, shape [N, 3, ks, ks]. |
Source code in deeplens-src/deeplens/psfnetlens.py
psf_map_rgb
Compute an RGB PSF map over a grid of field points.
Builds a grid of point sources at the given depth and evaluates the RGB PSF at each grid location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid size as |
(11, 11)
|
depth
|
float
|
Object depth [mm]. When None (default), falls
back to |
None
|
ks
|
int
|
Kernel size [pixels]. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map |
Tensor
|
Shape [grid_h, grid_w, 3, ks, ks]. |
Source code in deeplens-src/deeplens/psfnetlens.py
render_rgbd
Render a defocused image from an all-in-focus image and depth map.
Refocuses the lens to foc_dist, predicts a per-pixel RGB PSF from the
per-pixel field position and depth, then splats those PSFs onto the input
image. Only batch size 1 is supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Tensor
|
All-in-focus image, shape [1, C, H, W]. |
required |
depth
|
Tensor
|
Depth map [mm], shape [1, H, W] (negative depths). |
required |
foc_dist
|
Tensor
|
Focus distance [mm], shape [1] (negative). |
required |
ks
|
int
|
PSF kernel size [pixels]. Defaults to 64. |
64
|
high_res
|
bool
|
If True, splat in tiles to reduce memory use. Defaults to False. |
False
|
chunk_size
|
int
|
Tile size used when |
256
|
Returns:
| Name | Type | Description |
|---|---|---|
render |
Tensor
|
Rendered image, shape [1, C, H, W]. |