当前位置:   article > 正文

复现论文之对YOLOV7添加可变卷积网络与注意力机制_yolov7添加动态卷积

yolov7添加动态卷积

首先,让我们先看一看这篇论文主要的修改地方

 这篇论文中,在yolov7的backbone最后一个部分引入了可变卷积结构,我们先来了解一下yolov7的大概结构是什么样子的。

 仔细观察两副图,我们可以看到论文其实是对yolov7最后stage5部分的Transition_Block和Multi_Concat_Block进行了变换,在这里引入了可变卷积结构。

其次,论文在neck中引入了SimAm注意力机制,注意力机制的添加可以参考我的上一篇文章:

对YOLOv7添加CA注意力机制并对数据集进行检测_小赵每天都来学习的博客-CSDN博客

接下来我们继续替换backbone结构中的 Transition_Block和Multi_Concat_Block。

首先我们需要知道在yolov7中Transition_Block和Multi_Concat_Block是具体是什么样子的:

 

 可以看到Transition_Block有着非常多的卷积过程,类似于残差结构,而在Multi_Concat_Block中左边是maxpool操作过程,右边是两次卷积,都会使特征图维度变小,最后进行Concat。

随后我们来看看论文中修改的地方:

 

 论文中在Transition_Block中对右边连续的卷积进行了替换,替换为了可变性卷积结构;

在Multi_Concat_Block中将两个卷积中后面的一个卷积替换为了可变性卷积结构,接下来我们在代码中看看怎么体现:

