{'fraction_non_alphanumeric': 0.03416963745207507, 'fraction_numerical': 0.013452613170108294, 'mean_word_length': 4.512791991101223, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Learning to Model and Calibrate Optics Via a Differentiable Wave Optics Simulator

We present a novel learning-based method to build a differentiable computational model of a real fluorescence microscope. Our model can be used to calibrate a real optical setup directly from data samples and to engineer point spread functions by specifying the desired input-output data. This approach is poised to drastically improve the design of microscopes, because the parameters of current models of optical setups cannot be easily fit to real data. Inspired by the recent progress in deep learning, our solution is to build a differentiable wave optics simulator as a composition of trainable modules, each computing light wave-front (WF) propagation due to a specific optical element. We call our differentiable modules WaveBlocks and show reconstruction results in the case of lenses, wave propagation in air, camera sensors and diffractive elements (e.g., phase-masks).


INTRODUCTION
Microscopes have a fundamental impact in biology, life science and engineering. Because of their essential role in imaging, their design has seen a constant evolution in an effort to improve the imaging quality (e.g., the lateral and axial resolutions and the speed). Towards this goal, one approach is to improve the quality of the captured data through better sensors, optics and their configuration; also referred to as point spread function (PSF) engineering [1,2,3], and it is currently achieved mostly through manual design and analysis. A second approach is instead to improve the captured data via image processing. The task is posed as an inverse problem, where the latent high-quality image is reconstructed by combining a generative model of observed data (the data fidelity term) with a model of the patterns typically observed in the data (the data prior). In this domain recent deep learning J. Page  methods have demonstrated an impressive performance, both in terms of image reconstruction accuracy and speed, thanks to their ability to efficiently and accurately describe complex data priors and generative models [4]. Current PSF engineering is sub-optimal as it does not take into account the subsequent image processing step. In fact, an optimal data capture system should be designed around the patterns of typically observed data, and this is an extremely difficult problem to solve analytically. Therefore, we argue that a better approach is to jointly optimize both steps numerically through real data. Towards this goal, in this paper we introduce and study a model of optics that can be readily integrated with deep learning models for image processing. Our solution is to first accurately describe light propagation through optical elements (e.g., lenses, air, phase-masks) with computational modules, which we call WaveBlocks (see Fig. 1). In this way, it is possible to relate each module to a physical counterpart. Secondly, to enable the automatic optimization of the optical elements, we ensure that the modules are differentiable with respect to their parameters. We can then fit real data to our compositional model through optimization (i.e., via stochastic gradient descent), a procedure that we call calibration. In this paper, we illustrate our approach with several calibration tasks. First, we show how to accurately fit the PSF of a real microscope by using real data. Later, we show how to recover the diffracting pattern caused by the lack of 100% fill factor in a spatial light modulator (SLM) within a 4-f setup.

PREVIOUS WORK
PSF engineering depends on the imaging properties of interest, for example, one could aim to improve imaging for super-resolution [5,6,2,7,1], or 3D reconstruction [8,4,2,9], or extended depth of field [10,9,3], or even lightfield microscopy [8]. General phase-mask optimization [3] and calibration [1] have been explored before. One approach to solve calibration is to pose the task as a blind deconvolution problem [11,12,13]. Also phase retrieval and the removal of the zero-order diffraction from a SLM have been previously studied [14,15]. Previously a microscope simulator for the generation of synthetic biological images was proposed [16]. However, this computational model is not suitable for data-driven automatic optimization of the optical components. Thus, our proposed WaveBlocks, a differentiable and scalable optimization framework, provides a fundamental tool to do automatic data-driven PSF engineering. In future, work we aim to demonstrate our framework on a variety of image processing tasks and imaging devices.

BUILDING A WAVE OPTICS SIMULATOR
As discussed in the Introduction, we are interested in building a differentiable model that can accurately approximate real optics and with modules that are associated to each optical component in a microscope. We start by defining the image formation process of a fluorescence microscope. In its simplest instance, it follows the convolution operation where o is the fluorescent object, i the observed image, n additive noise and H the PSF or system matrix. H can be acquired in several ways: by measuring it directly with the microscope, or, as it is usually done, by modeling it computationally. In our modules, the light emitted by a fluorescent particle is propagated as a complex WF through each of the optical elements in the microscope until it hits the sensor, where the irradiance of the WF is computed and stored as the PSF for later use.
The linear operators that model each optical element forming the PSF are based on complex diffraction integrals, which difficult to optimize when stacked together. However, their linearity enables the usage of each operator as a block that receives an input WF and produces an output. Moreover, it is possible to easily stack as many blocks as desired. We implement these operators in the Pytorch framework [17], which performs auto-differentiation. The architecture is user-friendly and allows the optimization of a wide variety of parameters in an optical system. The rationale behind WaveBlocks is that the user provides a bright-field PSF (for example, generated in Fiji [18]). This PSF propagates through the user-defined optical blocks until it reaches a camera module. On this module the image irradiance is computed and convolved with a user-provided object as shown in Fig. 1.

Wave Blocks Implementation
In the following sections, we describe the computational models of the key optical elements implemented so far.

Wave propagation (WP)
To compute the monochromatic WF propagation through a medium (the WP block), we use the Rayleight-Sommerfeld integral (see [19], page 52). Our implementation of the Rayleight-Sommerfeld propagation is limited to a minimum distance of 200µm, to make sure that the required sampling remains computationally practical. According to the Fourier convolution theorem (see [19], page 39), the WP block can be obtained via , where F denotes the Fourier transform, F −1 denotes its inverse, (x, y, z) are 3D image spatial coordinates, j denotes the imaginary component in complex numbers, h(x, y) is the Rayleigh-Sommerfeld impulse response, λ is the light wavelength, k is the wave number and r = z 2 + x 2 + y 2 is the distance from a point in U 1 to another in U 2 .

Lens (L)
The lens block (the L block) describes a convex lens which propagates a WF from a plane at the front of the lens to the back of it. According to the Fraunhofer approximation (see [19], page 96), the lens propagation from the front plane to the back focal plane is equivalent to a scaled Fourier transform given by the following equation where dx 1 is the source sample interval, f l the focal length of the lens, P (x, y) the pupil function and c is the scaling factor given by

Camera (C)
The camera block (the C block) performs two tasks: 1) It determines the PSF H by computing the irradiance of the incoming WF, which is the time-averaged square magnitude of the field U , given by H(x, y) = U (x, y)U (x, y) * = |U (x, y)| 2 . This time averaging occurs due to the incapability of current detectors to follow the high frequency oscillations of the electric field (> 10 14 Hz) (see [19], page 49); 2) It convolves the computed PSF H with an object o placed in front of the microscope, i.e., i = H * o.

