.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/self-supervised-learning/demo_poisson2sparse.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note New to DeepInverse? Get started with the basics with the :ref:`5 minute quickstart tutorial `. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_self-supervised-learning_demo_poisson2sparse.py: Poisson denoising using Poisson2Sparse ======================================= This code shows how to restore a single image corrupted by Poisson noise using Poisson2Sparse, without requiring external training or knowledge of the noise level. This method is based on the paper "Poisson2Sparse" :footcite:t:`ta2022poisson2sparse` and restores an image by learning a sparse non-linear dictionary parametrized by a neural network using a combination of Neighbor2Neighbor :footcite:t:`huang2021neighbor2neighbor`, of the negative log Poisson likelihood, of the :math:`\ell^1` pixel distance and of a sparsity-inducing :math:`\ell^1` regularization function on the weights. .. GENERATED FROM PYTHON SOURCE LINES 10-15 .. code-block:: Python import deepinv as dinv import torch .. GENERATED FROM PYTHON SOURCE LINES 16-20 Load a Poisson corrupted image ------------------------------ This example uses an image from the microscopy dataset FMD :footcite:t:`zhang2018poisson`. .. GENERATED FROM PYTHON SOURCE LINES 20-36 .. code-block:: Python # Seed the RNGs for reproducibility torch.manual_seed(0) torch.cuda.manual_seed(0) device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu" physics = dinv.physics.Denoising(dinv.physics.PoissonNoise(gain=0.01, normalize=True)) x = dinv.utils.demo.load_example( "FMD_TwoPhoton_MICE_R_gt_12_avg50.png", img_size=(256, 256) ).to(device) x = x[:, 0:1, :64, :64] x = x.clamp(0, 1) y = physics(x) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Define the Poisson2Sparse model .. GENERATED FROM PYTHON SOURCE LINES 38-58 .. code-block:: Python backbone = dinv.models.ConvLista( in_channels=1, out_channels=1, kernel_size=3, num_filters=512, num_iter=10, stride=2, threshold=0.01, ) model = dinv.models.Poisson2Sparse( backbone=backbone, lr=1e-4, num_iter=200, weight_n2n=2.0, weight_l1_regularization=1e-5, verbose=True, ).to(device) .. GENERATED FROM PYTHON SOURCE LINES 59-64 Run the model ------------- Note that we do not pass in the physics model as Poisson2Sparse assumes a Poisson noise model internally and does not depend on the noise level. .. GENERATED FROM PYTHON SOURCE LINES 64-80 .. code-block:: Python x_hat = model(y) # Compute and display PSNR values learning_free_psnr = dinv.metric.PSNR()(y, x).item() model_psnr = dinv.metric.PSNR()(x_hat, x).item() print(f"Measurement PSNR: {learning_free_psnr:.1f} dB") print(f"Poisson2Sparse PSNR: {model_psnr:.1f} dB") # Plot results dinv.utils.plot( [y, x_hat, x], titles=["Measurement", "Poisson2Sparse", "Ground truth"], subtitles=[f"{learning_free_psnr:.1f} dB", f"{model_psnr:.1f} dB", ""], ) .. image-sg:: /auto_examples/self-supervised-learning/images/sphx_glr_demo_poisson2sparse_001.png :alt: Measurement, Poisson2Sparse, Ground truth :srcset: /auto_examples/self-supervised-learning/images/sphx_glr_demo_poisson2sparse_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/200 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_poisson2sparse.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_poisson2sparse.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_