首先我们先导入我们的可变卷积结构代码:

  1. import torch
  2. import torch.nn as nn
  3. class DeformConv2d(nn.Module):
  4. def __init__(self, inc, outc, kernel_size=3, stride=1, padding=1, bias=None, modulation=False):
  5. """
  6. Args:
  7. modulation (bool, optional): If True, Modulated Defomable Convolution (Deformable ConvNets v2).
  8. """
  9. super(DeformConv2d, self).__init__()
  10. self.kernel_size = kernel_size
  11. self.padding = padding
  12. self.stride = stride
  13. self.zero_padding = nn.ZeroPad2d(padding)
  14. # conv则是实际进行的卷积操作,注意这里步长设置为卷积核大小,因为与该卷积核进行卷积操作的特征图是由输出特征图中每个点扩展为其对应卷积核那么多个点后生成的。
  15. self.conv = nn.Conv2d(inc, outc, kernel_size=kernel_size, stride=kernel_size, bias=bias)
  16. # p_conv是生成offsets所使用的卷积,输出通道数为卷积核尺寸的平方的2倍,代表对应卷积核每个位置横纵坐标都有偏移量。
  17. self.p_conv = nn.Conv2d(inc, 2 * kernel_size * kernel_size, kernel_size=3, padding=1, stride=stride)
  18. nn.init.constant_(self.p_conv.weight, 0)
  19. self.p_conv.register_backward_hook(self._set_lr)
  20. self.modulation = modulation # modulation是可选参数,若设置为True,那么在进行卷积操作时,对应卷积核的每个位置都会分配一个权重。
  21. if modulation:
  22. self.m_conv = nn.Conv2d(inc, kernel_size * kernel_size, kernel_size=3, padding=1, stride=stride)
  23. nn.init.constant_(self.m_conv.weight, 0)
  24. self.m_conv.register_backward_hook(self._set_lr)
  25. @staticmethod
  26. def _set_lr(module, grad_input, grad_output):
  27. grad_input = (grad_input[i] * 0.1 for i in range(len(grad_input)))
  28. grad_output = (grad_output[i] * 0.1 for i in range(len(grad_output)))
  29. def forward(self, x):
  30. offset = self.p_conv(x)
  31. if self.modulation:
  32. m = torch.sigmoid(self.m_conv(x))
  33. dtype = offset.data.type()
  34. ks = self.kernel_size
  35. N = offset.size(1) // 2
  36. if self.padding:
  37. x = self.zero_padding(x)
  38. # (b, 2N, h, w)
  39. p = self._get_p(offset, dtype)
  40. # (b, h, w, 2N)
  41. p = p.contiguous().permute(0, 2, 3, 1)
  42. q_lt = p.detach().floor()
  43. q_rb = q_lt + 1
  44. q_lt = torch.cat([torch.clamp(q_lt[..., :N], 0, x.size(2) - 1), torch.clamp(q_lt[..., N:], 0, x.size(3) - 1)],
  45. dim=-1).long()
  46. q_rb = torch.cat([torch.clamp(q_rb[..., :N], 0, x.size(2) - 1), torch.clamp(q_rb[..., N:], 0, x.size(3) - 1)],
  47. dim=-1).long()
  48. q_lb = torch.cat([q_lt[..., :N], q_rb[..., N:]], dim=-1)
  49. q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], dim=-1)
  50. # clip p
  51. p = torch.cat([torch.clamp(p[..., :N], 0, x.size(2) - 1), torch.clamp(p[..., N:], 0, x.size(3) - 1)], dim=-1)
  52. # bilinear kernel (b, h, w, N)
  53. g_lt = (1 + (q_lt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_lt[..., N:].type_as(p) - p[..., N:]))
  54. g_rb = (1 - (q_rb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_rb[..., N:].type_as(p) - p[..., N:]))
  55. g_lb = (1 + (q_lb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_lb[..., N:].type_as(p) - p[..., N:]))
  56. g_rt = (1 - (q_rt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_rt[..., N:].type_as(p) - p[..., N:]))
  57. # (b, c, h, w, N)
  58. x_q_lt = self._get_x_q(x, q_lt, N)
  59. x_q_rb = self._get_x_q(x, q_rb, N)
  60. x_q_lb = self._get_x_q(x, q_lb, N)
  61. x_q_rt = self._get_x_q(x, q_rt, N)
  62. # (b, c, h, w, N)
  63. x_offset = g_lt.unsqueeze(dim=1) * x_q_lt + \
  64. g_rb.unsqueeze(dim=1) * x_q_rb + \
  65. g_lb.unsqueeze(dim=1) * x_q_lb + \
  66. g_rt.unsqueeze(dim=1) * x_q_rt
  67. # modulation
  68. if self.modulation:
  69. m = m.contiguous().permute(0, 2, 3, 1)
  70. m = m.unsqueeze(dim=1)
  71. m = torch.cat([m for _ in range(x_offset.size(1))], dim=1)
  72. x_offset *= m
  73. x_offset = self._reshape_x_offset(x_offset, ks)
  74. out = self.conv(x_offset)
  75. return out
  76. def _get_p_n(self, N, dtype):
  77. # 由于卷积核中心点位置是其尺寸的一半,于是中心点向左(上)方向移动尺寸的一半就得到起始点,向右(下)方向移动另一半就得到终止点
  78. p_n_x, p_n_y = torch.meshgrid(
  79. torch.arange(-(self.kernel_size - 1) // 2, (self.kernel_size - 1) // 2 + 1),
  80. torch.arange(-(self.kernel_size - 1) // 2, (self.kernel_size - 1) // 2 + 1))
  81. # (2N, 1)
  82. p_n = torch.cat([torch.flatten(p_n_x), torch.flatten(p_n_y)], 0)
  83. p_n = p_n.view(1, 2 * N, 1, 1).type(dtype)
  84. return p_n
  85. def _get_p_0(self, h, w, N, dtype):
  86. # p0_y、p0_x就是输出特征图每点映射到输入特征图上的纵、横坐标值。
  87. p_0_x, p_0_y = torch.meshgrid(
  88. torch.arange(1, h * self.stride + 1, self.stride),
  89. torch.arange(1, w * self.stride + 1, self.stride))
  90. p_0_x = torch.flatten(p_0_x).view(1, 1, h, w).repeat(1, N, 1, 1)
  91. p_0_y = torch.flatten(p_0_y).view(1, 1, h, w).repeat(1, N, 1, 1)
  92. p_0 = torch.cat([p_0_x, p_0_y], 1).type(dtype)
  93. return p_0
  94. # 输出特征图上每点(对应卷积核中心)加上其对应卷积核每个位置的相对(横、纵)坐标后再加上自学习的(横、纵坐标)偏移量。
  95. # p0就是将输出特征图每点对应到卷积核中心,然后映射到输入特征图中的位置;
  96. # pn则是p0对应卷积核每个位置的相对坐标;
  97. def _get_p(self, offset, dtype):
  98. N, h, w = offset.size(1) // 2, offset.size(2), offset.size(3)
  99. # (1, 2N, 1, 1)
  100. p_n = self._get_p_n(N, dtype)
  101. # (1, 2N, h, w)
  102. p_0 = self._get_p_0(h, w, N, dtype)
  103. p = p_0 + p_n + offset
  104. return p
  105. def _get_x_q(self, x, q, N):
  106. # 计算双线性插值点的4邻域点对应的权重
  107. b, h, w, _ = q.size()
  108. padded_w = x.size(3)
  109. c = x.size(1)
  110. # (b, c, h*w)
  111. x = x.contiguous().view(b, c, -1)
  112. # (b, h, w, N)
  113. index = q[..., :N] * padded_w + q[..., N:] # offset_x*w + offset_y
  114. # (b, c, h*w*N)
  115. index = index.contiguous().unsqueeze(dim=1).expand(-1, c, -1, -1, -1).contiguous().view(b, c, -1)
  116. x_offset = x.gather(dim=-1, index=index).contiguous().view(b, c, h, w, N)
  117. return x_offset
  118. @staticmethod
  119. def _reshape_x_offset(x_offset, ks):
  120. b, c, h, w, N = x_offset.size()
  121. x_offset = torch.cat([x_offset[..., s:s + ks].contiguous().view(b, c, h, w * ks) for s in range(0, N, ks)],
  122. dim=-1)
  123. x_offset = x_offset.contiguous().view(b, c, h * ks, w * ks)
  124. return x_offset

之后我们打开yolov7的nets文件夹,在文件夹里有一个backbone.py文件,我们将其打开,翻到下面可以看到定义好了的Transition_Block和Multi_Concat_Block函数:

  1. class Multi_Concat_Block(nn.Module):
  2. def __init__(self, c1, c2, c3, n=4, e=1, ids=[0]):
  3. super(Multi_Concat_Block, self).__init__()
  4. c_ = int(c2 * e)
  5. self.ids = ids
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c1, c_, 1, 1)
  8. self.cv3 = nn.ModuleList(
  9. [Conv(c_ if i ==0 else c2, c2, 3, 1) for i in range(n)]
  10. )
  11. self.cv4 = Conv(c_ * 2 + c2 * (len(ids) - 2), c3, 1, 1)
  12. def forward(self, x):
  13. x_1 = self.cv1(x)
  14. x_2 = self.cv2(x)
  15. x_all = [x_1, x_2]
  16. # [-1, -3, -5, -6] => [5, 3, 1, 0]
  17. for i in range(len(self.cv3)):
  18. x_2 = self.cv3[i](x_2)
  19. x_all.append(x_2)
  20. out = self.cv4(torch.cat([x_all[id] for id in self.ids], 1))
  21. return out
  22. class Transition_Block(nn.Module):
  23. def __init__(self, c1, c2):
  24. super(Transition_Block, self).__init__()
  25. self.cv1 = Conv(c1, c2, 1, 1)
  26. self.cv2 = Conv(c1, c2, 1, 1)
  27. self.cv3 = Conv(c2, c2, 3, 2)
  28. self.mp = MP()
  29. def forward(self, x):
  30. # 160, 160, 256 => 80, 80, 256 => 80, 80, 128
  31. x_1 = self.mp(x)
  32. x_1 = self.cv1(x_1)
  33. # 160, 160, 256 => 160, 160, 128 => 80, 80, 128
  34. x_2 = self.cv2(x)
  35. x_2 = self.cv3(x_2)
  36. # 80, 80, 128 cat 80, 80, 128 => 80, 80, 256
  37. return torch.cat([x_2, x_1], 1)

 这里小编建议大家复制一份原函数,在复制的函数上进行修改,不会破坏其他层的正常运行。

下面是修改之后的Transition_Block和Multi_Concat_Block函数: 

  1. class Multi_Concat_Block_DeformConv(nn.Module):
  2. def __init__(self, c1, c2, c3, n=4, e=1, ids=[0]):
  3. super(Multi_Concat_Block_DeformConv, self).__init__()
  4. c_ = int(c2 * e)
  5. self.ids = ids
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c1, c_, 1, 1)
  8. self.cv3 = nn.ModuleList(
  9. [DeformConv2d(c_ if i == 0 else c2, c2, 3, 1) for i in range(n)]
  10. )
  11. # 这里将Conv替换成了DeformConv()函数,符合原论文中的结构
  12. self.cv4 = Conv(c_ * 2 + c2 * (len(ids) - 2), c3, 1, 1)
  13. def forward(self, x):
  14. x_1 = self.cv1(x)
  15. x_2 = self.cv2(x)
  16. x_all = [x_1, x_2]
  17. # [-1, -3, -5, -6] => [5, 3, 1, 0]
  18. for i in range(len(self.cv3)):
  19. x_2 = self.cv3[i](x_2)
  20. x_all.append(x_2)
  21. out = self.cv4(torch.cat([x_all[id] for id in self.ids], 1))# 最后torch.cat堆叠之后再cv4卷积
  22. return out
  23. class Transition_Block_DeformConv(nn.Module):
  24. def __init__(self, c1, c2):
  25. super(Transition_Block_DeformConv, self).__init__()
  26. self.cv1 = Conv(c1, c2, 1, 1)
  27. self.cv2 = Conv(c1, c2, 1, 1)
  28. self.cv3 = Conv(c2, c2, 3, 2)
  29. self.DeformConv = DeformConv2d(c2, c2, 3, 2)# 定义可变卷积
  30. self.mp = MP()
  31. def forward(self, x):
  32. # 160, 160, 256 => 80, 80, 256 => 80, 80, 128
  33. x_1 = self.mp(x)
  34. x_1 = self.cv1(x_1)
  35. # 160, 160, 256 => 160, 160, 128 => 80, 80, 128
  36. x_2 = self.cv2(x)
  37. # x_2 = self.cv3(x_2)
  38. x_2 = self.DeformConv(x_2)# 加了一个可变卷积代替原来的cv3
  39. # 80, 80, 128 cat 80, 80, 128 => 80, 80, 256
  40. return torch.cat([x_2, x_1], 1)

这里要注意一点,在DeformConv2d和使用的时候的stride和padding一定要对应,否则会出现维度不正确的错误!

接下来我们改变backbone部分的dark5部分,下图是修改后的backbone代码:

  1. class Backbone(nn.Module):
  2. def __init__(self, transition_channels, block_channels, n, phi, pretrained=False):
  3. super().__init__()
  4. #-----------------------------------------------#
  5. # 输入图片是640, 640, 3
  6. #-----------------------------------------------#
  7. ids = {
  8. 'l' : [-1, -3, -5, -6],
  9. 'x' : [-1, -3, -5, -7, -8],
  10. }[phi]
  11. # 640, 640, 3 => 640, 640, 32 => 320, 320, 64
  12. self.stem = nn.Sequential(
  13. Conv(3, transition_channels, 3, 1),
  14. Conv(transition_channels, transition_channels * 2, 3, 2),
  15. Conv(transition_channels * 2, transition_channels * 2, 3, 1),
  16. )
  17. # 320, 320, 64 => 160, 160, 128 => 160, 160, 256
  18. self.dark2 = nn.Sequential(
  19. Conv(transition_channels * 2, transition_channels * 4, 3, 2),
  20. Multi_Concat_Block(transition_channels * 4, block_channels * 2, transition_channels * 8, n=n, ids=ids),
  21. )
  22. # 160, 160, 256 => 80, 80, 256 => 80, 80, 512
  23. self.dark3 = nn.Sequential(
  24. Transition_Block(transition_channels * 8, transition_channels * 4),
  25. Multi_Concat_Block(transition_channels * 8, block_channels * 4, transition_channels * 16, n=n, ids=ids),
  26. )
  27. # 80, 80, 512 => 40, 40, 512 => 40, 40, 1024
  28. self.dark4 = nn.Sequential(
  29. Transition_Block(transition_channels * 16, transition_channels * 8),
  30. Multi_Concat_Block(transition_channels * 16, block_channels * 8, transition_channels * 32, n=n, ids=ids),
  31. )
  32. # 40, 40, 1024 => 20, 20, 1024 => 20, 20, 1024
  33. self.dark5 = nn.Sequential(
  34. Transition_Block_DeformConv(transition_channels * 32, transition_channels * 16),
  35. Multi_Concat_Block_DeformConv(transition_channels * 32, block_channels * 8, transition_channels * 32, n=n, ids=ids),
  36. )
  37. # 这里我进行了修改,将原本的Transition_Block和Muti_Concat_Block中的Conv部分使用DeformConv进行了替换
  38. if pretrained:
  39. url = {
  40. "l" : 'https://github.com/bubbliiiing/yolov7-pytorch/releases/download/v1.0/yolov7_backbone_weights.pth',
  41. "x" : 'https://github.com/bubbliiiing/yolov7-pytorch/releases/download/v1.0/yolov7_x_backbone_weights.pth',
  42. }[phi]
  43. checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", model_dir="./model_data")
  44. self.load_state_dict(checkpoint, strict=False)
  45. print("Load weights from " + url.split('/')[-1])
  46. def forward(self, x):
  47. x = self.stem(x)
  48. x = self.dark2(x)
  49. #-----------------------------------------------#
  50. # dark3的输出为80, 80, 512,是一个有效特征层
  51. #-----------------------------------------------#
  52. x = self.dark3(x)
  53. feat1 = x
  54. #-----------------------------------------------#
  55. # dark4的输出为40, 40, 1024,是一个有效特征层
  56. #-----------------------------------------------#
  57. x = self.dark4(x)
  58. feat2 = x
  59. #-----------------------------------------------#
  60. # dark5的输出为20, 20, 1024,是一个有效特征层
  61. #-----------------------------------------------#
  62. x = self.dark5(x)
  63. feat3 = x
  64. return feat1, feat2, feat3

这样一来,在yolov7 backbone部分最后一个特征图的输出就得到了改变,里面Transition_Block和Multi_Concat_Block函数中的卷积发生了变换。

最后我们就可以开始训练啦

 这里小编这里刚开始训练了几轮,结果还没出来。。。

大家也可以随机发挥,将其他部分的卷积也可以替换成可变卷积结构,不过最终的mAP值小编这里就不知道啦,哈哈哈。

感谢您的观看!


                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/479201
推荐阅读
  

闽ICP备14008679号