【YOLOv5/v7改進系列】改進池化層為RFB

一、導言

論文 "Receptive Field Block Net for Accurate and Fast Object Detection" 中提出的 RFB (Receptive Field Block) 模塊旨在模仿人類視覺系統中的感受野結構,以增強深度學習模型對不同尺度和位置的目標檢測能力。下面總結了RFB模塊的主要優點和潛在局限性。

優點:

  1. 增強特征區分性和魯棒性: RFB模塊通過模擬人類視覺系統中感受野的大小和偏心率之間的關系,增強了輕量級CNN網絡的特征表示,從而提高了特征的區分性和魯棒性。

  2. 多尺度信息整合: RFB模塊利用多個分支,每個分支有不同的卷積核大小和空洞卷積層,這有助于捕捉圖像中不同尺度的信息,類似于人類視覺系統中感受野的多樣性。

  3. 實時性能: 實驗表明,將RFB模塊添加到SSD之上構建的RFB Net不僅保持了實時處理速度,而且在Pascal VOC和MS COCO數據集上達到了與高級深層檢測器相當的性能。

  4. 通用性: RFB模塊可以很容易地鏈接到不同的輕量級網絡架構上,如MobileNet,這證明了它的通用性和適應性。

  5. 從零開始訓練的能力: RFB模塊還顯示出即使在沒有預訓練的情況下也能有效訓練物體檢測器的能力,這在某些情況下可能是一個優勢。

局限性:

  1. 參數增加: 盡管RFB模塊可以提高精度,但它也增加了額外的層和參數,這可能會影響計算效率和資源需求,尤其是在低端設備上。

  2. 特定設計的局限性: RFB模塊的設計是手工制作的,這意味著它可能不如自適應機制靈活,例如可變形卷積神經網絡中使用的那些,后者可以根據對象的尺度和形狀調整感受野的分布。

  3. 對感受野的簡化假設: RFB模塊通過固定大小和偏心率的感受野來近似人類視覺系統,這可能忽略了更復雜的人類視覺處理機制,如動態調整或非均勻采樣。

  4. 依賴于基礎網絡: RFB模塊的性能很大程度上依賴于底層的基礎網絡,這意味著在不同的網絡架構上的效果可能會有所不同。

盡管存在這些局限性,RFB模塊在物體檢測任務中表現出了顯著的改進,并且在保持實時處理速度的同時,提供了一種增強輕量級模型檢測精度的有效方法。

二、準備工作

首先在YOLOv5/v7的models文件夾下新建文件morepooling.py,導入如下代碼

from models.common import *# RFB
# https://arxiv.org/pdf/1711.07767
class BasicConv(nn.Module):def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True,bn=True):super(BasicConv, self).__init__()self.out_channels = out_planesif bn:self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding,dilation=dilation, groups=groups, bias=False)self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True)self.relu = nn.ReLU(inplace=True) if relu else Noneelse:self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding,dilation=dilation, groups=groups, bias=True)self.bn = Noneself.relu = nn.ReLU(inplace=True) if relu else Nonedef forward(self, x):x = self.conv(x)if self.bn is not None:x = self.bn(x)if self.relu is not None:x = self.relu(x)return xclass BasicRFB(nn.Module):def __init__(self, in_planes, out_planes, stride=1, scale=0.1, map_reduce=8, vision=1, groups=1):super(BasicRFB, self).__init__()self.scale = scaleself.out_channels = out_planesinter_planes = in_planes // map_reduceself.branch0 = nn.Sequential(BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision, dilation=vision,relu=False, groups=groups))self.branch1 = nn.Sequential(BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 2,dilation=vision + 2, relu=False, groups=groups))self.branch2 = nn.Sequential(BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),BasicConv(inter_planes, (inter_planes // 2) * 3, kernel_size=3, stride=1, padding=1, groups=groups),BasicConv((inter_planes // 2) * 3, 2 * inter_planes, kernel_size=3, stride=stride, padding=1,groups=groups),BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 4,dilation=vision + 4, relu=False, groups=groups))self.ConvLinear = BasicConv(6 * inter_planes, out_planes, kernel_size=1, stride=1, relu=False)self.shortcut = BasicConv(in_planes, out_planes, kernel_size=1, stride=stride, relu=False)self.relu = nn.ReLU(inplace=False)def forward(self, x):x0 = self.branch0(x)x1 = self.branch1(x)x2 = self.branch2(x)out = torch.cat((x0, x1, x2), 1)out = self.ConvLinear(out)short = self.shortcut(x)out = out * self.scale + shortout = self.relu(out)return out

