.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/optimization/demo_TV_minimisation.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_optimization_demo_TV_minimisation.py: Image deblurring with Total-Variation (TV) prior ==================================================================================================== This example shows how to use a standard TV prior for image deblurring. The problem writes as :math:`y = Ax + \epsilon` where :math:`A` is a convolutional operator and :math:`\epsilon` is the realization of some Gaussian noise. The goal is to recover the original image :math:`x` from the blurred and noisy image :math:`y`. The TV prior is used to regularize the problem. .. GENERATED FROM PYTHON SOURCE LINES 10-21 .. code-block:: Python import deepinv as dinv from pathlib import Path import torch from torchvision import transforms from deepinv.optim.data_fidelity import L2 from deepinv.optim.optimizers import optim_builder from deepinv.utils.demo import load_dataset, load_degradation from deepinv.utils.plotting import plot, plot_curves .. GENERATED FROM PYTHON SOURCE LINES 22-25 Setup paths for data loading and results. ---------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. 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 33-38 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 38-60 .. 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. dataset_name = "set3c" img_size = 256 if torch.cuda.is_available() else 64 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:000` is a regularisation parameters. We use a Proximal Gradient Descent (PGD) algorithm to solve the inverse problem. .. GENERATED FROM PYTHON SOURCE LINES 131-162 .. code-block:: Python # Select the data fidelity term data_fidelity = L2() # Specify the prior (we redefine it with a smaller number of iteration for faster computation) prior = dinv.optim.prior.TVPrior(n_it_max=20) # Logging parameters verbose = True plot_convergence_metrics = ( True # compute performance and convergence metrics along the algorithm. ) # Algorithm parameters stepsize = 1.0 lamb = 1e-2 # TV regularisation parameter params_algo = {"stepsize": stepsize, "lambda": lamb} max_iter = 300 early_stop = True # Instantiate the algorithm class to solve the problem. model = optim_builder( iteration="PGD", prior=prior, data_fidelity=data_fidelity, early_stop=early_stop, max_iter=max_iter, verbose=verbose, params_algo=params_algo, ) .. GENERATED FROM PYTHON SOURCE LINES 163-168 Evaluate the model on the problem and plot the results. -------------------------------------------------------------------- The model returns the output and the metrics computed along the iterations. For computing PSNR, the ground truth image ``x_gt`` must be provided. .. GENERATED FROM PYTHON SOURCE LINES 168-191 .. code-block:: Python x_lin = physics.A_adjoint(y) # linear reconstruction with the adjoint operator # run the model on the problem. x_model, metrics = model( y, physics, x_gt=x, compute_metrics=True ) # reconstruction with PGD algorithm # compute PSNR print(f"Linear reconstruction PSNR: {dinv.metric.PSNR()(x, x_lin).item():.2f} dB") print(f"PGD reconstruction PSNR: {dinv.metric.PSNR()(x, x_model).item():.2f} dB") # plot images. Images are saved in RESULTS_DIR. imgs = [y, x, x_lin, x_model] plot( imgs, titles=["Input", "GT", "Linear", "Recons."], ) # plot convergence curves if plot_convergence_metrics: plot_curves(metrics) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/optimization/images/sphx_glr_demo_TV_minimisation_002.png :alt: Input, GT, Linear, Recons. :srcset: /auto_examples/optimization/images/sphx_glr_demo_TV_minimisation_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/optimization/images/sphx_glr_demo_TV_minimisation_003.png :alt: PSNR, F, residual :srcset: /auto_examples/optimization/images/sphx_glr_demo_TV_minimisation_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Linear reconstruction PSNR: 15.01 dB PGD reconstruction PSNR: 21.43 dB .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.325 seconds) .. _sphx_glr_download_auto_examples_optimization_demo_TV_minimisation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_TV_minimisation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_TV_minimisation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_TV_minimisation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_