Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bahjat-kawar committed Nov 2, 2022
0 parents commit e3704ce
Show file tree
Hide file tree
Showing 77 changed files with 5,098 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright [2022] [Bahjat Kawar, Jiaming Song, Stefano Ermon, Michael Elad]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# JPEG Artifact Correction using Denoising Diffusion Restoration Models

[arXiv](https://arxiv.org/abs/2209.11888) | [PDF](https://arxiv.org/pdf/2209.11888.pdf)

[Bahjat Kawar](https://bahjat-kawar.github.io/)<sup>\* 1</sup>, [Jiaming Song](http://tsong.me)<sup>\* 2</sup>, [Stefano Ermon](http://cs.stanford.edu/~ermon)<sup>3</sup>, [Michael Elad](https://elad.cs.technion.ac.il/)<sup>1</sup><br />
<sup>1</sup> Technion, <sup>2</sup>NVIDIA, <sup>3</sup>Stanford University, <sup>\*</sup>Equal contribution.

![](assets/ddrm-jpeg-demo.png)


We extend DDRM (Denoising Diffusion Restoration Models) for the problems of JPEG artifact correction and image dequantization.

## Running the Experiments
The code has been tested on PyTorch 1.8 and PyTorch 1.10. Please refer to `environment.yml` for a list of conda/mamba environments that can be used to run the code. The codebase is based heavily on the [original DDRM codebase](https://github.com/bahjat-kawar/ddrm).

### Pretrained models
We use pretrained models from [https://github.com/openai/guided-diffusion](https://github.com/openai/guided-diffusion), [https://github.com/pesser/pytorch_diffusion](https://github.com/pesser/pytorch_diffusion) and [https://github.com/ermongroup/SDEdit](https://github.com/ermongroup/SDEdit)

We use 1,000 images from the ImageNet validation set for comparison with other methods. The list of images is taken from [https://github.com/XingangPan/deep-generative-prior/](https://github.com/XingangPan/deep-generative-prior/)

The models and datasets are placed in the `exp/` folder as follows:
```bash
<exp> # a folder named by the argument `--exp` given to main.py
├── datasets # all dataset files
│ ├── celeba # all CelebA files
│ ├── imagenet # all ImageNet files
│ ├── ood # out of distribution ImageNet images
│ ├── ood_bedroom # out of distribution bedroom images
│ ├── ood_cat # out of distribution cat images
│ └── ood_celeba # out of distribution CelebA images
├── logs # contains checkpoints and samples produced during training
│ ├── celeba
│ │ └── celeba_hq.ckpt # the checkpoint file for CelebA-HQ
│ ├── diffusion_models_converted
│ │ └── ema_diffusion_lsun_<category>_model
│ │ └── model-x.ckpt # the checkpoint file saved at the x-th training iteration
│ ├── imagenet # ImageNet checkpoint files
│ │ ├── 256x256_classifier.pt
│ │ ├── 256x256_diffusion.pt
│ │ ├── 256x256_diffusion_uncond.pt
│ │ ├── 512x512_classifier.pt
│ │ └── 512x512_diffusion.pt
├── image_samples # contains generated samples
└── imagenet_val_1k.txt # list of the 1k images used in ImageNet-1K.
```

### Sampling from the model

The general command to sample from the model is as follows:
```
python main.py --ni --config {CONFIG}.yml --doc {DATASET} -i {IMAGE_FOLDER} --timesteps {STEPS} --init_timestep {INIT_T} --eta {ETA} --etaB {ETA_B} --deg {DEGRADATION} --num_avg_samples {NUM_AVG}
```
where the following are options
- `ETA` is the eta hyperparameter in the paper. (default: `1`)
- `ETA_B` is the eta_b hyperparameter in the paper. (default: `0.4`)
- `STEPS` controls how many timesteps used in the process. (default: `20`)
- `INIT_T` controls the timestep to start sampling from. (default: `300`)
- `NUM_AVG` is the number of samples per input to average for the final result. (default: `1`)
- `DEGREDATION` is the type of degredation used. (One of: `quant` for dequantization, or `jpegXX` for JPEG with quality factor `XX`, e.g. `jpeg80`)
- `CONFIG` is the name of the config file (see `configs/` for a list), including hyperparameters such as batch size and network architectures.
- `DATASET` is the name of the dataset used, to determine where the checkpoint file is found.
- `IMAGE_FOLDER` is the name of the folder the resulting images will be placed in (default: `images`)

For example, to use the default settings from the paper on the ImageNet 256x256 dataset, the problem of JPEG artifact correction for QF=80, and averaging 8 samples per input:
```
python main.py --ni --config imagenet_256.yml --doc imagenet -i imagenet --deg jpeg80 --num_avg_samples 8
```
The generated images are place in the `<exp>/image_samples/{IMAGE_FOLDER}` folder, where `orig_{id}.png`, `y0_{id}.png`, `{id}_-1.png` refer to the original, degraded, restored images respectively.

The config files contain a setting controlling whether to test on samples from the trained dataset's distribution or not.

## References and Acknowledgements
```
@inproceedings{kawar2022jpeg,
title={JPEG Artifact Correction using Denoising Diffusion Restoration Models},
author={Bahjat Kawar and Jiaming Song and Stefano Ermon and Michael Elad},
booktitle={Neural Information Processing Systems (NeurIPS) Workshop on Score-Based Methods},
year={2022}
}
```

```
@inproceedings{kawar2022denoising,
title={Denoising Diffusion Restoration Models},
author={Bahjat Kawar and Michael Elad and Stefano Ermon and Jiaming Song},
booktitle={Advances in Neural Information Processing Systems},
year={2022}
}
```

This implementation is based on / inspired by [https://github.com/bahjat-kawar/ddrm](https://github.com/bahjat-kawar/ddrm)

## License

The code is released under the MIT License.
Binary file added assets/ddrm-jpeg-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions configs/bedroom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
data:
dataset: "LSUN"
category: "bedroom"
image_size: 256
channels: 3
logit_transform: false
uniform_dequantization: false
gaussian_dequantization: false
random_flip: true
rescaled: true
num_workers: 32
out_of_dist: true

model:
type: "simple"
in_channels: 3
out_ch: 3
ch: 128
ch_mult: [1, 1, 2, 2, 4, 4]
num_res_blocks: 2
attn_resolutions: [16, ]
dropout: 0.0
var_type: fixedsmall
ema_rate: 0.999
ema: True
resamp_with_conv: True

diffusion:
beta_schedule: linear
beta_start: 0.0001
beta_end: 0.02
num_diffusion_timesteps: 1000

training:
batch_size: 64
n_epochs: 10000
n_iters: 5000000
snapshot_freq: 5000
validation_freq: 2000

sampling:
batch_size: 6
last_only: True

optim:
weight_decay: 0.000
optimizer: "Adam"
lr: 0.00002
beta1: 0.9
amsgrad: false
eps: 0.00000001
51 changes: 51 additions & 0 deletions configs/cat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
data:
dataset: "LSUN"
category: "cat"
image_size: 256
channels: 3
logit_transform: false
uniform_dequantization: false
gaussian_dequantization: false
random_flip: true
rescaled: true
num_workers: 32
out_of_dist: false

model:
type: "simple"
in_channels: 3
out_ch: 3
ch: 128
ch_mult: [1, 1, 2, 2, 4, 4]
num_res_blocks: 2
attn_resolutions: [16, ]
dropout: 0.0
var_type: fixedsmall
ema_rate: 0.999
ema: True
resamp_with_conv: True

diffusion:
beta_schedule: linear
beta_start: 0.0001
beta_end: 0.02
num_diffusion_timesteps: 1000

training:
batch_size: 64
n_epochs: 10000
n_iters: 5000000
snapshot_freq: 5000
validation_freq: 2000

sampling:
batch_size: 32
last_only: True

optim:
weight_decay: 0.000
optimizer: "Adam"
lr: 0.00002
beta1: 0.9
amsgrad: false
eps: 0.00000001
36 changes: 36 additions & 0 deletions configs/celeba_hq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
data:
dataset: "CelebA_HQ"
category: ""
image_size: 256
channels: 3
logit_transform: false
uniform_dequantization: false
gaussian_dequantization: false
random_flip: true
rescaled: true
num_workers: 32
out_of_dist: True

model:
type: "simple"
in_channels: 3
out_ch: 3
ch: 128
ch_mult: [1, 1, 2, 2, 4, 4]
num_res_blocks: 2
attn_resolutions: [16, ]
dropout: 0.0
var_type: fixedsmall
ema_rate: 0.999
ema: True
resamp_with_conv: True

diffusion:
beta_schedule: linear
beta_start: 0.0001
beta_end: 0.02
num_diffusion_timesteps: 1000

sampling:
batch_size: 4
last_only: True
51 changes: 51 additions & 0 deletions configs/church.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
data:
dataset: "LSUN"
category: "church_outdoor"
image_size: 256
channels: 3
logit_transform: false
uniform_dequantization: false
gaussian_dequantization: false
random_flip: true
rescaled: true
num_workers: 32
out_of_dist: true

model:
type: "simple"
in_channels: 3
out_ch: 3
ch: 128
ch_mult: [1, 1, 2, 2, 4, 4]
num_res_blocks: 2
attn_resolutions: [16, ]
dropout: 0.0
var_type: fixedsmall
ema_rate: 0.999
ema: True
resamp_with_conv: True

diffusion:
beta_schedule: linear
beta_start: 0.0001
beta_end: 0.02
num_diffusion_timesteps: 1000

training:
batch_size: 64
n_epochs: 10000
n_iters: 5000000
snapshot_freq: 5000
validation_freq: 2000

sampling:
batch_size: 6
last_only: True

optim:
weight_decay: 0.000
optimizer: "Adam"
lr: 0.00002
beta1: 0.9
amsgrad: false
eps: 0.00000001
43 changes: 43 additions & 0 deletions configs/imagenet_256.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
data:
dataset: "ImageNet"
image_size: 256
channels: 3
logit_transform: false
uniform_dequantization: false
gaussian_dequantization: false
random_flip: true
rescaled: true
num_workers: 32
subset_1k: True
out_of_dist: False

model:
type: "openai"
in_channels: 3
out_channels: 3
num_channels: 256
num_heads: 4
num_res_blocks: 2
attention_resolutions: "32,16,8"
dropout: 0.0
resamp_with_conv: True
learn_sigma: True
use_scale_shift_norm: true
use_fp16: true
resblock_updown: true
num_heads_upsample: -1
var_type: 'fixedsmall'
num_head_channels: 64
image_size: 256
class_cond: false
use_new_attention_order: false

diffusion:
beta_schedule: linear
beta_start: 0.0001
beta_end: 0.02
num_diffusion_timesteps: 1000

sampling:
batch_size: 8
last_only: True
Loading

0 comments on commit e3704ce

Please sign in to comment.