其次在在YOLOv5/v7項目文件下的models/yolo.py中在文件首部添加代碼

from models.morepooling import *

并搜索def parse_model(d, ch)

定位到如下行添加以下代碼

BasicRFB

三、YOLOv7-tiny改進工作

完成二后,在YOLOv7項目文件下的models文件夾下創建新的文件yolov7-tiny-rfb.yaml,導入如下代碼。

# parameters
nc: 80  # number of classes
depth_multiple: 1.0  # model depth multiple
width_multiple: 1.0  # layer channel multiple# anchors
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# yolov7-tiny backbone
backbone:# [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True[[-1, 1, Conv, [32, 3, 2, None, 1, nn.LeakyReLU(0.1)]],  # 0-P1/2[-1, 1, Conv, [64, 3, 2, None, 1, nn.LeakyReLU(0.1)]],  # 1-P2/4[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 7[-1, 1, MP, []],  # 8-P3/8[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 14[-1, 1, MP, []],  # 15-P4/16[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 21[-1, 1, MP, []],  # 22-P5/32[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [512, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 28]# yolov7-tiny head
head:[[-1, 1, BasicRFB, [256]], # 29[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[21, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P4[[-1, -2], 1, Concat, [1]],[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 39[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[14, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P3[[-1, -2], 1, Concat, [1]],[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 49[-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],[[-1, 39], 1, Concat, [1]],[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 57[-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],[[-1, 29], 1, Concat, [1]],[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[-1, -2, -3, -4], 1, Concat, [1]],[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],  # 65[49, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[57, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[65, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],[[66, 67, 68], 1, IDetect, [nc, anchors]],   # Detect(P3, P4, P5)]
from  n    params  module                                  arguments                     0                -1  1       928  models.common.Conv                      [3, 32, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]2                -1  1      2112  models.common.Conv                      [64, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]3                -2  1      2112  models.common.Conv                      [64, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]4                -1  1      9280  models.common.Conv                      [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]5                -1  1      9280  models.common.Conv                      [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]6  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           7                -1  1      8320  models.common.Conv                      [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]8                -1  1         0  models.common.MP                        []                            9                -1  1      4224  models.common.Conv                      [64, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]10                -2  1      4224  models.common.Conv                      [64, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]11                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]12                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]13  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]15                -1  1         0  models.common.MP                        []                            16                -1  1     16640  models.common.Conv                      [128, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]17                -2  1     16640  models.common.Conv                      [128, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]18                -1  1    147712  models.common.Conv                      [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]19                -1  1    147712  models.common.Conv                      [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]20  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           21                -1  1    131584  models.common.Conv                      [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]22                -1  1         0  models.common.MP                        []                            23                -1  1     66048  models.common.Conv                      [256, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]24                -2  1     66048  models.common.Conv                      [256, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]25                -1  1    590336  models.common.Conv                      [256, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]26                -1  1    590336  models.common.Conv                      [256, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]27  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           28                -1  1    525312  models.common.Conv                      [1024, 512, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]29                -1  1   1086528  models.morepooling.BasicRFB             [512, 256]                    30                -1  1     33024  models.common.Conv                      [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]31                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          32                21  1     33024  models.common.Conv                      [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]33          [-1, -2]  1         0  models.common.Concat                    [1]                           34                -1  1     16512  models.common.Conv                      [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]35                -2  1     16512  models.common.Conv                      [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]36                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]37                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]38  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           39                -1  1     33024  models.common.Conv                      [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]40                -1  1      8320  models.common.Conv                      [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]41                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          42                14  1      8320  models.common.Conv                      [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]43          [-1, -2]  1         0  models.common.Concat                    [1]                           44                -1  1      4160  models.common.Conv                      [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]45                -2  1      4160  models.common.Conv                      [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]46                -1  1      9280  models.common.Conv                      [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]47                -1  1      9280  models.common.Conv                      [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]48  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           49                -1  1      8320  models.common.Conv                      [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]50                -1  1     73984  models.common.Conv                      [64, 128, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]51          [-1, 39]  1         0  models.common.Concat                    [1]                           52                -1  1     16512  models.common.Conv                      [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]53                -2  1     16512  models.common.Conv                      [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]54                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]55                -1  1     36992  models.common.Conv                      [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]56  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           57                -1  1     33024  models.common.Conv                      [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]58                -1  1    295424  models.common.Conv                      [128, 256, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]59          [-1, 29]  1         0  models.common.Concat                    [1]                           60                -1  1     65792  models.common.Conv                      [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]61                -2  1     65792  models.common.Conv                      [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]62                -1  1    147712  models.common.Conv                      [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]63                -1  1    147712  models.common.Conv                      [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]64  [-1, -2, -3, -4]  1         0  models.common.Concat                    [1]                           65                -1  1    131584  models.common.Conv                      [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]66                49  1     73984  models.common.Conv                      [64, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]67                57  1    295424  models.common.Conv                      [128, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]68                65  1   1180672  models.common.Conv                      [256, 512, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]69      [66, 67, 68]  1     17132  models.yolo.IDetect                     [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]Model Summary: 284 layers, 6444108 parameters, 6444108 gradients, 13.5 GFLOPS

