Network API Reference
The deeplens.network module provides neural networks for PSF prediction (surrogates) and image reconstruction, plus loss functions for training.
Surrogate Networks
Neural networks that learn to predict PSFs from lens parameters, replacing expensive ray tracing during training.
deeplens.network.MLP
Bases: Module
Fully-connected network for low-frequency PSF prediction.
Predicts PSFs as flattened vectors using stacked linear layers with ReLU activations and a Sigmoid output. The output is L1-normalized so it sums to 1 (valid as a PSF energy distribution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
Number of input features (e.g., field angle + wavelength). |
required | |
out_features
|
Number of output features (flattened PSF size). |
required | |
hidden_features
|
Width of hidden layers. Defaults to 64. |
64
|
|
hidden_layers
|
Number of hidden layers. Defaults to 3. |
3
|
Source code in deeplens/network/surrogate/mlp.py
forward
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
L1-normalized output tensor of shape |
Source code in deeplens/network/surrogate/mlp.py
deeplens.network.MLPConv
Bases: Module
MLP encoder + convolutional decoder for high-resolution PSF prediction.
Uses a linear encoder to produce a low-resolution feature map, then a transposed-convolution decoder to upsample to the target PSF resolution. The output is Sigmoid-activated and L1-normalized per spatial dimension.
Reference: "Differentiable Compound Optics and Processing Pipeline Optimization for End-To-end Camera Design".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
Number of input features (e.g., field angle + wavelength). |
required | |
ks
|
Spatial size of the output PSF. Must be a multiple of 32 if > 32. |
required | |
channels
|
Number of output channels. Defaults to 3. |
3
|
|
activation
|
Activation function, |
'relu'
|
Source code in deeplens/network/surrogate/mlpconv.py
forward
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Normalized PSF tensor of shape |
Source code in deeplens/network/surrogate/mlpconv.py
deeplens.network.surrogate.siren.Siren
Bases: Module
Single SIREN (Sinusoidal Representation Network) layer.
A linear layer followed by a sine activation. Uses the initialization scheme from "Implicit Neural Representations with Periodic Activation Functions".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim_in
|
Input dimension. |
required | |
dim_out
|
Output dimension. |
required | |
w0
|
Frequency multiplier for the sine activation. Defaults to 1.0. |
1.0
|
|
c
|
Constant for weight initialization. Defaults to 6.0. |
6.0
|
|
is_first
|
Whether this is the first layer (uses different init). Defaults to False. |
False
|
|
use_bias
|
Whether to include a bias term. Defaults to True. |
True
|
|
activation
|
Custom activation module. If None, uses |
None
|
Source code in deeplens/network/surrogate/siren.py
forward
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Output tensor of shape |
Source code in deeplens/network/surrogate/siren.py
deeplens.network.ModulateSiren
ModulateSiren(dim_in, dim_hidden, dim_out, dim_latent, num_layers, image_width, image_height, w0=1.0, w0_initial=30.0, use_bias=True, final_activation=None, outermost_linear=True)
Bases: Module
Modulated SIREN for latent-conditioned image synthesis.
Combines a SIREN synthesizer network (mapping pixel coordinates to output values) with a modulator network that conditions each layer based on a latent vector. Used to predict spatially-varying PSFs conditioned on lens parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim_in
|
Input coordinate dimension (typically 2 for x, y). |
required | |
dim_hidden
|
Hidden layer width for both synthesizer and modulator. |
required | |
dim_out
|
Output dimension per pixel (e.g., 1 for grayscale PSF). |
required | |
dim_latent
|
Dimension of the conditioning latent vector. |
required | |
num_layers
|
Number of SIREN + modulator layers. |
required | |
image_width
|
Output image width in pixels. |
required | |
image_height
|
Output image height in pixels. |
required | |
w0
|
Frequency multiplier for hidden sine layers. Defaults to 1.0. |
1.0
|
|
w0_initial
|
Frequency multiplier for the first sine layer. Defaults to 30.0. |
30.0
|
|
use_bias
|
Whether to use bias in sine layers. Defaults to True. |
True
|
|
final_activation
|
Activation for the last layer. Defaults to None (linear). |
None
|
|
outermost_linear
|
If True, the last layer is a plain linear layer. Defaults to True. |
True
|
Source code in deeplens/network/surrogate/modulate_siren.py
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 | |
forward
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent
|
Conditioning latent vector of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Output image tensor of shape |
Source code in deeplens/network/surrogate/modulate_siren.py
Reconstruction Networks
Image restoration networks that recover a clean image from a degraded (aberrated) sensor capture.
deeplens.network.NAFNet
NAFNet(in_chan=3, out_chan=3, width=32, middle_blk_num=1, enc_blk_nums=[1, 1, 1, 28], dec_blk_nums=[1, 1, 1, 1])
Bases: Module
Nonlinear Activation Free Network for image restoration.
A U-Net-style encoder-decoder with NAFBlocks that replace nonlinear activations with SimpleGate (element-wise multiplication of channel-split halves). Includes a global residual connection from input to output.
Reference: "Simple Baselines for Image Restoration" (ECCV 2022).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_chan
|
Number of input channels. Defaults to 3. |
3
|
|
out_chan
|
Number of output channels. Defaults to 3. |
3
|
|
width
|
Base channel width. Defaults to 32. |
32
|
|
middle_blk_num
|
Number of NAFBlocks in the bottleneck. Defaults to 1. |
1
|
|
enc_blk_nums
|
Number of NAFBlocks per encoder stage. Defaults to |
[1, 1, 1, 28]
|
|
dec_blk_nums
|
Number of NAFBlocks per decoder stage. Defaults to |
[1, 1, 1, 1]
|
Source code in deeplens/network/reconstruction/nafnet.py
forward
Forward pass with global residual connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Input image tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Restored image tensor of shape |
Source code in deeplens/network/reconstruction/nafnet.py
deeplens.network.UNet
Bases: Module
U-Net with residual skip connections for image restoration.
A 3-level encoder-decoder with dense BasicBlocks and PixelShuffle upsampling. Uses additive skip connections between encoder and decoder stages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
Number of input channels. Defaults to 3. |
3
|
|
out_channels
|
Number of output channels. Defaults to 3. |
3
|
Source code in deeplens/network/reconstruction/unet.py
forward
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Input image tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Output tensor of shape |
Source code in deeplens/network/reconstruction/unet.py
deeplens.network.Restormer
Restormer(inp_channels=3, out_channels=3, dim=48, num_blocks=[4, 6, 6, 8], num_refinement_blocks=4, heads=[1, 2, 4, 8], ffn_expansion_factor=2.66, bias=False, LayerNorm_type='WithBias', dual_pixel_task=False)
Bases: Module
Restormer: Efficient Transformer for high-resolution image restoration.
A multi-scale encoder-decoder transformer using Multi-DConv Head Transposed Self-Attention (MDTA) and Gated-DConv Feed-Forward Networks (GDFN). Includes a global residual connection from input to output.
Reference: Zamir et al., "Restormer: Efficient Transformer for High-Resolution Image Restoration" (CVPR 2022).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp_channels
|
Number of input channels. Defaults to 3. |
3
|
|
out_channels
|
Number of output channels. Defaults to 3. |
3
|
|
dim
|
Base embedding dimension. Defaults to 48. |
48
|
|
num_blocks
|
Number of transformer blocks per encoder/decoder stage.
Defaults to |
[4, 6, 6, 8]
|
|
num_refinement_blocks
|
Number of refinement blocks after the decoder. Defaults to 4. |
4
|
|
heads
|
Number of attention heads per stage. Defaults to |
[1, 2, 4, 8]
|
|
ffn_expansion_factor
|
Hidden dimension multiplier in GDFN. Defaults to 2.66. |
2.66
|
|
bias
|
Whether to use bias in convolutions. Defaults to False. |
False
|
|
LayerNorm_type
|
|
'WithBias'
|
|
dual_pixel_task
|
If True, uses skip connection for dual-pixel defocus
deblurring (set |
False
|
Source code in deeplens/network/reconstruction/restormer.py
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 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 382 383 384 385 386 387 388 | |
forward
Forward pass with global residual connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp_img
|
Input image tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
Restored image tensor of shape |
Source code in deeplens/network/reconstruction/restormer.py
Loss Functions
deeplens.network.PerceptualLoss
Bases: Module
Perceptual loss based on VGG16 features.
Initialize perceptual loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
Device to put the VGG model on. If None, uses cuda if available. |
None
|
|
weights
|
Weights for different feature layers. |
[1.0, 1.0, 1.0, 1.0, 1.0]
|
Source code in deeplens/network/loss/perceptual_loss.py
forward
Calculate perceptual loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Predicted tensor. |
required | |
y
|
Target tensor. |
required |
Returns:
| Type | Description |
|---|---|
|
Perceptual loss. |
Source code in deeplens/network/loss/perceptual_loss.py
deeplens.network.PSNRLoss
Bases: Module
Peak Signal-to-Noise Ratio (PSNR) loss.
Initialize PSNR loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_weight
|
Weight for the loss. |
1.0
|
|
reduction
|
Reduction method, only "mean" is supported. |
'mean'
|
|
toY
|
Whether to convert RGB to Y channel. |
False
|
Source code in deeplens/network/loss/psnr_loss.py
forward
Calculate PSNR loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Predicted tensor. |
required | |
target
|
Target tensor. |
required |
Returns:
| Type | Description |
|---|---|
|
PSNR loss. |
Source code in deeplens/network/loss/psnr_loss.py
deeplens.network.SSIMLoss
Bases: Module
Structural Similarity Index (SSIM) loss.
Initialize SSIM loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_size
|
Size of the window. |
11
|
|
size_average
|
Whether to average the loss. |
True
|
Source code in deeplens/network/loss/ssim_loss.py
forward
Calculate SSIM loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Predicted tensor. |
required | |
target
|
Target tensor. |
required |
Returns:
| Type | Description |
|---|---|
|
1 - SSIM value. |