Image restoration with neural networks but without learning.

Overview

Warning! The optimization may not converge on some GPUs. We've personally experienced issues on Tesla V100 and P40 GPUs. When running the code, make sure you get similar results to the paper first. Easiest to check using text inpainting notebook. Try to set double precision mode or turn off cudnn.

Deep image prior

In this repository we provide Jupyter Notebooks to reproduce each figure from the paper:

Deep Image Prior

CVPR 2018

Dmitry Ulyanov, Andrea Vedaldi, Victor Lempitsky

[paper] [supmat] [project page]

Here we provide hyperparameters and architectures, that were used to generate the figures. Most of them are far from optimal. Do not hesitate to change them and see the effect.

We will expand this README with a list of hyperparameters and options shortly.

Install

Here is the list of libraries you need to install to execute the code:

  • python = 3.6
  • pytorch = 0.4
  • numpy
  • scipy
  • matplotlib
  • scikit-image
  • jupyter

All of them can be installed via conda (anaconda), e.g.

conda install jupyter

or create an conda env with all dependencies via environment file

conda env create -f environment.yml

Docker image

Alternatively, you can use a Docker image that exposes a Jupyter Notebook with all required dependencies. To build this image ensure you have both docker and nvidia-docker installed, then run

nvidia-docker build -t deep-image-prior .

After the build you can start the container as

nvidia-docker run --rm -it --ipc=host -p 8888:8888 deep-image-prior

you will be provided an URL through which you can connect to the Jupyter notebook.

Google Colab

To run it using Google Colab, click here and select the notebook to run. Remember to uncomment the first cell to clone the repository into colab's environment.

Citation

