README.md 6.45 KB
Newer Older
mashun1's avatar
diffbir  
mashun1 committed
1
# DiffBIR
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
2

mashun1's avatar
diffbir  
mashun1 committed
3
## 论文
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
4

mashun1's avatar
diffbir  
mashun1 committed
5
**DiffBIR: Towards Blind Image Restoration with Generative Diffusion Prior**
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
6

mashun1's avatar
diffbir  
mashun1 committed
7
* https://arxiv.org/abs/2308.15070
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
8

mashun1's avatar
diffbir  
mashun1 committed
9
## 模型结构
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
10

dcuai's avatar
dcuai committed
11
第一阶段模型使用8个Swin Transformer blocks(RSTB),每个RSTB中包含6个Swin Transformer Layers(STL),其中head数为6,window size为8。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
12

mashun1's avatar
diffbir  
mashun1 committed
13
第二阶段模型基于Stable Diffusioin 2.1-base,创建了一个与Unet中encoder block与middle block相同的网络。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
14

mashun1's avatar
diffbir  
mashun1 committed
15
![Alt text](images/image.png)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
16

mashun1's avatar
diffbir  
mashun1 committed
17
## 算法原理
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
18

mashun1's avatar
diffbir  
mashun1 committed
19
用途:该算法为两阶段算法,可以提升图像的分辨率。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
20

mashun1's avatar
diffbir  
mashun1 committed
21
第一阶段使用复原模块,从具有未知和复杂降质的低质量(LQ)图像中恢复清晰图像;
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
22

mashun1's avatar
mashun1 committed
23
24
![Alt text](images/s1.png)

mashun1's avatar
diffbir  
mashun1 committed
25
第二阶段使用生成模块来重新生成丢失的信息。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
26

mashun1's avatar
mashun1 committed
27
28
![Alt text](images/s2.png)

mashun1's avatar
diffbir  
mashun1 committed
29
## 环境配置
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
30

mashun1's avatar
diffbir  
mashun1 committed
31
### Docker(方法一)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
32

mashun1's avatar
mashun1 committed
33
    docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
mashun1's avatar
mashun1 committed
34
35
    docker run --shm-size 10g --network=host --name=diffbir --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v 项目地址(绝对路径):/home/ -it <your IMAGE ID> bash
    pip install -r requirements.txt
mashun1's avatar
mashun1 committed
36
    pip uninstall xformers
mashun1's avatar
mashun1 committed
37

mashun1's avatar
mashun1 committed
38
### Dockerfile(方法二)
mashun1's avatar
mashun1 committed
39

mashun1's avatar
diffbir  
mashun1 committed
40
41
42
43
    # 需要在对应的目录下
    docker build -t <IMAGE_NAME>:<TAG> .
    # <your IMAGE ID>用以上拉取的docker的镜像ID替换
    docker run -it --shm-size 10g --network=host --name=diffbir --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined <your IMAGE ID> bash
mashun1's avatar
mashun1 committed
44
    pip uninstall xformers
mashun1's avatar
mashun1 committed
45
   
mashun1's avatar
mashun1 committed
46
47


mashun1's avatar
diffbir  
mashun1 committed
48
49
50
### Anaconda (方法三)
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装:
https://developer.hpccube.com/tool/
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
51

mashun1's avatar
mashun1 committed
52
53
54
55
    DTK驱动:dtk24.04.1
    python:python3.10
    torch:2.1.0
    torchvision:0.16.1
56

mashun1's avatar
diffbir  
mashun1 committed
57
Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
58

mashun1's avatar
diffbir  
mashun1 committed
59
2、其它非特殊库参照requirements.txt安装
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
60

mashun1's avatar
diffbir  
mashun1 committed
61
    pip install -r requirements.txt
mashun1's avatar
mashun1 committed
62
    pip uninstall xformers
63

0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
64

mashun1's avatar
diffbir  
mashun1 committed
65
## 数据集
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
66

