学习资源站

YOLOv11改进-特殊场景改进篇-单阶段盲真实图像去噪网络RIDNet辅助YOLOv11图像去噪(全网独家首发)

一、本文介绍

本文给大家带来的改进机制是 单阶段 盲真实 图像去噪 网络RIDNet,RIDNet(Real Image Denoising with Feature Attention)是一个用于真实图像去噪的卷积神经网络( CNN ),旨在解决现有去噪方法在处理真实噪声图像时 性能 受限的问题。通过单阶段结构和特征注意机制,RIDNet在多种数据集上展示了其优越性,下面的图片为其效果图片包括和其它图像图像网络的对比图。

欢迎大家订阅我的专栏一起学习YOLO!



二、RIDNet 网络的原理和机制

官方论文地址: 官方论文地址点击此处即可跳转

官方代码地址: 官方代码地址点击此处即可跳转


RIDNet(Real Image Denoising with Feature Attention )是一个用于真实图像去噪的 卷积神经网络 (CNN),旨在解决现有去噪方法在处理真实噪声图像时性能受限的问题。通过单阶段结构和特征注意机制,RIDNet在多种数据集上展示了其优越性。 RIDNet由三个主要模块组成:

1. 特征提取模块(Feature Extraction Module): 该模块包含一个卷积层,旨在从输入的噪声图像中提取初始特征。

2. 特征学习模块(Feature Learning Module): 核心部分是增强注意模块(Enhanced Attention Module,EAM),使用残差在残差结构(Residual on Residual)和特征注意机制来增强特征学习能力。 EAM包括两个主要部分:

(1)特征提取子模块:通过两个膨胀卷积层和一个合并卷积层提取和学习特征。

(2)特征注意子模块:使用全局平均池化和自门控机制生成特征注意力,调整每个通道的特征权重,以突出重要特征。

3. 重建模块(Reconstruction Module): 包含一个卷积层,将学习到的特征重建为去噪后的图像。

结论: RIDNet在多个合成和真实噪声数据集上进行了广泛的实验,展示了其在定量指标(如PSNR)和视觉质量上的优越性。与现有最先进的算法相比,RIDNet在处理合成噪声和真实噪声图像时均表现出色。RIDNet通过引入特征注意机制和残差在残差结构,实现了对真实图像去噪的有效处理。其单阶段结构、跳跃连接和特征注意机制确保了高效的特征学习和信息传递,使其在多个数据集上均取得了优异的性能。


三、核心代码

