SwinIR實戰:如何使用SwinIR和預訓練模型實現圖片的超分

語言: CN / TW / HK

持續創作,加速成長!這是我參與「掘金日新計劃 · 6 月更文挑戰」的第16天,點擊查看活動詳情

摘要

論文地址:http://arxiv.org/pdf/2108.10257.pdf

預訓練模型下載:http://github.com/JingyunLiang/SwinIR/releases

訓練代碼下載:http://github.com/cszn/KAIR

測試:http://github.com/JingyunLiang/SwinIR

論文翻譯:http://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/124434886

本文介紹如何使用SwinIR和預訓練模型實現圖像的超分。

測試

下載代碼:http://github.com/JingyunLiang/SwinIR,然後解壓,如下圖:

image-20220430151743517

然後下載預訓練權重文件,打開連接:http://github.com/JingyunLiang/SwinIR/releases,如下圖:

image-20220430154021976

預訓練權重有很多,選擇自己用到的預訓練權重下載,下載後將其放入./model_zoo/swinir文件夾下面,如下圖:

image-20220430155811191

然後執行命令:

python python main_test_swinir.py --task classical_sr --scale 2 --training_patch_size 48 --model_path model_zoo/swinir/001_classicalSR_DIV2K_s48w8_SwinIR-M_x2.pth --folder_lq testsets/Set5/LR_bicubic/X2 --folder_gt testsets/Set5/HR

task:任務類型,包括:classical_sr, lightweight_sr, real_sr, gray_dn, color_dn, jpeg_car。

scale:放大的倍數,這個和預訓練權重對應。比如這次設置的是2,對應權重的x2。

training_patch_size:用於訓練 SwinIR 的patch大小。

model_path:預訓練模型的路徑。

folder_lq:低質量圖片的文件夾路徑。

folder_gt:高質量圖片的文件夾路徑。

image-20220430171938855

更多的命令如下:

```python

001 Classical Image Super-Resolution (middle size)

Note that --training_patch_size is just used to differentiate two different settings in Table 2 of the paper. Images are NOT tested patch by patch.

(setting1: when model is trained on DIV2K and with training_patch_size=48)

python main_test_swinir.py --task classical_sr --scale 2 --training_patch_size 48 --model_path model_zoo/swinir/001_classicalSR_DIV2K_s48w8_SwinIR-M_x2.pth --folder_lq testsets/Set5/LR_bicubic/X2 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 3 --training_patch_size 48 --model_path model_zoo/swinir/001_classicalSR_DIV2K_s48w8_SwinIR-M_x3.pth --folder_lq testsets/Set5/LR_bicubic/X3 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 4 --training_patch_size 48 --model_path model_zoo/swinir/001_classicalSR_DIV2K_s48w8_SwinIR-M_x4.pth --folder_lq testsets/Set5/LR_bicubic/X4 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 8 --training_patch_size 48 --model_path model_zoo/swinir/001_classicalSR_DIV2K_s48w8_SwinIR-M_x8.pth --folder_lq testsets/Set5/LR_bicubic/X8 --folder_gt testsets/Set5/HR

(setting2: when model is trained on DIV2K+Flickr2K and with training_patch_size=64)

python main_test_swinir.py --task classical_sr --scale 2 --training_patch_size 64 --model_path model_zoo/swinir/001_classicalSR_DF2K_s64w8_SwinIR-M_x2.pth --folder_lq testsets/Set5/LR_bicubic/X2 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 3 --training_patch_size 64 --model_path model_zoo/swinir/001_classicalSR_DF2K_s64w8_SwinIR-M_x3.pth --folder_lq testsets/Set5/LR_bicubic/X3 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 4 --training_patch_size 64 --model_path model_zoo/swinir/001_classicalSR_DF2K_s64w8_SwinIR-M_x4.pth --folder_lq testsets/Set5/LR_bicubic/X4 --folder_gt testsets/Set5/HR python main_test_swinir.py --task classical_sr --scale 8 --training_patch_size 64 --model_path model_zoo/swinir/001_classicalSR_DF2K_s64w8_SwinIR-M_x8.pth --folder_lq testsets/Set5/LR_bicubic/X8 --folder_gt testsets/Set5/HR

002 Lightweight Image Super-Resolution (small size)

python main_test_swinir.py --task lightweight_sr --scale 2 --model_path model_zoo/swinir/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x2.pth --folder_lq testsets/Set5/LR_bicubic/X2 --folder_gt testsets/Set5/HR python main_test_swinir.py --task lightweight_sr --scale 3 --model_path model_zoo/swinir/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x3.pth --folder_lq testsets/Set5/LR_bicubic/X3 --folder_gt testsets/Set5/HR python main_test_swinir.py --task lightweight_sr --scale 4 --model_path model_zoo/swinir/002_lightweightSR_DIV2K_s64w8_SwinIR-S_x4.pth --folder_lq testsets/Set5/LR_bicubic/X4 --folder_gt testsets/Set5/HR

003 Real-World Image Super-Resolution (use --tile 400 if you run out-of-memory)

(middle size)

python main_test_swinir.py --task real_sr --scale 4 --model_path model_zoo/swinir/003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.pth --folder_lq testsets/RealSRSet+5images --tile

(larger size + trained on more datasets)

python main_test_swinir.py --task real_sr --scale 4 --large_model --model_path model_zoo/swinir/003_realSR_BSRGAN_DFOWMFC_s64w8_SwinIR-L_x4_GAN.pth --folder_lq testsets/RealSRSet+5images

004 Grayscale Image Deoising (middle size)

python main_test_swinir.py --task gray_dn --noise 15 --model_path model_zoo/swinir/004_grayDN_DFWB_s128w8_SwinIR-M_noise15.pth --folder_gt testsets/Set12 python main_test_swinir.py --task gray_dn --noise 25 --model_path model_zoo/swinir/004_grayDN_DFWB_s128w8_SwinIR-M_noise25.pth --folder_gt testsets/Set12 python main_test_swinir.py --task gray_dn --noise 50 --model_path model_zoo/swinir/004_grayDN_DFWB_s128w8_SwinIR-M_noise50.pth --folder_gt testsets/Set12

005 Color Image Deoising (middle size)

python main_test_swinir.py --task color_dn --noise 15 --model_path model_zoo/swinir/005_colorDN_DFWB_s128w8_SwinIR-M_noise15.pth --folder_gt testsets/McMaster python main_test_swinir.py --task color_dn --noise 25 --model_path model_zoo/swinir/005_colorDN_DFWB_s128w8_SwinIR-M_noise25.pth --folder_gt testsets/McMaster python main_test_swinir.py --task color_dn --noise 50 --model_path model_zoo/swinir/005_colorDN_DFWB_s128w8_SwinIR-M_noise50.pth --folder_gt testsets/McMaster

006 JPEG Compression Artifact Reduction (middle size, using window_size=7 because JPEG encoding uses 8x8 blocks)

python main_test_swinir.py --task jpeg_car --jpeg 10 --model_path model_zoo/swinir/006_CAR_DFWB_s126w7_SwinIR-M_jpeg10.pth --folder_gt testsets/classic5 python main_test_swinir.py --task jpeg_car --jpeg 20 --model_path model_zoo/swinir/006_CAR_DFWB_s126w7_SwinIR-M_jpeg20.pth --folder_gt testsets/classic5 python main_test_swinir.py --task jpeg_car --jpeg 30 --model_path model_zoo/swinir/006_CAR_DFWB_s126w7_SwinIR-M_jpeg30.pth --folder_gt testsets/classic5 python main_test_swinir.py --task jpeg_car --jpeg 40 --model_path model_zoo/swinir/006_CAR_DFWB_s126w7_SwinIR-M_jpeg40.pth --folder_gt testsets/classic5

Result ```