運行后若打印出如上文本代表改進成功。

四、YOLOv5s改進工作

完成二后,在YOLOv5項目文件下的models文件夾下創建新的文件yolov5s-rfb.yaml,導入如下代碼。

# Parameters
nc: 1  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # 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, [128]],[-1, 1, Conv, [256, 3, 2]],  # 3-P3/8[-1, 6, C3, [256]],[-1, 1, Conv, [512, 3, 2]],  # 5-P4/16[-1, 9, C3, [512]],[-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32[-1, 3, C3, [1024]],#[-1, 1, ASPP, [1024]],  # 9[-1, 1, BasicRFB, [1024]],#[-1, 1, SPP, [1024]],#[-1, 1, SPPF, [1024, 5]],  # 9#[-1, 1, SPPCSPC, [1024]],]# 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, [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, [256, False]],  # 17 (P3/8-small)[-1, 1, Conv, [256, 3, 2]],[[-1, 14], 1, Concat, [1]],  # cat head P4[-1, 3, C3, [512, False]],  # 20 (P4/16-medium)[-1, 1, Conv, [512, 3, 2]],[[-1, 10], 1, Concat, [1]],  # cat head P5[-1, 3, C3, [1024, False]],  # 23 (P5/32-large)[[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)]
from  n    params  module                                  arguments                     0                -1  1      3520  models.common.Conv                      [3, 32, 6, 2, 2]              1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                2                -1  1     18816  models.common.C3                        [64, 64, 1]                   3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               4                -1  2    115712  models.common.C3                        [128, 128, 2]                 5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              6                -1  3    625152  models.common.C3                        [256, 256, 3]                 7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              8                -1  1   1182720  models.common.C3                        [512, 512, 1]                 9                -1  1   1316928  models.morepooling.BasicRFB             [512, 512]                    10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          12           [-1, 6]  1         0  models.common.Concat                    [1]                           13                -1  1    361984  models.common.C3                        [512, 256, 1, False]          14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          16           [-1, 4]  1         0  models.common.Concat                    [1]                           17                -1  1     90880  models.common.C3                        [256, 128, 1, False]          18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              19          [-1, 14]  1         0  models.common.Concat                    [1]                           20                -1  1    296448  models.common.C3                        [256, 256, 1, False]          21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              22          [-1, 10]  1         0  models.common.Concat                    [1]                           23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          24      [17, 20, 23]  1     16182  models.yolo.Detect                      [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]Model Summary: 305 layers, 7682358 parameters, 7682358 gradients, 16.5 GFLOPs

運行后若打印出如上文本代表改進成功。

五、YOLOv5n改進工作

