Code Monkey home page Code Monkey logo

deflow's Introduction

DeFlow: Learning Complex Image Degradations from Unpaired Data with Conditional Flows

Official implementation of the paper DeFlow: Learning Complex Image Degradations from Unpaired Data with Conditional Flows

[Paper] CVPR 2021 Oral

Watch our oral presentation:
DeFlow CVPR presentation video

Setup and Installation

# create and activate new conda environment
conda create --name DeFlow python=3.7.9
conda activate DeFlow

# install pytorch 1.6 (untested with different versions)
conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.1 -c pytorch
# install required packages
pip install pyyaml imageio natsort opencv-python scikit-image tqdm jupyter psutil tensorboard

# clone the repository
git clone https://github.com/volflow/DeFlow.git
cd ./DeFlow/

Dataset Preparation

We provide bash scripts that download and prepare the AIM-RWSR, NTIRE-RWSR, and DPED-RWSR datasets. The script generates all the downsampled images required by DeFlow in advance for faster training.

Validation datasets

cd ./datasets
bash get-AIM-RWSR-val.sh 
bash get-NTIRE-RWSR-val.sh 
bash get-DPED-RWSR-val.sh 

Training datasets

cd ./datasets
bash get-AIM-RWSR-train.sh 
bash get-NTIRE-RWSR-train.sh 

DPED dataset
For the DPED-RWSR dataset, we followed the approach of https://github.com/jixiaozhong/RealSR and used KernelGAN https://github.com/sefibk/KernelGAN to estimate and apply blur kernels to the downsampled high-quality images. DeFlow is then trained with these blurred images. More detailed instructions on this will be added here soon.

Trained Models

DeFlow Models
To download the trained DeFlow models run:

cd ./trained_models/
bash get-DeFlow-models.sh 

Pretrained RRDB models
To download the pretrained RRDB models used for training run:

cd ./trained_models/
bash get-RRDB-models.sh 

ESRGAN Models
We use the degradations generated by DeFlow as synthetic training data to train ESRGAN image enhancement models. These models are then able to simultaneously 4x upscale and restore noisy images.
To download these pretrained ESRGAN models run:

cd ./trained_models/
bash get-ESRGAN-models.sh 

Validate Pretrained DeFlow Models

  1. Download and prepare the corresponding validation datasets (see above)
  2. Download the pretrained DeFlow models (see above)
  3. Run the below codes to validate the model on the images of the validation set:
cd ./codes
CUDA_VISIBLE_DEVICES=-1 python validate.py -opt DeFlow-AIM-RWSR.yml -model_path ../trained_models/DeFlow_models/DeFlow-AIM-RWSR-100k.pth -crop_size 256 -n_max 5;
CUDA_VISIBLE_DEVICES=-1 python validate.py -opt DeFlow-NTIRE-RWSR.yml -model_path ../trained_models/DeFlow_models/DeFlow-NTIRE-RWSR-100k.pth -crop_size 256 -n_max 5;

If your GPU has enough memory or -crop_size is set small enough you can remove CUDA_VISIBLE_DEVICES=-1 from the above commands to run the validation on your GPU.

The resulting images are saved to a subfolder in ./results/ which again contains four subfolders:

  • /0_to_1/ contains images from domain X (clean) translated to domain Y (noisy). This adds the synthetic degradations
  • /1_to_0/ contains images from domain Y (noisy) translated to domain X (clean). This reverses the degradation model and shows some denoising performance
  • /0_gen/ and the /1_gen/ folders contain samples from the conditional distributions p_X(x|h(x)) and p_Y(x|h(x)), respectively

Generate Synthetic Datasets for Downstream ESRGAN models

To apply the DeFlow degradation model to a folder of high-quality images use the translate.py script. For example to generate the degraded low-resolution images for the AIM-RWSR dataset that we used to train our ESRGAN model run:

## download dataset if not already done
# cd ./datasets
# bash get-AIM-RWSR-train.sh
# cd ..
cd ./codes
CUDA_VISIBLE_DEVICES=-1 python translate.py -opt DeFlow-AIM-RWSR.yml -model_path ../trained_models/DeFlow_models/DeFlow-AIM-RWSR-100k.pth -source_dir ../datasets/AIM-RWSR/train-clean-images/4x/ -out_dir ../datasets/AIM-RWSR/train-clean-images/4x_degraded/

Downstream ESRGAN Models

We use the code from https://github.com/jixiaozhong/RealSR to train and test the ESRGAN models on the high-resolution /1x/ and low-resolution /4x_degraded/ data that we showed how to generate in the previous section.

ESRGAN Setup
First you need to clone the RealSR repository and install its additional dependencies:

cd DeFlow 
# clone the Real-SR repo into the DeFlow folder
git clone https://github.com/Tencent/Real-SR.git

# install additional dependencies for Real-SR
pip install lmdb

ESRGAN Model Validation
First you need to download the vaildation datasets with the provided scripts as explained above.
Then run Real-SR/codes/test.py script with the corresponding config file:

# download trained ESRGAN models
cd trained_models
bash get-ESRGAN-models.sh 

