当前位置:网站首页>SwinIR实战:如何使用SwinIR和预训练模型实现图片的超分
SwinIR实战:如何使用SwinIR和预训练模型实现图片的超分
2022-08-03 16:17:00 【华为云】
@[toc]
摘要
论文地址:https://arxiv.org/pdf/2108.10257.pdf
预训练模型下载:https://github.com/JingyunLiang/SwinIR/releases
训练代码下载:https://github.com/cszn/KAIR
测试:https://github.com/JingyunLiang/SwinIR
论文翻译:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/124434886
本文介绍如何使用SwinIR和预训练模型实现图像的超分。
测试
下载代码:https://github.com/JingyunLiang/SwinIR,然后解压,如下图:

然后下载预训练权重文件,打开连接:https://github.com/JingyunLiang/SwinIR/releases,如下图:

预训练权重有很多,选择自己用到的预训练权重下载,下载后将其放入./model_zoo/swinir文件夹下面,如下图:

然后执行命令:
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/HRtask:任务类型,包括: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:高质量图片的文件夹路径。

更多的命令如下:
# 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/HRpython 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/HRpython 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/HRpython 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/HRpython 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/HRpython 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/HRpython 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/HRpython 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/HRpython 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/Set12python 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/Set12python 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/McMasterpython 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/McMasterpython 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/classic5python 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/classic5python 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/classic5python 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/classic5Result那么如何用起来呢?
我们使用classical_sr任务讲解。
首先将评分的部分注释掉。注释main_test_swinir.py中的83行到119行。

将185行的folder_gt改为folder_lq

修改218行到223行的代码。由
# 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.修改为:
# 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行的代码
cv2.imwrite(f'{save_dir}/{imgname}_SwinIR.png', output)修改为:
write_path=f'{save_dir}/{imgname}.jpg' cv2.imencode('.jpg', output)[1].tofile(write_path) # 保存图片这样,生成的图片和原来低质量的图片名字保持一致了。
完成上面的修改,执行命令:
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
等待命令运行完成后,我们就在result文件夹下面看到超分后的图像了。
对比结果:

我在对这些图片超分后,然后执行分类任务测试。在实际的测试中,和超分的图片作对比有了0.1%的提升,提升成绩有限。
我推测:超分后,只是让我们人眼觉得图像的画质提高了,但是对于神经网络来说,有用的特征并没有得到提升。图像在输入神经网络后,一般要经过Resize,超分前的图像尺寸接近224×224,超分后,提升了像素,但是输入进网络的时候又Resize成224×224,这样就导致了有效的特征被压缩了,所以提升有限。
完整代码:
https://download.csdn.net/download/hhhhhhhhhhwwwwwwwwww/85251176
边栏推荐
- C专家编程 第2章 这不是Bug,而是语言特性 2.2 多做之过
- C专家编程 第1章 C:穿越时空的迷雾 1.8 ANSI C标准的结构
- Kubernetes 笔记 / 生产环境
- window.open不显示favicon.icon
- 2年开发经验去面试,吊打面试官,即将面试的程序员这些笔记建议复习
- [Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01
- 使用.NET简单实现一个Redis的高性能克隆版(一)
- MySQL查询语法
- 新版本 MaxCompute 的SQL 中支持的 EXTRACT 函数有什么作用?
- 使用 PowerShell 将 Windows 转发事件导入 SQL Server
猜你喜欢
![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01](/img/8e/fcf79d150af4384c14a118fb209725.png)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01

【Unity入门计划】制作RubyAdventure01-玩家的创建&移动

leetcode:187. 重复的DNA序列

一文看懂推荐系统:召回01:基于物品的协同过滤(ItemCF),item-based Collaboration Filter的核心思想与推荐过程

spark入门学习-2

How much do you know about the intelligent operation and maintenance service of data warehouse based on DMS?

【QT】Qt 给已经开发好的程序快速封装成动态库

leetcode:202. 快乐数

vector类

虹科分享 | 如何测试与验证复杂的FPGA设计(3)——硬件测试
随机推荐
When mobile applications go overseas, is your "network optimization" holding back?
C专家编程 第2章 这不是Bug,而是语言特性 2.1 这关语言特性何事,在Fortran里这就是Bug呀
spark入门学习-2
DataGrip:非常好用的数据库工具,安装与使用教程,亮点介绍
DataGrip数据仓库工具
uniapp隐藏导航栏和横屏显示设置
Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology
基于DMS的数仓智能运维服务,知多少?
TCP 可靠吗?为什么?
Web3 安全风险令人生畏?应该如何应对?
C语言03、数组
protobuf 中数据编码规则
C专家编程 第3章 分析C语言的声明 3.6 typedef int x[10]和#define x int[10]的区别
一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样
#夏日挑战赛# HarmonyOS 实现一个绘画板
ffplay视频播放原理分析
C专家编程 第2章 这不是Bug,而是语言特性 2.4 少做之过
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 01
Detailed ReentrantLock
MySQL窗口函数 OVER()函数介绍