完成二后,在YOLOv5項目文件下的models文件夾下創建新的文件yolov5n-rfb.yaml,導入如下代碼。

# Parameters
nc: 1  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.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, [128]],[-1, 1, Conv, [256, 3, 2]],  # 3-P3/8[-1, 6, C3, [256]],[-1, 1, Conv, [512, 3, 2]],  # 5-P4/16[-1, 9, C3, [512]],[-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32[-1, 3, C3, [1024]],#[-1, 1, ASPP, [1024]],  # 9[-1, 1, BasicRFB, [1024]],#[-1, 1, SPP, [1024]],#[-1, 1, SPPF, [1024, 5]],  # 9#[-1, 1, SPPCSPC, [1024]],]# 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, [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, [256, False]],  # 17 (P3/8-small)[-1, 1, Conv, [256, 3, 2]],[[-1, 14], 1, Concat, [1]],  # cat head P4[-1, 3, C3, [512, False]],  # 20 (P4/16-medium)[-1, 1, Conv, [512, 3, 2]],[[-1, 10], 1, Concat, [1]],  # cat head P5[-1, 3, C3, [1024, False]],  # 23 (P5/32-large)[[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)]
from  n    params  module                                  arguments                     0                -1  1      1760  models.common.Conv                      [3, 16, 6, 2, 2]              1                -1  1      4672  models.common.Conv                      [16, 32, 3, 2]                2                -1  1      4800  models.common.C3                        [32, 32, 1]                   3                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                4                -1  2     29184  models.common.C3                        [64, 64, 2]                   5                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               6                -1  3    156928  models.common.C3                        [128, 128, 3]                 7                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              8                -1  1    296448  models.common.C3                        [256, 256, 1]                 9                -1  1    330272  models.morepooling.BasicRFB             [256, 256]                    10                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          12           [-1, 6]  1         0  models.common.Concat                    [1]                           13                -1  1     90880  models.common.C3                        [256, 128, 1, False]          14                -1  1      8320  models.common.Conv                      [128, 64, 1, 1]               15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          16           [-1, 4]  1         0  models.common.Concat                    [1]                           17                -1  1     22912  models.common.C3                        [128, 64, 1, False]           18                -1  1     36992  models.common.Conv                      [64, 64, 3, 2]                19          [-1, 14]  1         0  models.common.Concat                    [1]                           20                -1  1     74496  models.common.C3                        [128, 128, 1, False]          21                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              22          [-1, 10]  1         0  models.common.Concat                    [1]                           23                -1  1    296448  models.common.C3                        [256, 256, 1, False]          24      [17, 20, 23]  1      8118  models.yolo.Detect                      [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [64, 128, 256]]Model Summary: 305 layers, 1930934 parameters, 1930934 gradients, 4.4 GFLOPs
六、代碼部分的解釋

BasicRFB 類繼承自 PyTorch 的 nn.Module。構造函數接收輸入通道數 in_planes、輸出通道數 out_planes 和一些控制模塊行為的參數,比如步長 stride、縮放因子 scale、中間層通道減少倍數 map_reduce、擴張率 vision 和分組卷積的組數 groups

三個分支分別定義為 branch0branch1branch2。每個分支都由多個 BasicConv 組件構成,其中包含卷積、批量歸一化和激活函數。分支的內核大小、填充和擴張率各不相同,目的是捕捉不同尺度的信息。branch0 使用標準卷積和擴張卷積;branch1 使用稍大的擴張率;而 branch2 則使用更大的擴張率和額外的卷積層,進一步增加感受野的多樣性。

最終,三個分支的輸出被拼接在一起,并通過 ConvLinear 卷積層轉換為期望的輸出通道數。此外,還有一個快捷連接(shortcut)從輸入直接傳遞到輸出,與 ResNet 結構類似,用于緩解梯度消失問題。所有分支的輸出和快捷連接的輸出在相加前會乘以一個縮放因子 scale,然后經過 ReLU 激活函數,得到最終的輸出。

整個 BasicRFB 模塊在前向傳播時,將輸入通過三個分支并行處理,然后將結果拼接后進行線性變換,最后加上快捷連接的輸出。這種設計使得模塊能夠高效地捕獲多尺度特征,同時保持實時處理速度,適用于目標檢測等計算機視覺任務。