Phase-mask (PM) or spatial light modulator (SLM)
The phase-mask block (the PM block) describes a phase-mask that distorts the phase of the WF in a space-variant way. The incoming WF U 1 is modified by the modulation function φ as in U 2 (x, y) = U 1 (x, y) exp(jφ(x, y)). A special case applies when a phase-only SLM is used where only the imaginary part of the exponential is multiplied by U 1 and the real part becomes zero. In WaveBlocks φ is a parameter that can be changed at will or optimized simply by naming it in the initializer of the Microscope class.

Data-driven calibration
To calibrate an optical system we capture images of several known objects. If we denote with H gt the real system matrix, we can capture real images i of known objects o, which we model as i = H gt * o. WaveBlocks allow us to model this data via a system matrix H parametrized via Θ, which collects all the settings of the optical elements (e.g., the main objective PSF, the phase-mask phase change). This optimization problem can be then written as the minimization of the expectation over all objects and corresponding observed images (which can be approximated by using a finite, albeit large, number of samples)Θ

= arg min
where is the cost function measuring the discrepancy between the observed images i and the synthesized ones H(Θ) * o. H can be formed by the successive stack of blocks. For example, the configuration shown in Fig. 1 yields H(Θ) = C2(L2(P M (L1(W P 1(P SF ))))), where Θ has the parameters of the PM and/or the PSF. This configuration is also used in the experiments in sec. 4.2.

