.. 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. .. 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_basics_demo_pretrained_model.py: 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 :class:`Reconstruct Anything Model (RAM) `; * :ref:`Plug-and-play ` with a pretrained denoiser. * Pretrained :ref:`diffusion model `; See :ref:`pretrained models ` for a principled comparison between methods demonstrated in this example. .. tip:: * Want to use your own dataset? See :ref:`sphx_glr_auto_examples_basics_demo_custom_dataset.py` * Want to use your own physics? See :ref:`sphx_glr_auto_examples_basics_demo_custom_physics.py` .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. code-block:: Python import deepinv as dinv import torch device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu" .. GENERATED FROM PYTHON SOURCE LINES 28-29 Let's say you want to reconstruct a butterfly from noisy, blurry measurements: .. GENERATED FROM PYTHON SOURCE LINES 29-43 .. code-block:: Python # Ground truth x = dinv.utils.load_example("butterfly.png", device=device) # Define physics physics = dinv.physics.BlurFFT( x.shape[1:], filter=dinv.physics.blur.gaussian_blur((5, 5)), noise_model=dinv.physics.GaussianNoise(sigma=0.1), device=device, ) y = physics(x) .. GENERATED FROM PYTHON SOURCE LINES 44-49 For each model, define model in one line and reconstruct in one line. Pretrained Reconstruct Anything Model: .. seealso:: See :ref:`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 49-55 .. code-block:: Python model = dinv.models.RAM(pretrained=True, device=device) with torch.no_grad(): x_hat1 = model(y, physics) .. GENERATED FROM PYTHON SOURCE LINES 56-62 PnP algorithm with pretrained denoiser: .. seealso:: See :ref:`pretrained denoisers ` for a full list of denoisers that can be plugged into iterative/sampling algorithms. See :ref:`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 62-68 .. code-block:: 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 69-73 Pretrained diffusion model (we reduce the image size for demo speed on CPU, as diffusion model is slow): .. seealso:: See :ref:`sphx_glr_auto_examples_sampling_demo_ddrm.py` for a further example of using a pretrained diffusion model. .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: Python model = dinv.sampling.DDRM(denoiser, sigmas=torch.linspace(1, 0, 20)).to(device) x_hat3 = model(y, physics) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Plot results .. GENERATED FROM PYTHON SOURCE LINES 81-92 .. code-block:: Python dinv.utils.plot( { "Ground truth": x, "Blurred measurement": y, "Pretrained RAM": x_hat1, "Pretrained PnP": x_hat2, "Pretrained diffusion": x_hat3, } ) .. image-sg:: /auto_examples/basics/images/sphx_glr_demo_pretrained_model_001.png :alt: Ground truth, Blurred measurement, Pretrained RAM, Pretrained PnP, Pretrained diffusion :srcset: /auto_examples/basics/images/sphx_glr_demo_pretrained_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-100 🎉 Well done, you now know how to use pretrained models! What's next? ~~~~~~~~~~~~ * Check out the :ref:`example on how to fine-tune a foundation model to your own problem `. * See :ref:`pretrained models ` for a comparison between methods demonstrated in this example. * See :ref:`diffusion ` and :ref:`iterative ` for how to fully customize your sampling or iterative algorithm using a pretrained denoiser. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 50.291 seconds) .. _sphx_glr_download_auto_examples_basics_demo_pretrained_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_pretrained_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_pretrained_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_pretrained_model.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_