目录

  • 1 Res2Net
  • 2 YOLOv5中添加Res2Net block
    • 2.1 common.py配置
    • 2.2 yolo.py配置
    • 2.3.3 创建添加Res2Net block模块的YOLOv5的yaml配置文件 


1 Res2Net


关于代表性计算机视觉任务的进一步消融研究和实验结果,即目标检测,类激活 mapping和显著目标检测,进一步验证了Res2Net相对于现有技术的基线方法的优越性。


面向视觉任务的多尺度表示对于目标检测、语义分割和显著目标检测任务具有重大意义。通过CNN新模块Res2Net,能够实现与比以往优秀的基于CNN backbone 的模型(如ResNet,ResNeXt和DLA)更好的性能表现。


Res2Net:计算负载不增加,特征提取能力更强大


在多个尺度上表示特征对于许多视觉任务非常重要。卷积神经网络(CNN) backbone 的最新进展不断展示出更强的多尺度表示能力,从而在广泛的应用中实现一致的性能提升。然而,大多数现有方法以分层方式(layer-wise)表示多尺度特征。


在本文中,研究人员在一个单个残差块内构造分层的残差类连接,为CNN提出了一种新的构建模块,即Res2Net——以更细粒度(granular level)表示多尺度特征,并增加每个网络层的感受野(receptive fields)范围。


上图中,左侧为CNN网络架构的基本构成,右侧为本文新提出的Res2Net模块。新模块具备更强的多规模特征提取能力,但计算负载量与左侧架构类似。具体而言,新模块用一个较小的3×3过滤器取代了过滤器组,同时可以将不同的过滤器组以层级残差式风格连接。模块内部的连接形式与残差网络(ResNet)类似,故命名为Res2Net。


本文所提出的Res2Net模块可以融合到最先进的backbone CNN模型中,例如ResNet,ResNeXt和DLA。研究人员在所有这些模型上评估 Res2Net 模块,并在广泛使用的数据集(例如CIFAR-100和ImageNet)上展示相对于基线模型的一致性能提升。

由于单独的Res2Net模块对于整体网络结构没有特定的要求,Res2Net模块的多尺度表示能力也和CNN的分层特征聚合模型彼此独立,所以可以很容易地将Res2Net模块集成到现有的其他优秀CNN模型中。比如ResNet,ResNeXt 和DLA 等。Res2Net结构简单,性能优秀,可以进一步探索CNN在更细粒度级别的多尺度表示能力。 Res2Net揭示了一个新的维度,即“尺度”(Scale),除了深度,宽度和基数的现有维度之外,“规模”是一个必不可少的更有效的因素。
Res2Net模块可以很容易地与现有的最新模块整合。对CIFAR100和ImageNet基准测试的图像分类结果表明,使用Res2Net模块的网络始终在与对手的竞争中表现出更优秀的性能,这些对手包括ResNet,ResNeXt,DLA等。Res2Net性能上的优越性已经在几个具有代表性的计算机视觉任务体现出来,包括类激活映射,对象检测和显着对象检测等。多尺度表示对于未来开拓更广泛的应用领域至关重要。


2 YOLOv5中添加Res2Net block


2.1 common.py配置


在yolov5-6.1/models/common.py文件中增加以下模块,直接复制即可。

class Bottle2neck(nn.Module):
    expansion = 1
 
    def __init__(self, inplanes, planes, shortcut, baseWidth=26, scale = 4):
        """ Constructor
        Args:
            inplanes: input channel dimensionality
            planes: output channel dimensionality
            baseWidth: basic width of conv3x3
            scale: number of scale.
        """
        super(Bottle2neck, self).__init__()
 
        width = int(math.floor(planes * (baseWidth/64.0)))
        self.conv1 = Conv(inplanes, width*scale, k=1)
        
        if scale == 1:
          self.nums = 1
        else:
          self.nums = scale -1
        convs = []
        for i in range(self.nums):
          convs.append(Conv(width, width, k=3))
        self.convs = nn.ModuleList(convs)
 
        self.conv3 = Conv(width*scale, planes * self.expansion, k=1, act=False)
 
        self.silu = nn.SiLU(inplace=True)
        self.scale = scale
        self.width  = width
        self.shortcut = shortcut
 
    def forward(self, x):
        print(1)
        if self.shortcut:
            residual = x
        out = self.conv1(x)
        spx = torch.split(out, self.width, 1)
        for i in range(self.nums):
          if i==0:
            sp = spx[i]
          else:
            sp = sp + spx[i]
          sp = self.convs[i](sp)
          if i==0:
            out = sp
          else:
            out = torch.cat((out, sp), 1)
        if self.scale != 1:
          out = torch.cat((out, spx[self.nums]),1)
 
        out = self.conv3(out)
        if self.shortcut:
            out += residual
        out = self.silu(out)
        return out
 
class C3_Res2Block(C3):
    # CSP Bottleneck with 3 convolutions
    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):  # ch_in, ch_out, number, shortcut, groups, expansion
        super().__init__(c1, c2, n, shortcut, g, e)
        c_ = int(c2 * e)  # hidden channels
        self.m = nn.Sequential(*(Bottle2neck(c_, c_, shortcut) for _ in range(n)))


2.2 yolo.py配置

然后找到yolov5-6.1/models//yolo.py文件下里的parse model函数,将类名加入进去,如下所示。


2.3.3 创建添加Res2Net block模块的YOLOv5的yaml配置文件

# YOLOv5  by Ultralytics, GPL-3.0 license
 
# Parameters
nc: 80  # number of classes
depth_multiple: 1.33  # model depth multiple
width_multiple: 1.25  # layer channel multiple
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32
 
# YOLOv5 v6.0 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Conv, [64, 6, 2, 2]],  # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4
   [-1, 3, C3_Res2Block, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8
   [-1, 6, C3_Res2Block, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16
   [-1, 9, C3_Res2Block, [512]],
   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32
   [-1, 3, C3_Res2Block, [1024]],
   [-1, 1, SPPF, [1024, 5]],  # 9
  ]
 
# YOLOv5 v6.0 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, C3_Res2Block, [512, False]],  # 13
 
   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, C3_Res2Block, [256, False]],  # 17 (P3/8-small)
 
   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 14], 1, Concat, [1]],  # cat head P4
   [-1, 3, C3_Res2Block, [512, False]],  # 20 (P4/16-medium)
 
   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 10], 1, Concat, [1]],  # cat head P5
   [-1, 3, C3_Res2Block, [1024, False]],  # 23 (P5/32-large)
 
   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]