EXPERIMENTS
To demonstrate the capabilities of our proposed calibration approach, we aim to reconstruct two sets of parameters: 1) the initial PSF of the microscope and 2) the distortion diffraction pattern generated by the empty space between the pixels of a SLM. We employ a USAF 1951 resolution target, a 20x 0.45NA objective, a 165mm focal length tube-lens, a camera (C1-C2) with 6.9µm pixel size, two lenses (L1-L2) with focal length of 150mm and aperture of 50.8mm and the phase-mask (PM) used is an SLM Holoeye Pluto-vision.

Bright-field PSF estimation
In this section we focus on reconstructing the PSF of a real microscope in the simplest case, where only a PSF block representing the PSF generated by the objective and tubelens, a wave-propagation block and a camera block are used (PSF, WP1 and C1 in Fig. 1). As o we choose the USAF 1951 target object (only one image in this case), and the loss is the normalized mean square error (NMSE) loss given by . The optimization uses the Adam optimizer.

Recovery of the PSF from synthetic data
To measure the robustness of the PSF reconstruction algorithm we randomly transform and reconstruct a PSF computed via the scalar Debye theory [20,21]. First, we translate the object randomly in a range of ±20 pixels, then re-scale it in x and y directions independently in the range 1±0.3 and defocus it using WP1 by a random distance between ±200 and ±1000 µm in image space. This synthetic PSF H gt is built by setting the parameters to some Θ gt . A total of 500 different PSFs were estimated, using defocused images at -50, 0 and 50 µm in reference of the focal plane of the objective, this by using the Adam optimizer from Pytorch. The results of this evaluation (see Table 1) show that even though the error from the PSF estimation is not optimal, the error of from the images used for optimization is very low, meaning that the system finds a PSF that its able to reproduce the observed images.

Recovery of the PSF from real data
In this experiment we aim to recover the PSF of a real microscope. We acquire a stack of images of the USAF 1951 target placed at depths spanning -50 to 50 µm in steps of 2µm relative to the focal plane of the objective. For this experiment only the images at depths -50, 50 and 0 µm were used in the training, and the rest were stored for later testing. The recovered PSF can be seen in Fig. 2 next to the PSF of an ideal microscope. Notice how the recovered PSF exhibits aberrations not present in the ideal case.
To verify that the recovered PSF is optimal, we use the optical configuration in Fig. 1   dict the depth at which an image with the real microscope was taken. We compare each image in the full depth range against a stack generated through our estimated model and plotted which depth yields the smallest error. As shown at the top of Fig. 3 the recovered PSF achieves the highest accuracy, with a mean NMSE error of 2.8µm, against the initial PSF with error of 19.21µm. Note how the ideal PSF is not robust against the depth symmetry, by predicting in some cases the wrong direction of defocus.

Phase-mask distortion pattern recovery
In this section we aim to recover the diffraction pattern created by the lack of 100% pixel fill factor in a SLM [14,15] placed at the Fourier plane of a 4-f system (see supplementary material for an image of our setup). The zero-order diffraction lobe hits exactly the center of the output image, thus hampering the usability of an SLM.

Recovery on synthetic data
Consistently with sec. 4.1, a test on synthetic data was first performed, where a PM was selected randomly between a cubic mask due to its simplicity and stable behavior in the Fourier domain [10] and a circular mask with a circular gradient towards the center, simulating the zero diffraction mode  Table 1. A video of the optimization process can be found in the attached material.

Recovering the phase-mask distortion pattern
We use the recovered PSF from sec. 4.1 in a 4-f system, and propagate the WF until C2. As discussed in sec. 4.2, the diffraction pattern created by the SLM distorts the PSF in the frequency domain and by recovering this pattern the distortion can be corrected. In this experiment we show that by using our synthesis model with the correction pattern displayed at the SLM an accurate depth of a stack of defocused images can be inferred (see Fig. 3 bottom). Dealing a mean NMSE error of 0.67µm against 5.70µm without PM correction. The recovered phase range matches the description of the vendor (0 to 5π max shift).

CONCLUSIONS
We have introduced a novel learning-based method to build a differentiable computational model of a real microscope. Experiments with both synthetic and real experiments, demonstrated that the proposed method allows to recover latent parameters of an optical setup (e.g., a PSF or a PM) with high accuracy. We encourage the reader to explore the code repository [22] for working examples and constant updates.