<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/optimization/demo_epll.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-optimization-demo-epll-py"></a>

# Expected Patch Log Likelihood (EPLL) for Denoising and Inpainting

In this example we use the expected patch log likelihood (EPLL) prior Zoran and Weiss<sup>[1](#footcite-zoran2011learning)</sup>.
for denoising and inpainting of natural images.
To this end, we consider the inverse problem $y = Ax+\epsilon$, where $A$ is either the identity (for denoising)
or a masking operator (for inpainting) and $\epsilon\sim\mathcal{N}(0,\sigma^2 I)$ is white Gaussian noise with standard deviation $\sigma$.

<!-- GENERATED FROM PYTHON SOURCE LINES 10-22 -->
```Python
import torch
import deepinv as dinv
from deepinv.optim import EPLL
from deepinv.physics import GaussianNoise, Denoising, Inpainting
from deepinv.loss.metric import PSNR
from deepinv.utils import plot
from deepinv.utils import load_example

device = dinv.utils.get_device()
```

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

<!-- GENERATED FROM PYTHON SOURCE LINES 24-29 -->

## Load test image and model

As default EPLL loads pretrained weights for the Gaussian mixture model which where estimated based
on 50 mio patches extracted from the BSDS500 dataset. An example how to estimate the parameters of GMM
is included in the demo for limited-angle CT with patch priors.

<!-- GENERATED FROM PYTHON SOURCE LINES 29-34 -->
```Python
test_img = load_example("CBSD_0010.png", grayscale=False).to(device)
patch_size = 6
model = EPLL(channels=test_img.shape[1], patch_size=patch_size, device=device)
```

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

## Denoising

In this setting, the operator $A$ is the identity; we set the noise level to $\sigma=25/255$.
Define noise model, operator and generate observation

<!-- GENERATED FROM PYTHON SOURCE LINES 39-45 -->
```Python
sigma = 0.1
noise_model = GaussianNoise(sigma)
physics = Denoising(noise_model=noise_model)
observation = physics(test_img)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 46-50 -->

We use the default choice of the betas in the half quadratic splitting given by
$\beta \in \sigma^{-2} \{1,4,8,16,32\}$.
Generally, the betas are hyperparameters, which have to be chosen for each inverse problem separately.

<!-- GENERATED FROM PYTHON SOURCE LINES 50-68 -->
```Python
# Reconstruction
with torch.no_grad():
    x_out = model(observation, physics, batch_size=5000)

# PSNR computation and plots.
psnr_obs = PSNR()(observation, test_img).item()
psnr_recon = PSNR()(x_out, test_img).item()

print("PSNRs for Denoising:")
print("Observation: {0:.2f}".format(psnr_obs))
print("EPLL: {0:.2f}".format(psnr_recon))

plot(
    [test_img, observation.clip(0, 1), x_out.clip(0, 1)],
    ["Ground truth", "Observation", "EPLL"],
)
```

```none
PSNRs for Denoising:
Observation: 20.00
EPLL: 28.68
```

<!-- GENERATED FROM PYTHON SOURCE LINES 69-73 -->

## Inpainting

We now turn to a noisy inpainting problem, where the operator $A$ is a masking operator.
Define noise model, operator and generate observation

<!-- GENERATED FROM PYTHON SOURCE LINES 73-83 -->
```Python
sigma = 0.01
physics = Inpainting(
    img_size=test_img[0].shape,
    mask=0.7,
    device=device,
    noise_model=GaussianNoise(sigma),
)
observation = physics(test_img)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 84-86 -->

Here, we need a different choice of beta. To this end, we extended the default choice
by two values and optimized a constant factor via grid search.

<!-- GENERATED FROM PYTHON SOURCE LINES 86-106 -->
```Python
betas = [1.0, 5.0, 10.0, 40.0, 80.0, 160.0, 320.0]

# Reconstruction
with torch.no_grad():
    x_out = model(observation, physics, betas=betas, batch_size=5000)

# PSNR computation and plots
psnr_obs = PSNR()(observation, test_img).item()
psnr_recon = PSNR()(x_out, test_img).item()

print("PSNRs for Inpainting:")
print("Observation: {0:.2f}".format(psnr_obs))
print("EPLL: {0:.2f}".format(psnr_recon))

plot(
    [test_img, observation.clip(0, 1), x_out.clip(0, 1)],
    ["Ground truth", "Observation", "EPLL"],
)
```

```none
PSNRs for Inpainting:
Observation: 10.93
EPLL: 29.10
```

<!-- GENERATED FROM PYTHON SOURCE LINES 107-110 -->
* **References:**

* <a id='footcite-zoran2011learning'>**[1]**</a> Daniel Zoran and Yair Weiss. From learning models of natural image patches to whole image restoration. In *2011 international conference on computer vision*, 479–486. IEEE, 2011.

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

🏷 Tags: Denoising, Inpainting

<a id="sphx-glr-download-auto-examples-optimization-demo-epll-py"></a>

[`Download Jupyter notebook: demo_epll.ipynb`](https://deepinv.org/auto_examples/optimization/_downloads/e199a1cb1899220da92fb0b6c5baae2d/demo_epll.ipynb)

[`Download Python source code: demo_epll.py`](https://deepinv.org/auto_examples/optimization/_downloads/1d063f1a961be328716be329132735d3/demo_epll.py)

[`Download zipped: demo_epll.zip`](https://deepinv.org/auto_examples/optimization/_downloads/e2aca9ff714612cdbd4610eed8152a19/demo_epll.zip)

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