運行后打印如上代碼說明改進成功。

更多文章產出中,主打簡潔和準確,歡迎關注我,共同探討!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:
http://www.pswp.cn/web/41737.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/41737.shtml
英文地址,請注明出處:http://en.pswp.cn/web/41737.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MySQL數據庫巡檢步驟

MySQL巡檢 系統基本信息 機型號 IP CPU 內存 磁盤 (業務)系統信息 操作系統 主機名 操作系統巡檢 檢查內容 說明 檢查方法 結果(異常需詳細說明) 正常輸出結果 系統配置檢查 操作系 統版本 #uname –a □正常 □異常 顯示系統版本和核心補丁信…

AIGC時代程序員的躍遷——編程高手的密碼武器

💝💝💝歡迎來到我的博客,很高興能夠在這里和您見面!希望您在這里可以感受到一份輕松愉快的氛圍,不僅可以獲得有趣的內容和知識,也可以暢所欲言、分享您的想法和見解。 推薦:kwan 的首頁,持續學…

一、redis-萬字長文讀懂redis

高性能分布式緩存Redis `第一篇章`1.1緩存發展史&緩存分類1.1.1 大型網站中緩存的使用帶來的問題1.1.2 常見緩存的分類及對比與memcache對比1.2 數據類型選擇&應用場景1.2.1 string1.2.2 hash1.2.3 鏈表1.2.4 set1.2.5 sortedset有序集合類型1.2.6 總結1.3 Redis高級應…

[數倉]三、離線數倉(Hive數倉系統)

第1章 數倉分層 1.1 為什么要分層 DIM:dimensionality 維度 1.2 數據集市與數據倉庫概念 1.3 數倉命名規范 1.3.1 表命名 ODS層命名為ods_表名DIM層命名為dim_表名DWD層命名為dwd_表名DWS層命名為dws_表名 DWT層命名為dwt_表名ADS層命名為ads_表名臨時表命名為…

昇思25天訓練營Day11 - 基于 MindSpore 實現 BERT 對話情緒識別

模型簡介 BERT全稱是來自變換器的雙向編碼器表征量(Bidirectional Encoder Representations from Transformers),它是Google于2018年末開發并發布的一種新型語言模型。與BERT模型相似的預訓練語言模型例如問答、命名實體識別、自然語言推理、…

56、最近鄰向量量化(LVQ) 網絡訓練對輸入向量進行分類

1、LVQ 網絡訓練對輸入向量進行分類簡介 1)簡介 LVQ(最近鄰向量量化)是一種簡單而有效的神經網絡模型,用于對輸入向量進行分類。LVQ網絡通過學習一組原型向量(也稱為代碼矢量或參考向量),來表…

HTML5 WebSocket技術使用詳解

HTML5 WebSocket API 提供了一種在單個連接上進行全雙工通信的方式。這意味著客戶端和服務器可以同時發送和接收數據,而不需要像傳統的 HTTP 請求那樣進行多次請求和響應的輪詢。WebSocket 允許更實時的交互,非常適合需要快速、連續數據交換的應用場景&a…

SAP Build4-office 操作

1. 郵件操作 1.1 前期準備 商店中找到outlook的sdk,添加到build中 在process中添加outlook的SDK 電腦上裝了outlook的郵箱并且已經登錄 我用個人foxmail郵箱向outlook發了一封帶附件的銷售訂單郵件,就以此作為例子 1.2 搜索郵件 搜索有兩層&…

計算機視覺、目標檢測、視頻分析的過去和未來:目標檢測從入門到精通 ------ YOLOv8 到 多模態大模型處理視覺基礎任務

文章大綱 計算機視覺項目的關鍵步驟計算機視覺項目核心內容概述步驟1: 確定項目目標步驟2:數據收集和數據標注步驟3:數據增強和拆分數據集步驟4:模型訓練步驟5:模型評估和模型微調步驟6:模型測試步驟7:模型部署常見問題目標檢測入門什么是目標檢測目標檢測算法的分類一階…

CSS實現圖片裁剪居中(只截取剪裁圖片中間部分,圖片不變形)

1.第一種方式:(直接給圖片設置:object-fit:cover;) .imgbox{width: 100%;height:200px;overflow: hidden;position: relative;img{width: 100%;height: 100%; //圖片要設置高度display: block;position: absolute;left: 0;right…

OpenCV:解鎖計算機視覺的魔法鑰匙

OpenCV:解鎖計算機視覺的魔法鑰匙 在人工智能與圖像處理的世界里,OpenCV是一個響當當的名字。作為計算機視覺領域的瑞士軍刀,OpenCV以其豐富的功能庫、跨平臺的特性以及開源的便利性,成為了開發者手中不可或缺的工具。本文將深入…

基于Java+SpringMvc+Vue技術的在線學習交流平臺的設計與實現---60頁論文參考

博主介紹:碩士研究生,專注于Java技術領域開發與管理,以及畢業項目實戰? 從事基于java BS架構、CS架構、c/c 編程工作近16年,擁有近12年的管理工作經驗,擁有較豐富的技術架構思想、較扎實的技術功底和資深的項目管理經…

AI+若依框架(低代碼開發)

提前說明: 文章是實時更新,寫了就會更。 文章是黑馬視頻的筆記,如果要自己學可以點及下面的鏈接: https://www.bilibili.com/video/BV1pf421B71v/一、若依介紹 1.版本介紹 若依為滿足多樣化的開發需求,提供了多個版本…

基于jeecgboot-vue3的Flowable流程-集成仿釘釘流程(一)圖標svgicon的使用

因為這個項目license問題無法開源,更多技術支持與服務請加入我的知識星球。 1、lowflow這里使用了tsx的動態圖標,如下: import ./index.scss import type { CSSProperties, PropType } from vue import { computed, defineComponent, resolv…

MATLAB基礎應用精講-【數模應用】 嶺回歸(Ridge)(附MATLAB、python和R語言代碼實現)

目錄 前言 算法原理 數學模型 Ridge 回歸的估計量 Ridge 回歸與標準多元線性回歸的比較 3. Ridge 參數的選擇 算法步驟 SPSSPRO 1、作用 2、輸入輸出描述 3、案例示例 4、案例數據 5、案例操作 6、輸出結果分析 7、注意事項 8、模型理論 SPSSAU 嶺回歸分析案…

Java [ 進階 ] 深入理解 JVM

?探索Java基礎 深入理解 JVM? 深入理解 JVM:結構與垃圾回收機制 Java 虛擬機(JVM)是 Java 程序運行的核心,了解 JVM 的內部結構和垃圾回收機制對優化 Java 應用性能至關重要。本文將深入探討 JVM 的結構和垃圾回收機制&#…

支付寶沙箱對接(GO語言)

支付寶沙箱對接 1.1 官網1.2 秘鑰生成(系統默認)1.3 秘鑰生成(軟件生成)1.4 golan 安裝 SDK1.5 GoLand 代碼1.6 前端代碼 1.1 官網 沙箱官網: https://open.alipay.com/develop/sandbox/app 秘鑰用具下載: https://ope…

序列化、反序列化

java 提供了一種對象序列化的機制,該機制中,一個對象可以被表示為一個字節序列,該字節序列包括該對象的數據、有關對象的類型的信息和存儲在對象中數據的類型。 將序列化對象寫入文件之后,可以從文件中讀取出來,并且對…

Java并發編程-ThreadLocal深入解讀及案例實戰

文章目錄 概述原理使用場景示例最佳實踐內存泄漏風險阿里開源組件TransmittableThreadLocal原理和機制使用場景如何使用注意事項ThreadLocal在分布式存儲系統edits_log案例中的實踐1. 為什么使用`ThreadLocal`?2. 實踐案例2.1 緩存日志操作2.2 線程局部的編輯日志狀態3. 注意事…

在 Spring 中編寫單元測試

單元測試是軟件開發過程中不可或缺的一部分,它能有效地提高代碼質量,確保代碼功能的正確性。在 Spring 應用中,JUnit 和 Mockito 是常用的單元測試工具,而 Spring Test 提供了豐富的測試支持。本文將介紹如何在 Spring 中使用 JUn…