那麼如何用起來呢?

我們使用classical_sr任務講解。

首先將評分的部分註釋掉。註釋main_test_swinir.py中的83行到119行。

image-20220430172831688

將185行的folder_gt改為folder_lq

image-20220430172950705

修改218行到223行的代碼。由

```python

001 classical image sr/ 002 lightweight image sr (load lq-gt image pairs)

if args.task in ['classical_sr', 'lightweight_sr']: img_gt = cv2.imread(path, cv2.IMREAD_COLOR).astype(np.float32) / 255. img_lq = cv2.imread(f'{args.folder_lq}/{imgname}x{args.scale}{imgext}', cv2.IMREAD_COLOR).astype( np.float32) / 255. ```

修改為:

python # 001 classical image sr/ 002 lightweight image sr (load lq-gt image pairs) if args.task in ['classical_sr', 'lightweight_sr']: # img_gt = cv2.imread(path, cv2.IMREAD_COLOR).astype(np.float32) / 255. img_gt = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1) img_gt = cv2.cvtColor(img_gt, cv2.IMREAD_COLOR).astype(np.float32) / 255. lq_path=f'{args.folder_lq}/{imgname}{imgext}' img_lq = cv2.imdecode(np.fromfile(lq_path, dtype=np.uint8), -1) img_lq = cv2.cvtColor(img_lq, cv2.IMREAD_COLOR).astype(np.float32) / 255.

使用cv2.imdecode方法讀取圖片,可以解決路徑有中文的問題。

將81行的代碼

python cv2.imwrite(f'{save_dir}/{imgname}_SwinIR.png', output)

修改為:

python write_path=f'{save_dir}/{imgname}.jpg' cv2.imencode('.jpg', output)[1].tofile(write_path) # 保存圖片

這樣,生成的圖片和原來低質量的圖片名字保持一致了。

完成上面的修改,執行命令:

shell python main_test_swinir.py --task classical_sr --scale 2 --training_patch_size 64 --model_path model_zoo/swinir/001_classicalsr_df2k_s64w8_swinir-m_x2.pth --folder_lq testsets/Set5/LR_bicubic/X2

image-20220430185949659

等待命令運行完成後,我們就在result文件夾下面看到超分後的圖像了。

對比結果:

image-20220430190742838 我在對這些圖片超分後,然後執行分類任務測試。在實際的測試中,和超分的圖片作對比有了0.1%的提升,提升成績有限。

我推測:超分後,只是讓我們人眼覺得圖像的畫質提高了,但是對於神經網絡來説,有用的特徵並沒有得到提升。圖像在輸入神經網絡後,一般要經過Resize,超分前的圖像尺寸接近224×224,超分後,提升了像素,但是輸入進網絡的時候又Resize成224×224,這樣就導致了有效的特徵被壓縮了,所以提升有限。

完整代碼:

http://download.csdn.net/download/hhhhhhhhhhwwwwwwwwww/85251176