# run testing scripts (see below for instructions to run on CPU)
cd ../Real-SR/codes/
python3 test.py -opt ../../ESRGAN_confs/test/ESRGAN-DeFlow-AIM-RWSR.yml
python3 test.py -opt ../../ESRGAN_confs/test/ESRGAN-DeFlow-NTIRE-RWSR.yml
python3 test.py -opt ../../ESRGAN_confs/test/ESRGAN-DeFlow-DPED-RWSR.yml

The resulting upscaled and restored images can then be found in DeFlow/Real-SR/results/.

To run the testing code without a GPU or if you get CUDA out-of-memory you can change line 11 in DeFlow/Real-SR/codes/models/base_model.py to self.device = 'cpu' and prepend CUDA_VISIBLE_DEVICES=-1 to the commands.

ESRGAN Training
First you need to generate the synthetic training data as explained above.
Then run Real-SR/codes/train.py script with the corresponding config file:

# run training
cd Real-SR/codes/
python3 train.py -opt ../../ESRGAN_confs/train/ESRGAN-DeFlow-AIM-RWSR.yml
python3 train.py -opt ../../ESRGAN_confs/train/ESRGAN-DeFlow-NTIRE-RWSR.yml

Instructions on training the ESRGAN with the DPED-RWSR dataset will be here added soon.

Training DeFlow

  1. Download and prepare the corresponding training datasets (see above)
  2. Download and prepare the corresponding validation datasets (see above)
  3. Download the pretrained RRDB models (see above)
  4. Run the provided train.py script with the corresponding configs
cd code
python train.py -opt ./confs/DeFlow-AIM-RWSR.yml
python train.py -opt ./confs/DeFlow-NTIRE-RWSR.yml

If you run out of GPU memory you can reduce the batch size or the patch size in the config files. To train without a GPU prefix the commands with CUDA_VISIBLE_DEVICES=-1.

Instructions for training DeFlow on the DPED dataset will be added here soon.

To train DeFlow on other datasets simply create your own config file and change the dataset paths accordingly. To pre-generate the downsampled images that are used as conditional features by DeFlow you can use the ./datasets/create_DeFlow_train_dataset.py script.

Citation

[Paper] CVPR 2021 Oral

@inproceedings{wolf2021deflow,
    author    = {Valentin Wolf and
                Andreas Lugmayr and
                Martin Danelljan and
                Luc Van Gool and
                Radu Timofte},
    title     = {DeFlow: Learning Complex Image Degradations from Unpaired Data with Conditional Flows},
    booktitle = {{IEEE/CVF} Conference on Computer Vision and Pattern Recognition, {CVPR}},
    year      = {2021},
    url       = {https://arxiv.org/abs/2101.05796}
}

deflow's People

Contributors

volflow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deflow's Issues

About the cov_shift in the model.

Why the conv_shift in your "DeFlow-DPED-RWSR-100k.pth" is 0? And I retrian Deflow on my own dataset, but the conv_shift is 0, too. Is it common? Why does this happen?Looking forward to your reply,thanks.

question about the process of sample

hello author, thank you for your contributions. I have a question about the process of sample in your work. you assume that Zx ~N(0,I), and Zy = Zx + u. So in the process of sample, Can I directly let Zx ~N(0, I) rather than encoding x. I am looking forward to your reply.

Training on custom dataset

Hello
How are you?
Thanks contributing to this project.
I am going to train a DeFlow model on my custom dataset consisting of UNPAIRED low-quality images and high-quality images.
Could you provide templates of config file and dataset file?

Applying to low-quality image generation

Hello
How are you?
Thanks for contributing to this project.
It looks that this method did NOT consider out-of-focus blur or motion blur as low-quality.
Is it possible to use this method for low-quality blurry image generation?
if so, could u guide me how to train a DeFlow model on unpaired dataset consisting high-quality images and low-quality images?
In fact, I looked at your code for training a DeFlow model on my custom dataset.
But the configuration file looks likely to be used for training image super-resolution model rather than simple high-quality-to-low quality generator.

image

Some issues about the NLL loss function

Thanks for sharing your excellent work. I have a basic question about the NLL loss function.

By using Change of Variable Theory, we can reformulate the NLL p(x) = p(z) * abs(det(inv(Jacobian))), and I did found calculating the second term in your code. But I have a question about whether your did calculate the first term on the right of the equation.

The problem is basic, but I have been confused about it for quite a long time. Do we need to calculate the probability of z (in theory, we need to calculate the inverse of x and find the corresponding z which obeys N(0, I). And we need to calculate the probability of sampling such exact z)?

If we need to calculate the term, could you please tell me which period of code you used?

Looking forward to your reply.

Training on my data sometimes breaks on maximum number of Nans of the NLL (100)

I am training DeFlow on my own generated data.
During the train I sometimes get a raise due to reaching maximum number of Nans of the NLL (100).
Also, on some of my validation images, I get Nans during training, and also on translate.py (and results are of course bad).
Do you have an idea of the root cause of those Nans, and maybe an idea of how to resolve them?
Thanks,
Mani

can you share RRDB_PSNR_x16.pth pre-trained model?

Hi,
in my data the difference between LQ and HQ data is really big.
so in order that h() will not indicate from which domain (x, y) it comes from I need to do a very strong down-sampling (x8 is not enough).
you have already shared RRDB_PSNR_x8.pth and RRDB_PSNR_x4.pth pre-trained models, can you share RRDB_PSNR_x16.pth as well?
Thanks,
Mani

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.