@article{UlyanovVL17,
    author    = {Ulyanov, Dmitry and Vedaldi, Andrea and Lempitsky, Victor},
    title     = {Deep Image Prior},
    journal   = {arXiv:1711.10925},
    year      = {2017}
}
Comments
  • RuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/TH/generic/THTensorMath.c:2864

    RuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/TH/generic/THTensorMath.c:2864

    env:

    • pytorch 0.3
    • python 3.5
    • cuda8.0

    inpainting.ipynb got this error at

    Visualize : plot_image_grid([img_np, img_mask_np, img_mask_np*img_np], 3,11);

    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-6-e9e29a4716c2> in <module>()
          1 img_mask_var = np_to_var(img_mask_np).type(dtype)
          2 
    ----> 3 plot_image_grid([img_np, img_mask_np, img_mask_np*img_np], 3,11);
    
    /data/notebooks/deep-image-prior/utils/common_utils.py in plot_image_grid(images_np, nrow, factor, interpolation)
         70         interpolation: interpolation used in plt.imshow
         71     """
    ---> 72     grid = get_image_grid(images_np, nrow)
         73 
         74     plt.figure(figsize=(len(images_np)+factor,12+factor))
    
    /data/notebooks/deep-image-prior/utils/common_utils.py in get_image_grid(images_np, nrow)
         57     '''Creates a grid from a list of images by concatenating them.'''
         58     images_torch = [torch.from_numpy(x) for x in images_np]
    ---> 59     torch_grid = torchvision.utils.make_grid(images_torch, nrow)
         60 
         61     return torch_grid.numpy()
    
    /usr/local/lib/python3.5/dist-packages/torchvision/utils.py in make_grid(tensor, nrow, padding, normalize, range, scale_each, pad_value)
         33     # if list of tensors, convert to a 4D mini-batch Tensor
         34     if isinstance(tensor, list):
    ---> 35         tensor = torch.stack(tensor, dim=0)
         36 
         37     if tensor.dim() == 2:  # single image H x W
    
    /usr/local/lib/python3.5/dist-packages/torch/functional.py in stack(sequence, dim, out)
         62     inputs = [t.unsqueeze(dim) for t in sequence]
         63     if out is None:
    ---> 64         return torch.cat(inputs, dim)
         65     else:
         66         return torch.cat(inputs, dim, out=out)
    
    RuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/TH/generic/THTensorMath.c:2864
    
    opened by eromoe 7
  • Cuda Error

    Cuda Error

    I tried to run this with few other images. I am getting this error. Can you please tell me where I am going wrong? The following is the error I am getting.

    RuntimeError Traceback (most recent call last) in () ----> 1 net_input = get_noise(input_depth, INPUT, (imgs['HR_pil'].size[1], imgs['HR_pil'].size[0])).type(dtype).detach() 2 3 NET_TYPE = 'skip' # UNet, ResNet 4 net = get_net(input_depth, 'skip', pad, 5 skip_n33d=128,

    RuntimeError: CUDA error: out of memory

    opened by sanayam 5
  • CUDA out of memory

    CUDA out of memory

    I successfully tested your inpainting algorithm for the kate.png and peppers.png on my own image (I changed only this:) elif ('kate.png' in img_path) or ('peppers.png' in img_path) or ('normal.png' in img_path)

    Unfortunately on trying again with another image, I'm getting this error on the main loop:

    Starting optimization with ADAM
    
    ---------------------------------------------------------------------------
    
    RuntimeError                              Traceback (most recent call last)
    
    <ipython-input-13-a43cee3b5493> in <module>()
         31 
         32 p = get_params(OPT_OVER, net, net_input)
    ---> 33 optimize(OPTIMIZER, p, closure, LR, num_iter)
    
    10 frames
    
    /usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps)
       2014     return torch.batch_norm(
       2015         input, weight, bias, running_mean, running_var,
    -> 2016         training, momentum, eps, torch.backends.cudnn.enabled
       2017     )
       2018 
    
    RuntimeError: CUDA out of memory. Tried to allocate 684.00 MiB (GPU 0; 15.75 GiB total capacity; 13.88 GiB already allocated; 124.88 MiB free; 14.47 GiB reserved in total by PyTorch)
    
    

    I have tried restarting the runtime twice and ran torch.cuda.empty_cache() but apparently the memory is still allocated. Would you mind telling a newbie what's going on and how to resolve this?

    opened by Tetsuo7945 4
  • Replacement for the image of Lena

    Replacement for the image of Lena

    This is not a technical bug, but an important matter you want to address.

    In the selection of example images, you have included the photo known as Lena (or Lenna).

    While I'm guessing you might want to show the behavior of your algorithm on human faces, the choice of the image in this case is quite unfortunate and widely discussed as an unwanted picture in today's image processing sets.

    Inspired by other projects, please consider using one of these images instead:

    Thank you for your help and addressing this report with the most careful attention.

    Have a nice day

    opened by xuv 4
  • import error

    import error

    Hi, @DmitryUlyanov ,

    I got the following import error, which shows it cannot find skip. I think that's path pointing issue. Would you give some suggestion to fix it? THX!

    ModuleNotFoundError                       Traceback (most recent call last)
    <ipython-input-1-9abca6442a47> in <module>()
          7 
          8 import numpy as np
    ----> 9 from models import *
         10 
         11 import torch
    
    /data/code/deep-image-prior/models/__init__.py in <module>()
    ----> 1 from skip import skip
          2 from texture_nets import get_texture_nets
          3 from resnet import ResNet
          4 # from model_pad_ds import Net
          5 from unet import UNet
    
    ModuleNotFoundError: No module named 'skip'
    
    opened by amiltonwong 4
  • Flash, no Flash

    Flash, no Flash

    Do you have the example for the flash, no flash figure? In the paper, it says you used the same parameters as the denoising example. Is this correct?

    Thanks

    opened by nikky4D 3
  • Torch CUDA

    Torch CUDA

    IMO the install section should mention that pytorch needs to be compiled with CUDA support, which is quite fiddly on some macOS versions (needing to downgrade clang to compile at all, since nvcc doesn't like clang 90000).

    opened by turbo 3
  • Images should have 1 or 3 channels

    Images should have 1 or 3 channels

    I'm trying to test with my own image and mask and running into an issue on : plot_image_grid([img_np, img_mask_np, img_mask_np*img_np], 3,11);

    assert (n_channels == 3) or (n_channels == 1), "images should have 1 or 3 channels"

    I've ensured that my image only has 3 channels i.e. RGB, and removed the alpha channel. But I"m guessing maybe this isn't what it's referring to?

    opened by Tetsuo7945 2
  • ValueError: operands could not be broadcast together with shapes

    ValueError: operands could not be broadcast together with shapes

    ValueError Traceback (most recent call last)

    in () 1 img_mask_var = np_to_torch(img_mask_np).type(dtype) 2 ----> 3 plot_image_grid([img_np, img_mask_np, img_mask_np*img_np], 3,11);

    ValueError: operands could not be broadcast together with shapes (4,320,320) (3,320,320)

    I have that strange error, does anyone know how to fix it?

    opened by avmiz 2
  • Error while backtracking in restoration notebook

    Error while backtracking in restoration notebook

    Backtracking condition fails and gives an error.

    Falling back to previous checkpoint.R_masked 11.899794 PSNR 11.899794 
    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-16-f44ea9e7035f> in <module>()
         50 # Run
         51 p = get_params(OPT_OVER, net, net_input)
    ---> 52 optimize(OPTIMIZER, p, closure, LR=LR, num_iter=num_iter)
    
    /nfs1/code/aniruddha/deep-image-prior/utils/common_utils.py in optimize(optimizer_type, parameters, closure, LR, num_iter)
        227         for j in range(num_iter):
        228             optimizer.zero_grad()
    --> 229             closure()
        230             optimizer.step()
        231     else:
    
    <ipython-input-16-f44ea9e7035f> in closure()
         25 
         26             for new_param, net_param in zip(last_net, net.parameters()):
    ---> 27                 net_param.copy_(new_param.cuda())
         28 
         29             return total_loss*0
    
    RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
    

    I could reproduce this multiple times.

    opened by ani0075 2
  • Batch Normalization

    Batch Normalization

    There are a lot of Batch Normalization layers in the models, but the code only uses batch size of 1. What role does the BN play in the model? Have you tried training without BN and how did it perform?

    opened by joeyhng 2
  • loss function on the inpaiting notebook with the groundtruth image on it

    loss function on the inpaiting notebook with the groundtruth image on it

    There is an error on the inpaiting notebook. Because on the loss function (MSE) you use the corrupted image and also the ground truth img_var) image, so the model is always learning knowing the final image.

    opened by PabloMesas 0
  • ImportError: cannot import name 'compare_psnr' from 'skimage.measure'

    ImportError: cannot import name 'compare_psnr' from 'skimage.measure'

    For superresolution.py and denoising.py

    I believe this function was renamed in release 0.16 and the older compare_psnr name was removed in 0.18. You should be able to import under the new name with: from skimage.metrics import peak_signal_noise_ratio

    opened by malonec3 1
  • It really surprised me a lot

    It really surprised me a lot

    after my "training" on restoration task, I am curious about the loss function -- mse, the parameters including both restoration generated image and ground truth, some minutes after, a function training was finished, and when I add a watermark different from further one, result as following:

    Screenshot from 2022-09-15 14-27-52

    last, as far as I am concerned, this algorithm is unrealistic, on the other hand, maybe I have mistaken something because I just met this project yesterday

    opened by Breezewrf 1
  • Restoration on Mac M1 ?

    Restoration on Mac M1 ?

    Hi, I'm using deep prior in astronomy processing. This is an incredible process to remove random noise without introducing artefacts like a trained algorithm would do. I always have out of memory problems and Large GPU are too expansive. I have a very nice 32Gb GPU on my Mac M1 Max. Would anyone can give me some help to drop CUDA and make it work on a Mac M1 ?

    One of my image that used deep prior was featured in NASA's Astronomy Picture Of the Day ! https://apod.nasa.gov/apod/ap191127.html

    opened by AstroBB 2
  • What is the skip net used for inpainting?

    What is the skip net used for inpainting?

    Hi. Can you provide some refs or papers of the skip net used for inpainting? I want to know its structure and the difference between skip-net and unet.

    opened by daoyuli 0
  • Error inpainting for CUDA

    Error inpainting for CUDA

    Hi, In inpainting.ipynb the loss is NAN from the beginning. However, if I turn off cuda:

      torch.backends.cudnn.enabled = False 
      torch.backends.cudnn.benchmark = False 
      dtype = torch.FloatTensor 
    

    All the loss is right. I was using RTX 3070 Ti, cuda 9.0.

    Why the loss turns into 'nan' when running on GPU? Please help me with this problem.

    opened by June-phy 0
Owner
Dmitry Ulyanov
Co-Founder at in3D, Phd @ Skoltech
Dmitry Ulyanov
This is an implementation for the CVPR2020 paper "Learning Invariant Representation for Unsupervised Image Restoration"

Learning Invariant Representation for Unsupervised Image Restoration (CVPR 2020) Introduction This is an implementation for the paper "Learning Invari

GarField 88 Nov 7, 2022
Dynamic Attentive Graph Learning for Image Restoration, ICCV2021 [PyTorch Code]

Dynamic Attentive Graph Learning for Image Restoration This repository is for GATIR introduced in the following paper: Chong Mou, Jian Zhang, Zhuoyuan

Jian Zhang 84 Dec 9, 2022
Multi-Stage Progressive Image Restoration

Multi-Stage Progressive Image Restoration Syed Waqas Zamir, Aditya Arora, Salman Khan, Munawar Hayat, Fahad Shahbaz Khan, Ming-Hsuan Yang, and Ling Sh

Syed Waqas Zamir 859 Dec 22, 2022
(under submission) Bayesian Integration of a Generative Prior for Image Restoration

BIGPrior: Towards Decoupling Learned Prior Hallucination and Data Fidelity in Image Restoration Authors: Majed El Helou, and Sabine Süsstrunk {Note: p

Majed El Helou 22 Dec 17, 2022
HINet: Half Instance Normalization Network for Image Restoration

HINet: Half Instance Normalization Network for Image Restoration Liangyu Chen, Xin Lu, Jie Zhang, Xiaojie Chu, Chengpeng Chen Paper: https://arxiv.org

null 303 Dec 31, 2022
EDPN: Enhanced Deep Pyramid Network for Blurry Image Restoration

EDPN: Enhanced Deep Pyramid Network for Blurry Image Restoration Ruikang Xu, Zeyu Xiao, Jie Huang, Yueyi Zhang, Zhiwei Xiong. EDPN: Enhanced Deep Pyra

null 69 Dec 15, 2022
Real-ESRGAN aims at developing Practical Algorithms for General Image Restoration.

Real-ESRGAN Colab Demo for Real-ESRGAN . Portable Windows executable file. You can find more information here. Real-ESRGAN aims at developing Practica

Xintao 17.2k Jan 2, 2023
SwinIR: Image Restoration Using Swin Transformer

SwinIR: Image Restoration Using Swin Transformer This repository is the official PyTorch implementation of SwinIR: Image Restoration Using Shifted Win

Jingyun Liang 2.4k Jan 8, 2023
Image Restoration Toolbox (PyTorch). Training and testing codes for DPIR, USRNet, DnCNN, FFDNet, SRMD, DPSR, BSRGAN, SwinIR

Image Restoration Toolbox (PyTorch). Training and testing codes for DPIR, USRNet, DnCNN, FFDNet, SRMD, DPSR, BSRGAN, SwinIR

Kai Zhang 2k Dec 31, 2022
An official repository for Paper "Uformer: A General U-Shaped Transformer for Image Restoration".

Uformer: A General U-Shaped Transformer for Image Restoration Zhendong Wang, Xiaodong Cun, Jianmin Bao and Jianzhuang Liu Paper: https://arxiv.org/abs

Zhendong Wang 497 Dec 22, 2022
Image Restoration Using Swin Transformer for VapourSynth

SwinIR SwinIR function for VapourSynth, based on https://github.com/JingyunLiang/SwinIR. Dependencies NumPy PyTorch, preferably with CUDA. Note that t

Holy Wu 11 Jun 19, 2022
Half Instance Normalization Network for Image Restoration

HINet Half Instance Normalization Network for Image Restoration, based on https://github.com/megvii-model/HINet. Dependencies NumPy PyTorch, preferabl

Holy Wu 4 Jun 6, 2022
The 7th edition of NTIRE: New Trends in Image Restoration and Enhancement workshop will be held on June 2022 in conjunction with CVPR 2022.

NTIRE 2022 - Image Inpainting Challenge Important dates 2022.02.01: Release of train data (input and output images) and validation data (only input) 2

Andrés Romero 37 Nov 27, 2022
Like a cowsay but without cows!

Foxsay This is a simple program that generates pictures of a cute fox with a message. It is like a cowsay but without cows! Fox girls are better! Usag

Anastasia Kim 28 Feb 20, 2022
[CVPRW 21] "BNN - BN = ? Training Binary Neural Networks without Batch Normalization", Tianlong Chen, Zhenyu Zhang, Xu Ouyang, Zechun Liu, Zhiqiang Shen, Zhangyang Wang

BNN - BN = ? Training Binary Neural Networks without Batch Normalization Codes for this paper BNN - BN = ? Training Binary Neural Networks without Bat

VITA 40 Dec 30, 2022
Plenoxels: Radiance Fields without Neural Networks

Plenoxels: Radiance Fields without Neural Networks Alex Yu*, Sara Fridovich-Keil*, Matthew Tancik, Qinhong Chen, Benjamin Recht, Angjoo Kanazawa UC Be

Sara Fridovich-Keil 81 Dec 25, 2022
Old Photo Restoration (Official PyTorch Implementation)

Bringing Old Photo Back to Life (CVPR 2020 oral)

Microsoft 11.3k Dec 30, 2022
Implementation for "Exploiting Aliasing for Manga Restoration" (CVPR 2021)

[CVPR Paper](To appear) | [Project Website](To appear) | BibTex Introduction As a popular entertainment art form, manga enriches the line drawings det

null 133 Dec 15, 2022
Punctuation Restoration using Transformer Models for High-and Low-Resource Languages

Punctuation Restoration using Transformer Models This repository contins official implementation of the paper Punctuation Restoration using Transforme

Tanvirul Alam 142 Jan 1, 2023