.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plug-and-play/demo_PnP_DPIR_deblur.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plug-and-play_demo_PnP_DPIR_deblur.py: DPIR method for PnP image deblurring. ==================================================================================================== This example shows how to use the DPIR method to solve a PnP image deblurring problem. The DPIR method is described in the following paper: Zhang, K., Zuo, W., Gu, S., & Zhang, L. (2017). Learning deep CNN denoiser prior for image restoration. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 3929-3938). .. GENERATED FROM PYTHON SOURCE LINES 11-25 .. code-block:: Python import deepinv as dinv from pathlib import Path import torch from torch.utils.data import DataLoader from deepinv.models import DRUNet from deepinv.optim.data_fidelity import L2 from deepinv.optim.prior import PnP from deepinv.optim.optimizers import optim_builder from deepinv.training import test from torchvision import transforms from deepinv.optim.dpir import get_DPIR_params from deepinv.utils.demo import load_dataset, load_degradation .. GENERATED FROM PYTHON SOURCE LINES 26-29 Setup paths for data loading and results. ---------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-36 .. code-block:: Python BASE_DIR = Path(".") DATA_DIR = BASE_DIR / "measurements" RESULTS_DIR = BASE_DIR / "results" DEG_DIR = BASE_DIR / "degradations" .. GENERATED FROM PYTHON SOURCE LINES 37-42 Load base image datasets and degradation operators. ---------------------------------------------------------------------------------------- In this example, we use the Set3C dataset and a motion blur kernel from `Levin et al. (2009) `_. .. GENERATED FROM PYTHON SOURCE LINES 42-65 .. code-block:: Python # Set the global random seed from pytorch to ensure reproducibility of the example. torch.manual_seed(0) device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu" # Set up the variable to fetch dataset and operators. method = "DPIR" dataset_name = "set3c" img_size = 128 if torch.cuda.is_available() else 32 val_transform = transforms.Compose( [transforms.CenterCrop(img_size), transforms.ToTensor()] ) # Generate a motion blur operator. kernel_index = 1 # which kernel to chose among the 8 motion kernels from 'Levin09.mat' kernel_torch = load_degradation("Levin09.npy", DEG_DIR / "kernels", index=kernel_index) kernel_torch = kernel_torch.unsqueeze(0).unsqueeze( 0 ) # add batch and channel dimensions dataset = load_dataset(dataset_name, transform=val_transform) .. rst-class:: sphx-glr-script-out .. code-block:: none Levin09.npy degradation downloaded in degradations/kernels Downloading datasets/set3c.zip 0%| | 0.00/385k [00:00 1 # and restore multiple images in parallel. dataset = dinv.datasets.HDF5Dataset(path=dinv_dataset_path, train=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Dataset has been saved at measurements/set3c/deblur/dinv_dataset0.h5 .. GENERATED FROM PYTHON SOURCE LINES 103-111 Set up the DPIR algorithm to solve the inverse problem. -------------------------------------------------------------------------------- This method is based on half-quadratic splitting (HQS). The algorithm alternates between a denoising step and a data fidelity step, where the denoising step is performed by a pretrained denoiser :class:`deepinv.models.DRUNet`. .. note:: We provide a wrapper for rapidly creating the DPIR algorithm in :class:`deepinv.optim.DPIR`. .. GENERATED FROM PYTHON SOURCE LINES 111-138 .. code-block:: Python # load specific parameters for DPIR sigma_denoiser, stepsize, max_iter = get_DPIR_params(noise_level_img) params_algo = {"stepsize": stepsize, "g_param": sigma_denoiser} early_stop = False # Do not stop algorithm with convergence criteria # Select the data fidelity term data_fidelity = L2() # Specify the denoising prior prior = PnP(denoiser=DRUNet(pretrained="download", device=device)) # instantiate the algorithm class to solve the IP problem. model = optim_builder( iteration="HQS", prior=prior, data_fidelity=data_fidelity, early_stop=early_stop, max_iter=max_iter, verbose=True, params_algo=params_algo, ) # Set the model to evaluation mode. We do not require training here. model.eval() .. rst-class:: sphx-glr-script-out .. code-block:: none BaseOptim( (fixed_point): FixedPoint( (iterator): HQSIteration( (f_step): fStepHQS() (g_step): gStepHQS() ) ) ) .. GENERATED FROM PYTHON SOURCE LINES 139-143 Evaluate the model on the problem. -------------------------------------------------------------------- The test function evaluates the model on the test dataset and computes the metrics. .. GENERATED FROM PYTHON SOURCE LINES 143-163 .. code-block:: Python save_folder = RESULTS_DIR / method / operation / dataset_name plot_convergence_metrics = True # Metrics are saved in save_folder. plot_images = True # Images are saved in save_folder. dataloader = DataLoader( dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False ) test( model=model, test_dataloader=dataloader, physics=p, metrics=[dinv.metric.PSNR(), dinv.metric.LPIPS(device=device)], device=device, plot_images=plot_images, save_folder=save_folder, plot_convergence_metrics=plot_convergence_metrics, verbose=True, ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/plug-and-play/images/sphx_glr_demo_PnP_DPIR_deblur_001.png :alt: Ground truth, Measurement, No learning, Reconstruction :srcset: /auto_examples/plug-and-play/images/sphx_glr_demo_PnP_DPIR_deblur_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/plug-and-play/images/sphx_glr_demo_PnP_DPIR_deblur_002.png :alt: PSNR, residual :srcset: /auto_examples/plug-and-play/images/sphx_glr_demo_PnP_DPIR_deblur_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Loading pretrained model LPIPS from /home/runner/.cache/torch/hub/pyiqa/LPIPS_v0.1_alex-df73285e.pth 2025-03-13 09:16:02,160 INFO: Network [LPIPS] is created. 2025-03-13 09:16:02,160 INFO: Metric [LPIPS] is created. 0%| | 0/1 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_PnP_DPIR_deblur.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_PnP_DPIR_deblur.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_