Home

Awesome

Image Super-Resolution via Iterative Refinement

Paper | Project

Brief

This is an unofficial implementation of Image Super-Resolution via Iterative Refinement(SR3) by PyTorch.

There are some implementation details that may vary from the paper's description, which may be different from the actual SR3 structure due to details missing. Specifically, we:

If you just want to upscale $(64 \times 64)\text{px} \rightarrow (512 \times 512)\text{px}$ images using the pre-trained model, check out this google colab script.

Status

★★★ NEW: The follow-up Palette-Image-to-Image-Diffusion-Models is now available; See the details here ★★★

Conditional Generation (with Super Resolution)

Unconditional Generation

Training Step

Results

Note: We set the maximum reverse steps budget to $2000$. We limited the model parameters in Nvidia 1080Ti, image noise and hue deviation occasionally appear in high-resolution images, resulting in low scores. There is a lot of room for optimization. We are welcome to any contributions for more extensive experiments and code enhancements.

Tasks/MetricsSSIM(+)PSNR(+)FID(-)IS(+)
16×16 -> 128×1280.67523.26--
64×64 -> 512×5120.44519.87--
128×128--
1024×1024--
<img src="./misc/sr_process_16_128_0.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_process_16_128_1.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_process_16_128_2.png" alt="show" style="zoom:90%;" />
<img src="./misc/sr_64_512_0_inf.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_64_512_0_sr.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_64_512_0_hr.png" alt="show" style="zoom:90%;" />
<img src="./misc/sr_64_512_1_sr.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_64_512_2_sr.png" alt="show" style="zoom:90%;" /><img src="./misc/sr_64_512_3_sr.png" alt="show" style="zoom:90%;" />
<img src="./misc/sample_process_128_0.png" alt="show" style="zoom:90%;" /><img src="./misc/sample_process_128_1.png" alt="show" style="zoom:90%;" /><img src="./misc/sample_process_128_2.png" alt="show" style="zoom:90%;" />

Usage

Environment

pip install -r requirement.txt

Pretrained Model

This paper is based on "Denoising Diffusion Probabilistic Models", and we build both DDPM/SR3 network structures, which use timesteps/gamma as model embedding inputs, respectively. In our experiments, the SR3 model can achieve better visual results with the same reverse steps and learning rate. You can select the JSON files with annotated suffix names to train the different models.

TasksPlatform(Code:qwer)
16×16 -> 128×128 on FFHQ-CelebaHQGoogle Drive|Baidu Yun
64×64 -> 512×512 on FFHQ-CelebaHQGoogle Drive|Baidu Yun
128×128 face generation on FFHQGoogle Drive|Baidu Yun
# Download the pretrained model and edit [sr|sample]_[ddpm|sr3]_[resolution option].json about "resume_state":
"resume_state": [your pretrained model's path]

Data Prepare

New Start

If you didn't have the data, you can prepare it by following steps:

Download the dataset and prepare it in LMDB or PNG format using script.

# Resize to get 16×16 LR_IMGS and 128×128 HR_IMGS, then prepare 128×128 Fake SR_IMGS by bicubic interpolation
python data/prepare_data.py  --path [dataset root]  --out [output root] --size 16,128 -l

then you need to change the datasets config to your data path and image resolution:

"datasets": {
    "train": {
        "dataroot": "dataset/ffhq_16_128", // [output root] in prepare.py script
        "l_resolution": 16, // low resolution need to super_resolution
        "r_resolution": 128, // high resolution
        "datatype": "lmdb", //lmdb or img, path of img files
    },
    "val": {
        "dataroot": "dataset/celebahq_16_128", // [output root] in prepare.py script
    }
},

Own Data

You also can use your image data by following steps, and we have some examples in dataset folder.

At first, you should organize the images layout like this, this step can be finished by data/prepare_data.py automatically:

# set the high/low resolution images, bicubic interpolation images path 
dataset/celebahq_16_128/
├── hr_128 # it's same with sr_16_128 directory if you don't have ground-truth images.
├── lr_16 # vinilla low resolution images
└── sr_16_128 # images ready to super resolution
# super resolution from 16 to 128
python data/prepare_data.py  --path [dataset root]  --out celebahq --size 16,128 -l

Note: Above script can be used whether you have the vanilla high-resolution images or not.

then you need to change the dataset config to your data path and image resolution:

"datasets": {
    "train|val": { // train and validation part
        "dataroot": "dataset/celebahq_16_128",
        "l_resolution": 16, // low resolution need to super_resolution
        "r_resolution": 128, // high resolution
        "datatype": "img", //lmdb or img, path of img files
    }
},

Training/Resume Training

# Use sr.py and sample.py to train the super resolution task and unconditional generation task, respectively.
# Edit json files to adjust network structure and hyperparameters
python sr.py -p train -c config/sr_sr3.json

Test/Evaluation

# Edit json to add pretrain model path and run the evaluation 
python sr.py -p val -c config/sr_sr3.json

# Quantitative evaluation alone using SSIM/PSNR metrics on given result root
python eval.py -p [result root]

Inference Alone

Set the image path like steps in Own Data, then run the script:

# run the script
python infer.py -c [config file]

Weights and Biases 🎉

The library now supports experiment tracking, model checkpointing and model prediction visualization with Weights and Biases. You will need to install W&B and login by using your access token.

pip install wandb

# get your access token from wandb.ai/authorize
wandb login

W&B logging functionality is added to the sr.py, sample.py and infer.py files. You can pass -enable_wandb to start logging.

You can find more on using these features here. 🚀

Acknowledgements

Our work is based on the following theoretical works:

Furthermore, we are benefitting a lot from the following projects: