.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/unfolded/demo_vanilla_unfolded.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_unfolded_demo_vanilla_unfolded.py: Vanilla Unfolded algorithm for super-resolution ==================================================================================================== This is a simple example to show how to use vanilla unfolded Plug-and-Play. The DnCNN denoiser and the algorithm parameters (stepsize, regularization parameters) are trained jointly. For simplicity, we show how to train the algorithm on a small dataset. For optimal results, use a larger dataset. .. GENERATED FROM PYTHON SOURCE LINES 11-22 .. code-block:: Python import deepinv as dinv import torch from deepinv.models.utils import get_weights_url from torch.utils.data import DataLoader from deepinv.optim.data_fidelity import L2 from deepinv.optim.prior import PnP from deepinv.optim import DRS from torchvision import transforms from deepinv.utils import get_data_home from deepinv.datasets import BSDS500 .. GENERATED FROM PYTHON SOURCE LINES 23-26 Setup paths for data loading and results. ---------------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: Python BASE_DIR = get_data_home() DATA_DIR = BASE_DIR / "measurements" RESULTS_DIR = BASE_DIR / "results" CKPT_DIR = BASE_DIR / "ckpts" # 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" .. rst-class:: sphx-glr-script-out .. code-block:: none Selected GPU 0 with 5035.125 MiB free memory .. GENERATED FROM PYTHON SOURCE LINES 38-41 Load base image datasets and degradation operators. ---------------------------------------------------------------------------------------- In this example, we use the CBSD500 dataset for training and the Set3C dataset for testing. .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python img_size = 64 if torch.cuda.is_available() else 32 n_channels = 3 # 3 for color images, 1 for gray-scale images operation = "super-resolution" .. GENERATED FROM PYTHON SOURCE LINES 47-50 Generate a dataset of low resolution images and load it. ---------------------------------------------------------------------------------------- We use the Downsampling class from the physics module to generate a dataset of low resolution images. .. GENERATED FROM PYTHON SOURCE LINES 50-104 .. code-block:: Python # For simplicity, we use a small dataset for training. # To be replaced for optimal results. For example, you can use the larger DIV2K or LSDIR datasets (also provided in the library). # Specify the train and test transforms to be applied to the input images. test_transform = transforms.Compose( [transforms.CenterCrop(img_size), transforms.ToTensor()] ) train_transform = transforms.Compose( [transforms.RandomCrop(img_size), transforms.ToTensor()] ) # Define the base train and test datasets of clean images. train_base_dataset = BSDS500( BASE_DIR, download=True, train=True, transform=train_transform ) test_base_dataset = BSDS500( BASE_DIR, download=False, train=False, transform=test_transform ) # Use parallel dataloader if using a GPU to speed up training, otherwise, as all computes are on CPU, use synchronous # dataloading. num_workers = 4 if torch.cuda.is_available() else 0 # Degradation parameters factor = 2 noise_level_img = 0.03 # Generate the gaussian blur downsampling operator. physics = dinv.physics.Downsampling( filter="gaussian", img_size=(n_channels, img_size, img_size), factor=factor, device=device, noise_model=dinv.physics.GaussianNoise(sigma=noise_level_img), ) my_dataset_name = "demo_unfolded_sr" n_images_max = ( None if torch.cuda.is_available() else 10 ) # max number of images used for training (use all if you have a GPU) measurement_dir = DATA_DIR / "BSDS500" / operation generated_datasets_path = dinv.datasets.generate_dataset( train_dataset=train_base_dataset, test_dataset=test_base_dataset, physics=physics, device=device, save_dir=measurement_dir, train_datapoints=n_images_max, num_workers=num_workers, dataset_filename=str(my_dataset_name), ) train_dataset = dinv.datasets.HDF5Dataset(path=generated_datasets_path, train=True) test_dataset = dinv.datasets.HDF5Dataset(path=generated_datasets_path, train=False) .. rst-class:: sphx-glr-script-out .. code-block:: none 0it [00:00, ?it/s] 2.25MB [00:00, 22.9MB/s] 5.62MB [00:00, 29.9MB/s] 8.50MB [00:00, 28.8MB/s] 11.3MB [00:00, 28.7MB/s] 14.1MB [00:00, 28.0MB/s] 16.8MB [00:00, 28.0MB/s] 19.6MB [00:00, 28.1MB/s] 22.7MB [00:00, 29.1MB/s] 26.0MB [00:00, 30.7MB/s] 29.2MB [00:01, 31.5MB/s] 32.6MB [00:01, 32.5MB/s] 35.7MB [00:01, 32.0MB/s] 39.0MB [00:01, 32.7MB/s] 42.4MB [00:01, 33.2MB/s] 45.7MB [00:01, 33.6MB/s] 49.0MB [00:01, 33.9MB/s] 52.2MB [00:01, 33.9MB/s] 55.6MB [00:01, 34.1MB/s] 58.9MB [00:02, 27.5MB/s] 61.8MB [00:02, 23.7MB/s] 64.2MB [00:02, 21.8MB/s] 66.5MB [00:02, 20.2MB/s] 68.6MB [00:02, 19.8MB/s] 70.6MB [00:02, 19.2MB/s] 72.4MB [00:02, 18.7MB/s] 74.2MB [00:02, 18.2MB/s] 76.1MB [00:03, 18.1MB/s] 77.8MB [00:03, 18.1MB/s] 79.6MB [00:03, 17.9MB/s] 81.4MB [00:03, 18.2MB/s] 83.2MB [00:03, 17.8MB/s] 84.9MB [00:03, 17.8MB/s] 86.7MB [00:03, 17.9MB/s] 88.5MB [00:03, 17.9MB/s] 90.2MB [00:03, 17.8MB/s] 92.0MB [00:04, 17.9MB/s] 93.8MB [00:04, 18.0MB/s] 95.5MB [00:04, 17.6MB/s] 97.4MB [00:04, 17.8MB/s] 99.1MB [00:04, 17.9MB/s] 101MB [00:04, 17.9MB/s] 103MB [00:04, 17.8MB/s] 104MB [00:04, 17.6MB/s] 106MB [00:04, 17.4MB/s] 108MB [00:04, 18.0MB/s] 110MB [00:05, 17.8MB/s] 112MB [00:05, 17.6MB/s] 113MB [00:05, 17.9MB/s] 115MB [00:05, 17.8MB/s] 117MB [00:05, 17.7MB/s] 119MB [00:05, 17.8MB/s] 120MB [00:05, 17.9MB/s] 122MB [00:05, 18.0MB/s] 124MB [00:05, 18.0MB/s] 126MB [00:05, 17.7MB/s] 128MB [00:06, 17.7MB/s] 129MB [00:06, 18.0MB/s] 131MB [00:06, 18.0MB/s] 133MB [00:06, 9.05MB/s] 138MB [00:06, 15.3MB/s] 142MB [00:06, 21.1MB/s] 145MB [00:07, 20.8MB/s] 149MB [00:07, 25.4MB/s] 152MB [00:07, 26.7MB/s] 155MB [00:07, 19.5MB/s] 157MB [00:07, 13.6MB/s] 159MB [00:08, 11.9MB/s] 160MB [00:08, 20.3MB/s] Extracting: 0%| | 0/2492 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_vanilla_unfolded.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_vanilla_unfolded.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_