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

# Use a pretrained model

Follow this example to reconstruct images using a pretrained model in one line.

We show three sets of general pretrained reconstruction methods, including:

* Pretrained feedforward [`Reconstruct Anything Model (RAM)`](https://deepinv.org/api/stubs/deepinv.models.RAM.html.md#deepinv.models.RAM);
* [Plug-and-play](https://deepinv.org/user_guide/reconstruction/iterative.html.md#iterative) with a pretrained denoiser.
* Pretrained [diffusion model](https://deepinv.org/user_guide/reconstruction/sampling.html.md#diffusion);

See [pretrained models](https://deepinv.org/user_guide/reconstruction/pretrained-models.html.md#pretrained-models) for a principled comparison between methods demonstrated in this example.

#### TIP
* Want to use your own dataset? See [Bring your own dataset](https://deepinv.org/auto_examples/basics/demo_custom_dataset.html.md#sphx-glr-auto-examples-basics-demo-custom-dataset-py)
* Want to use your own physics? See [Bring your own physics](https://deepinv.org/auto_examples/basics/demo_custom_physics.html.md#sphx-glr-auto-examples-basics-demo-custom-physics-py)

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

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

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

<!-- GENERATED FROM PYTHON SOURCE LINES 32-33 -->

Let’s say you want to reconstruct a butterfly from noisy, blurry measurements:

<!-- GENERATED FROM PYTHON SOURCE LINES 33-49 -->
```Python
# Ground truth
x = dinv.utils.load_example("butterfly.png", device=device)

# Define physics
physics = dinv.physics.BlurFFT(
    x.shape[1:],
    filter=dinv.physics.functional.gaussian_blur(sigma=(5, 5)),
    noise_model=dinv.physics.GaussianNoise(
        sigma=0.1, rng=torch.Generator(device=x.device).manual_seed(123)
    ),
    device=device,
)

y = physics(x)
```

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

For each model, define model in one line and reconstruct in one line.
Pretrained Reconstruct Anything Model:

#### SEE ALSO
See [Inference and fine-tune a foundation model](https://deepinv.org/auto_examples/models/demo_foundation_model.html.md#sphx-glr-auto-examples-models-demo-foundation-model-py) for further one-line examples for the RAM model across various domains.

<!-- GENERATED FROM PYTHON SOURCE LINES 55-61 -->
```Python
model = dinv.models.RAM(pretrained=True, device=device)

with torch.no_grad():
    x_hat1 = model(y, physics)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 62-68 -->

PnP algorithm with pretrained denoiser:

#### SEE ALSO
See [pretrained denoisers](https://deepinv.org/user_guide/reconstruction/pretrained-models.html.md#pretrained-weights) for a full list of denoisers that can be plugged into iterative/sampling algorithms.

See [DPIR method for PnP image deblurring.](https://deepinv.org/auto_examples/plug-and-play/demo_PnP_DPIR_deblur.html.md#sphx-glr-auto-examples-plug-and-play-demo-pnp-dpir-deblur-py) for a further example of using plug-and-play with a pretrained denoiser.

<!-- GENERATED FROM PYTHON SOURCE LINES 68-74 -->
```Python
denoiser = dinv.models.DRUNet(device=device)
model = dinv.optim.DPIR(sigma=0.1, denoiser=denoiser, device=device)

x_hat2 = model(y, physics)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 75-79 -->

Pretrained diffusion model (we reduce the image size for demo speed on CPU, as diffusion model is slow):

#### SEE ALSO
See [Image reconstruction with a diffusion model](https://deepinv.org/auto_examples/sampling/demo_ddrm.html.md#sphx-glr-auto-examples-sampling-demo-ddrm-py) for a further example of using a pretrained diffusion model.

<!-- GENERATED FROM PYTHON SOURCE LINES 79-85 -->
```Python
model = dinv.sampling.DDRM(denoiser, sigmas=torch.linspace(1, 0, 20)).to(device)

x_hat3 = model(y, physics)
```

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

Plot results

<!-- GENERATED FROM PYTHON SOURCE LINES 87-107 -->
```Python
dinv.utils.plot(
    {
        "Ground Truth": x,
        "Measurement": y,
        "Pretrained \nRAM": x_hat1,
        "Pretrained \nPnP": x_hat2,
        "Pretrained \nDiffusion": x_hat3,
    },
    subtitles=[
        "PSNR:",
        f"{dinv.metric.PSNR()(y, x).item():.2f} dB",
        f"{dinv.metric.PSNR()(x_hat1, x).item():.2f} dB",
        f"{dinv.metric.PSNR()(x_hat2, x).item():.2f} dB",
        f"{dinv.metric.PSNR()(x_hat3, x).item():.2f} dB",
    ],
    figsize=(10, 5),
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 111-118 -->

🎉 Well done, you now know how to use pretrained models!

## What’s next?

* Check out the [example on how to fine-tune a foundation model to your own problem](https://deepinv.org/auto_examples/models/demo_foundation_model.html.md#sphx-glr-auto-examples-models-demo-foundation-model-py).
* See [pretrained models](https://deepinv.org/user_guide/reconstruction/pretrained-models.html.md#pretrained-models) for a comparison between methods demonstrated in this example.
* See [diffusion](https://deepinv.org/user_guide/reconstruction/sampling.html.md#diffusion) and [iterative](https://deepinv.org/user_guide/reconstruction/iterative.html.md#iterative) for how to fully customize your sampling or iterative algorithm using a pretrained denoiser.

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

🏷 Tags: Denoising

<a id="sphx-glr-download-auto-examples-basics-demo-pretrained-model-py"></a>

[`Download Jupyter notebook: demo_pretrained_model.ipynb`](https://deepinv.org/auto_examples/basics/_downloads/7bd3f0c37fab9846872188d9d92e6b7e/demo_pretrained_model.ipynb)

[`Download Python source code: demo_pretrained_model.py`](https://deepinv.org/auto_examples/basics/_downloads/d2c8ff79555a2ba421858b0a406e211e/demo_pretrained_model.py)

[`Download zipped: demo_pretrained_model.zip`](https://deepinv.org/auto_examples/basics/_downloads/df43f8c0a78b3a0cbb70c84c0476beb6/demo_pretrained_model.zip)

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