<!-- 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. -->

#### NOTE
New to DeepInverse? Get started with the basics with the
[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md#sphx-glr-auto-examples-basics-demo-quickstart-py)..

<a id="sphx-glr-auto-examples-self-supervised-learning-demo-poisson2sparse-py"></a>

# 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” Ta *et al.*<sup>[1](#footcite-ta2022poisson2sparse)</sup> and restores an image by learning a sparse non-linear dictionary parametrized by a neural network using a combination of Neighbor2Neighbor Huang *et al.*<sup>[2](#footcite-huang2021neighbor2neighbor)</sup>, of the negative log Poisson likelihood, of the $\ell^1$ pixel distance and of a sparsity-inducing $\ell^1$ regularization function on the weights.

<!-- GENERATED FROM PYTHON SOURCE LINES 10-15 -->
```Python
import deepinv as dinv
import torch
```

<!-- GENERATED FROM PYTHON SOURCE LINES 17-21 -->

## Load a Poisson corrupted image

This example uses an image from the microscopy dataset FMD Zhang *et al.*<sup>[3](#footcite-zhang2018poisson)</sup>.

<!-- GENERATED FROM PYTHON SOURCE LINES 21-37 -->
```Python
# Seed the RNGs for reproducibility
torch.manual_seed(0)
torch.cuda.manual_seed(0)

device = dinv.utils.get_device()

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)
```

```none
Selected GPU 0 with 22455.25 MiB free memory
```

<!-- GENERATED FROM PYTHON SOURCE LINES 38-39 -->

Define the Poisson2Sparse model

<!-- GENERATED FROM PYTHON SOURCE LINES 39-59 -->
```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 60-65 -->

## 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 65-81 -->
```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", ""],
)
```

```none
  0%|          | 0/200 [00:00<?, ?it/s]
  2%|▏         | 4/200 [00:00<00:04, 39.64it/s]
 10%|▉         | 19/200 [00:00<00:01, 102.82it/s]
 18%|█▊        | 37/200 [00:00<00:01, 134.42it/s]
 28%|██▊       | 55/200 [00:00<00:00, 148.91it/s]
 36%|███▋      | 73/200 [00:00<00:00, 156.79it/s]
 46%|████▌     | 91/200 [00:00<00:00, 161.59it/s]
 55%|█████▍    | 109/200 [00:00<00:00, 164.58it/s]
 64%|██████▎   | 127/200 [00:00<00:00, 166.48it/s]
 72%|███████▎  | 145/200 [00:00<00:00, 167.81it/s]
 82%|████████▏ | 163/200 [00:01<00:00, 168.65it/s]
 90%|█████████ | 181/200 [00:01<00:00, 169.27it/s]
100%|█████████▉| 199/200 [00:01<00:00, 169.68it/s]
100%|██████████| 200/200 [00:01<00:00, 158.22it/s]
Measurement PSNR: 27.5 dB
Poisson2Sparse PSNR: 31.1 dB
```

<!-- GENERATED FROM PYTHON SOURCE LINES 82-85 -->
* **References:**

* <a id='footcite-ta2022poisson2sparse'>**[1]**</a> Calvin-Khang Ta, Abhishek Aich, Akash Gupta, and Amit K Roy-Chowdhury. Poisson2sparse: self-supervised poisson denoising from a single image. In *International Conference on Medical Image Computing and Computer-Assisted Intervention*, 557–567. Springer, 2022.
* <a id='footcite-huang2021neighbor2neighbor'>**[2]**</a> Tao Huang, Songjiang Li, Xu Jia, Huchuan Lu, and Jianzhuang Liu. Neighbor2neighbor: self-supervised denoising from single noisy images. In *Proceedings of the IEEE/CVF conference on computer vision and pattern recognition*, 14781–14790. 2021.
* <a id='footcite-zhang2018poisson'>**[3]**</a> Yide Zhang, Yinhao Zhu, Evan Nichols, Qingfei Wang, Siyuan Zhang, Cody Smith, and Scott Howard. A poisson-gaussian denoising dataset with real fluorescence microscopy images. In *CVPR*. 2019.

**Total running time of the script:** (0 minutes 1.395 seconds)

🏷 Tags: Denoising

<a id="sphx-glr-download-auto-examples-self-supervised-learning-demo-poisson2sparse-py"></a>

[`Download Jupyter notebook: demo_poisson2sparse.ipynb`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/e391e3e6e17d82d10dcf1de37f61019c/demo_poisson2sparse.ipynb)

[`Download Python source code: demo_poisson2sparse.py`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/86cf8d00a5166eeb264d55058eb8acc7/demo_poisson2sparse.py)

[`Download zipped: demo_poisson2sparse.zip`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/cd3f36cfd1f454e564bccda15c633ace/demo_poisson2sparse.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