bailuo's avatar
bailuo committed
67
下载地址(训练+测试集):https://www.image-net.org/ (imagenet1k)\
bailuo's avatar
updata  
bailuo committed
68
或者从SCNet下载[imagenet1k](http://113.200.138.88:18080/aidatasets/project-dependency/imagenet-1k)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
69

mashun1's avatar
diffbir  
mashun1 committed
70
71
72
73
    datasets
        |- train
            |- n01440764
                |- xxx.JPEG
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
74

bailuo's avatar
bailuo committed
75
76
下载地址(训练+测试集):https://www.robots.ox.ac.uk/~vgg/data/pets/ \
或者从SCNet下载[vgg](http://113.200.138.88:18080/aidatasets/project-dependency/vgg)
mashun1's avatar
mashun1 committed
77
78
79
80
81
82

    datasets
        |- images
            |- xxx.JPG

注意:以上两个数据集都可以使用,可任选其一。
ziyannchen's avatar
ziyannchen committed
83

mashun1's avatar
diffbir  
mashun1 committed
84
## 训练
85

mashun1's avatar
diffbir  
mashun1 committed
86
### 阶段一
87

mashun1's avatar
diffbir  
mashun1 committed
88
89
90
91
92
93
1、数据准备:该操作用于生成训练以及验证数据路径列表

    python scripts/make_file_list.py \  
    --img_folder [hq_dir_path] \         # 包含图片的文件夹
    --val_size [validation_set_size] \   # 验证集大小
    --save_folder [save_dir_path] \      # 路径列表保存文件夹
ziyannchen's avatar
ziyannchen committed
94
    --follow_links
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
95

mashun1's avatar
diffbir  
mashun1 committed
96
97
98
2、修改配置文件

    修改 `configs/dataset`中相应的yaml配置文件
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
99

mashun1's avatar
diffbir  
mashun1 committed
100
    修改 `configs/train_swinir.yaml`配置文件
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
101

mashun1's avatar
mashun1 committed
102
注意:在修改`configs/dataset`中的配置文件时,可以选择修改"realesrgan"或"codeformer"中的任意一个。
mashun1's avatar
mashun1 committed
103

mashun1's avatar
diffbir  
mashun1 committed
104
105
106
3、训练

    python train.py --config [training_config_path]
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
107

mashun1's avatar
diffbir  
mashun1 committed
108
注意:该阶段训练得到的模型将用于第二阶段的训练。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
109

mashun1's avatar
diffbir  
mashun1 committed
110
### 阶段二
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
111

mashun1's avatar
mashun1 committed
112
113
114
115
116
117
118
119
1、模型准备

- (Stable Diffusion v2.1): https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-ema-pruned.ckpt \
或者从SCNet下载[stable-diffusion-2-1-base](http://113.200.138.88:18080/aimodels/stable-diffusion-2-1-base/-/raw/main/v2-1_512-ema-pruned.ckpt?      ref_type=heads&inline=false)

- [open_clip](http://113.200.138.88:18080/aimodels/CLIP-ViT-H-14-laion2B-s32B-b79K/-/blob/main/open_clip_pytorch_model.bin)

下载这些模型并放入`weights`文件夹中(需自行创建)。
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
120
121


mashun1's avatar
diffbir  
mashun1 committed
122
123
124
125
126
127
128
129
130
131
132
133
134
2、初始化模型参数

    python scripts/make_stage2_init_weight.py \
    --cldm_config configs/model/cldm.yaml \
    --sd_weight [sd_v2.1_ckpt_path] \
    --swinir_weight [swinir_ckpt_path] \  # 第一阶段训练得到的模型
    --output [init_weight_output_path]    # 初始化模型保存地址

3、修改配置文件

    修改`configs/train_cldm.yaml`配置文件

4、训练
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
135
136
137

    python train.py --config [training_config_path]

mashun1's avatar
diffbir  
mashun1 committed
138
## 推理
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
139

mashun1's avatar
diffbir  
mashun1 committed
140
### general Image
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
141

mashun1's avatar
diffbir  
mashun1 committed
142
模型下载地址:
bailuo's avatar
bailuo committed
143
144
* https://huggingface.co/lxq007/DiffBIR/resolve/main/general_full_v1.ckpt \
或者从SCNet下载[general_full_v1](http://113.200.138.88:18080/aimodels/DiffBIR/-/raw/main/general_full_v1.ckpt?ref_type=heads&inline=false)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
145

bailuo's avatar
bailuo committed
146
147
* https://huggingface.co/lxq007/DiffBIR/resolve/main/general_swinir_v1.ckpt \
或者从SCNet下载[general_swinir_v1](http://113.200.138.88:18080/aimodels/DiffBIR/-/raw/main/general_swinir_v1.ckpt?ref_type=heads&inline=false)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
148

mashun1's avatar
diffbir  
mashun1 committed
149
150
151
152
153
154
155
156
157
158
        python inference.py \
        --input inputs/demo/general \
        --config configs/model/cldm.yaml \
        --ckpt weights/general_full_v1.ckpt \  
        --reload_swinir --swinir_ckpt weights/general_swinir_v1.ckpt \  
        --steps 50 \
        --sr_scale 4 \
        --color_fix_type wavelet \
        --output results/demo/general \
        --device cuda [--tiled --tile_size 512 --tile_stride 256]
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
159

mashun1's avatar
diffbir  
mashun1 committed
160
注意:方括号中的参数为可选项,模型也可以替换为在训练阶段得到的
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
161

mashun1's avatar
diffbir  
mashun1 committed
162
### Face Image
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
163

mashun1's avatar
diffbir  
mashun1 committed
164
模型下载地址:
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
165

bailuo's avatar
bailuo committed
166
167
* https://huggingface.co/lxq007/DiffBIR/resolve/main/face_full_v1.ckpt \
或者从SCNet下载[face_full_v1](http://113.200.138.88:18080/aimodels/DiffBIR/-/raw/main/face_full_v1.ckpt?ref_type=heads&inline=false)
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
168

mashun1's avatar
diffbir  
mashun1 committed
169
for aligned face inputs
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
170

mashun1's avatar
diffbir  
mashun1 committed
171
172
173
174
175
176
177
178
179
180
181
    python inference_face.py \
    --input inputs/demo/face/aligned \
    --sr_scale 1 \
    --output results/demo/face/aligned \
    --has_aligned \
    --device cuda

for unaligned face inputs

    python inference_face.py \
    --input inputs/demo/face/whole_img \
mashun1's avatar
mashun1 committed
182
    --sr_scale 1 \
mashun1's avatar
diffbir  
mashun1 committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    --output results/demo/face/whole_img \
    --bg_upsampler DiffBIR \
    --device cuda

## result

恢复后的图像

![Alt text](images/samples_step-004900_e-000008_b-001203.png)

低质量图像

![Alt text](images/lq_step-004900_e-000008_b-001203.png)

### 精度



## 应用场景
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
202

mashun1's avatar
diffbir  
mashun1 committed
203
### 算法类别
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
204

mashun1's avatar
diffbir  
mashun1 committed
205
`图像超分`
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
206

mashun1's avatar
diffbir  
mashun1 committed
207
### 热点应用行业
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
208

mashun1's avatar
mashun1 committed
209
`媒体,科研,教育`
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
210

mashun1's avatar
diffbir  
mashun1 committed
211
## 源码仓库及问题反馈
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
212

mashun1's avatar
mashun1 committed
213
https://developer.hpccube.com/codes/modelzoo/diffbir_pytorch
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
214

mashun1's avatar
mashun1 committed
215
## 参考资料
0x3f3f3f3fun's avatar
0x3f3f3f3fun committed
216

mashun1's avatar
diffbir  
mashun1 committed
217
* https://github.com/XPixelGroup/DiffBIR
mashun1's avatar
mashun1 committed
218
* https://github.com/XPixelGroup/DiffBIR/issues/55