这个代码基础版本原先有1000+GFLOPs,我将其Block层数优化了一些,并将通道数减少了一部分将参数量降低到了20+。

  1. import math
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. def default_conv(in_channels, out_channels, kernel_size, bias=True):
  6. return nn.Conv2d(
  7. in_channels, out_channels, kernel_size,
  8. padding=(kernel_size//2), bias=bias)
  9. class MeanShift(nn.Conv2d):
  10. def __init__(self, rgb_range, rgb_mean, rgb_std, sign=-1):
  11. super(MeanShift, self).__init__(3, 3, kernel_size=1)
  12. std = torch.Tensor(rgb_std)
  13. self.weight.data = torch.eye(3).view(3, 3, 1, 1)
  14. self.weight.data.div_(std.view(3, 1, 1, 1))
  15. self.bias.data = sign * rgb_range * torch.Tensor(rgb_mean)
  16. self.bias.data.div_(std)
  17. self.requires_grad = False
  18. def init_weights(modules):
  19. pass
  20. class Merge_Run(nn.Module):
  21. def __init__(self,
  22. in_channels, out_channels,
  23. ksize=3, stride=1, pad=1, dilation=1):
  24. super(Merge_Run, self).__init__()
  25. self.body1 = nn.Sequential(
  26. nn.Conv2d(in_channels, out_channels, ksize, stride, pad),
  27. nn.ReLU(inplace=True)
  28. )
  29. self.body2 = nn.Sequential(
  30. nn.Conv2d(in_channels, out_channels, ksize, stride, 2, 2),
  31. nn.ReLU(inplace=True)
  32. )
  33. self.body3 = nn.Sequential(
  34. nn.Conv2d(in_channels * 2, out_channels, ksize, stride, pad),
  35. nn.ReLU(inplace=True)
  36. )
  37. init_weights(self.modules)
  38. def forward(self, x):
  39. out1 = self.body1(x)
  40. out2 = self.body2(x)
  41. c = torch.cat([out1, out2], dim=1)
  42. c_out = self.body3(c)
  43. out = c_out + x
  44. return out
  45. class Merge_Run_dual(nn.Module):
  46. def __init__(self,
  47. in_channels, out_channels,
  48. ksize=3, stride=1, pad=1, dilation=1):
  49. super(Merge_Run_dual, self).__init__()
  50. self.body1 = nn.Sequential(
  51. nn.Conv2d(in_channels, out_channels, ksize, stride, pad),
  52. nn.ReLU(inplace=True),
  53. nn.Conv2d(in_channels, out_channels, ksize, stride, 2, 2),
  54. nn.ReLU(inplace=True)
  55. )
  56. self.body2 = nn.Sequential(
  57. nn.Conv2d(in_channels, out_channels, ksize, stride, 3, 3),
  58. nn.ReLU(inplace=True),
  59. nn.Conv2d(in_channels, out_channels, ksize, stride, 4, 4),
  60. nn.ReLU(inplace=True)
  61. )
  62. self.body3 = nn.Sequential(
  63. nn.Conv2d(in_channels * 2, out_channels, ksize, stride, pad),
  64. nn.ReLU(inplace=True)
  65. )
  66. init_weights(self.modules)
  67. def forward(self, x):
  68. out1 = self.body1(x)
  69. out2 = self.body2(x)
  70. c = torch.cat([out1, out2], dim=1)
  71. c_out = self.body3(c)
  72. out = c_out + x
  73. return out
  74. class BasicBlock(nn.Module):
  75. def __init__(self,
  76. in_channels, out_channels,
  77. ksize=3, stride=1, pad=1):
  78. super(BasicBlock, self).__init__()
  79. self.body = nn.Sequential(
  80. nn.Conv2d(in_channels, out_channels, ksize, stride, pad),
  81. nn.ReLU(inplace=True)
  82. )
  83. init_weights(self.modules)
  84. def forward(self, x):
  85. out = self.body(x)
  86. return out
  87. class BasicBlockSig(nn.Module):
  88. def __init__(self,
  89. in_channels, out_channels,
  90. ksize=3, stride=1, pad=1):
  91. super(BasicBlockSig, self).__init__()
  92. self.body = nn.Sequential(
  93. nn.Conv2d(in_channels, out_channels, ksize, stride, pad),
  94. nn.Sigmoid()
  95. )
  96. init_weights(self.modules)
  97. def forward(self, x):
  98. out = self.body(x)
  99. return out
  100. class ResidualBlock(nn.Module):
  101. def __init__(self,
  102. in_channels, out_channels):
  103. super(ResidualBlock, self).__init__()
  104. self.body = nn.Sequential(
  105. nn.Conv2d(in_channels, out_channels, 3, 1, 1),
  106. nn.ReLU(inplace=True),
  107. nn.Conv2d(out_channels, out_channels, 3, 1, 1),
  108. )
  109. init_weights(self.modules)
  110. def forward(self, x):
  111. out = self.body(x)
  112. out = F.relu(out + x)
  113. return out
  114. class EResidualBlock(nn.Module):
  115. def __init__(self,
  116. in_channels, out_channels,
  117. group=1):
  118. super(EResidualBlock, self).__init__()
  119. self.body = nn.Sequential(
  120. nn.Conv2d(in_channels, out_channels, 3, 1, 1, groups=group),
  121. nn.ReLU(inplace=True),
  122. nn.Conv2d(out_channels, out_channels, 3, 1, 1, groups=group),
  123. nn.ReLU(inplace=True),
  124. nn.Conv2d(out_channels, out_channels, 1, 1, 0),
  125. )
  126. init_weights(self.modules)
  127. def forward(self, x):
  128. out = self.body(x)
  129. out = F.relu(out + x)
  130. return out
  131. class CALayer(nn.Module):
  132. def __init__(self, channel, reduction=16):
  133. super(CALayer, self).__init__()
  134. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  135. self.c1 = BasicBlock(channel, channel // reduction, 1, 1, 0)
  136. self.c2 = BasicBlockSig(channel // reduction, channel, 1, 1, 0)
  137. def forward(self, x):
  138. y = self.avg_pool(x)
  139. y1 = self.c1(y)
  140. y2 = self.c2(y1)
  141. return x * y2
  142. class Block(nn.Module):
  143. def __init__(self, in_channels, out_channels, group=1):
  144. super(Block, self).__init__()
  145. self.r1 = Merge_Run_dual(in_channels, out_channels)
  146. self.r2 = ResidualBlock(in_channels, out_channels)
  147. self.r3 = EResidualBlock(in_channels, out_channels)
  148. # self.g = ops.BasicBlock(in_channels, out_channels, 1, 1, 0)
  149. self.ca = CALayer(in_channels)
  150. def forward(self, x):
  151. r1 = self.r1(x)
  152. r2 = self.r2(r1)
  153. r3 = self.r3(r2)
  154. # g = self.g(r3)
  155. out = self.ca(r3)
  156. return out
  157. class RIDNET(nn.Module):
  158. def __init__(self, args):
  159. super(RIDNET, self).__init__()
  160. n_feats = 16
  161. kernel_size = 3
  162. rgb_range = 255
  163. mean = (0.4488, 0.4371, 0.4040)
  164. std = (1.0, 1.0, 1.0)
  165. self.sub_mean = MeanShift(rgb_range, mean, std)
  166. self.add_mean = MeanShift(rgb_range, mean, std, 1)
  167. self.head = BasicBlock(3, n_feats, kernel_size, 1, 1)
  168. self.b1 = Block(n_feats, n_feats)
  169. self.b2 = Block(n_feats, n_feats)
  170. self.b3 = Block(n_feats, n_feats)
  171. self.b4 = Block(n_feats, n_feats)
  172. self.tail = nn.Conv2d(n_feats, 3, kernel_size, 1, 1, 1)
  173. def forward(self, x):
  174. s = self.sub_mean(x)
  175. h = self.head(s)
  176. # b1 = self.b1(h)
  177. # b2 = self.b2(b1)
  178. # b3 = self.b3(b2)
  179. b_out = self.b4(h)
  180. res = self.tail(b_out)
  181. out = self.add_mean(res)
  182. f_out = out + x
  183. return f_out
  184. if __name__ == "__main__":
  185. # Generating Sample image
  186. image_size = (1, 3, 640, 640)
  187. image = torch.rand(*image_size)
  188. # Model
  189. model = RIDNET(3)
  190. out = model(image)
  191. print(out.size())


四、手把手教你添加RIDNet

4.1 修改一

第一还是建立文件,我们找到如下 ultralytics /nn文件夹下建立一个目录名字呢就是'Addmodules'文件夹( 用群内的文件的话已经有了无需新建) !然后在其内部建立一个新的py文件将核心代码复制粘贴进去即可。


4.2 修改二

第二步我们在该目录下创建一个新的py文件名字为'__init__.py'( 用群内的文件的话已经有了无需新建) ,然后在其内部导入我们的检测头如下图所示。


4.3 修改三

第三步我门中到如下文件'ultralytics/nn/tasks.py'进行导入和注册我们的模块( 用群内的文件的话已经有了无需重新导入直接开始第四步即可)

从今天开始以后的教程就都统一成这个样子了,因为我默认大家用了我群内的文件来进行修改!!

​​


4.4 修改四

按照我的添加在parse_model里添加即可。


到此就修改完成了,大家可以复制下面的yaml文件运行。


五、RIDNet 的yaml文件和运行记录

5.1 RIDNet 的yaml文件

此版本训练信息:YOLO11-RIDNet summary: 475 layers, 2,685,742 parameters, 2,685,726 gradients, 26.2 GFLOPs

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  8. s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  9. m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  10. l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  11. x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
  12. # YOLO11n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, RIDNET, []] # 0-P1/2
  16. - [-1, 1, Conv, [64, 3, 2]] # 1-P1/2
  17. - [-1, 1, Conv, [128, 3, 2]] # 2-P2/4
  18. - [-1, 2, C3k2, [256, False, 0.25]]
  19. - [-1, 1, Conv, [256, 3, 2]] # 4-P3/8
  20. - [-1, 2, C3k2, [512, False, 0.25]]
  21. - [-1, 1, Conv, [512, 3, 2]] # 6-P4/16
  22. - [-1, 2, C3k2, [512, True]]
  23. - [-1, 1, Conv, [1024, 3, 2]] # 8-P5/32
  24. - [-1, 2, C3k2, [1024, True]]
  25. - [-1, 1, SPPF, [1024, 5]] # 10
  26. - [-1, 2, C2PSA, [1024]] # 11
  27. # YOLO11n head
  28. head:
  29. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  30. - [[-1, 7], 1, Concat, [1]] # cat backbone P4
  31. - [-1, 2, C3k2, [512, False]] # 14
  32. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  33. - [[-1, 5], 1, Concat, [1]] # cat backbone P3
  34. - [-1, 2, C3k2, [256, False]] # 17 (P3/8-small)
  35. - [-1, 1, Conv, [256, 3, 2]]
  36. - [[-1, 14], 1, Concat, [1]] # cat head P4
  37. - [-1, 2, C3k2, [512, False]] # 20 (P4/16-medium)
  38. - [-1, 1, Conv, [512, 3, 2]]
  39. - [[-1, 11], 1, Concat, [1]] # cat head P5
  40. - [-1, 2, C3k2, [1024, True]] # 23 (P5/32-large)
  41. - [[17, 20, 23], 1, Detect, [nc]] # Detect(P3, P4, P5)


5.2 训练代码

大家可以创建一个py文件将我给的代码复制粘贴进去,配置好自己的文件路径即可运行。

  1. import warnings
  2. warnings.filterwarnings('ignore')
  3. from ultralytics import YOLO
  4. if __name__ == '__main__':
  5. model = YOLO('ultralytics/cfg/models/v8/yolov8-C2f-FasterBlock.yaml')
  6. # model.load('yolov8n.pt') # loading pretrain weights
  7. model.train(data=r'替换数据集yaml文件地址',
  8. # 如果大家任务是其它的'ultralytics/cfg/default.yaml'找到这里修改task可以改成detect, segment, classify, pose
  9. cache=False,
  10. imgsz=640,
  11. epochs=150,
  12. single_cls=False, # 是否是单类别检测
  13. batch=4,
  14. close_mosaic=10,
  15. workers=0,
  16. device='0',
  17. optimizer='SGD', # using SGD
  18. # resume='', # 如过想续训就设置last.pt的地址
  19. amp=False, # 如果出现训练损失为Nan可以关闭amp
  20. project='runs/train',
  21. name='exp',
  22. )


5.3 RIDNet 的训练过程截图


五、本文总结

到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~