Yolov5(tag v7.0)网络结构解读,以yolov5s为例

最近yolov5用的多,发现确实好用,于是较深入学了一下。下面按照训练的流程梳理一下网络的结构,同时也是自己记一下便于后面查阅。
同时,我也查了一些关于yolov5网络结构介绍的资料,发现大多是v5.0,少数v6.0的,我是从v7.0开始用,所以对比v7.0的代码有出入,于是打算梳理一下yolov5s的v7.0版本的网络结构。

1、模型构建, 在train.py下关于模型的定义

# Model
  check_suffix(weights, '.pt')  # check weights
  pretrained = weights.endswith('.pt')
  """定义模型的加载,是否是预训练模型还是配置文件格式的网络结构"""
  if pretrained:
      with torch_distributed_zero_first(LOCAL_RANK):
          weights = attempt_download(weights)  # download if not found locally
      ckpt = torch.load(weights, map_location='cpu')  # load checkpoint to CPU to avoid CUDA memory leak
      model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device)  # create
      exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else []  # exclude keys
      csd = ckpt['model'].float().state_dict()  # checkpoint state_dict as FP32
      csd = intersect_dicts(csd, model.state_dict(), exclude=exclude)  # intersect
      model.load_state_dict(csd, strict=False)  # load
      LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}')  # report
  else:
  	  """不是预训练模型加载网络配置文件,通过cfg参数传入,可在models下找到,如models/yolov5s.yaml"""
      model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device)  # create
      

根据上面的代码,我们看else后面的网络构建方法,这是以配置文件cfg传入,可以进行自定义和修改网络。可以看到 Model 有四个传入参数:

cfg 是网络配置文件
ch 是输入数据维度(通道)rgb图像
nc 是数据集类别数量
anchors 没细看,自查

然后去定义 Model 的位置,找到models.yolo,可以看到:
在这里插入图片描述
Model = DetectionModel,DetectionModel继承于BaseModel,BaseModel继承于torch的类nn.Model,基本上知道了模型的构建流程。
再根据参数cfg看一下具体的网络构建:

class DetectionModel(BaseModel):
    # YOLOv5 detection model
    def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None):  # model, input channels, number of classes
        super().__init__()
        if isinstance(cfg, dict):
            self.yaml = cfg  # model dict
        else:  # is *.yaml
            import yaml  # for torch hub
            self.yaml_file = Path(cfg).name
            with open(cfg, encoding='ascii', errors='ignore') as f:
                self.yaml = yaml.safe_load(f)  # model dict

        # Define model
        ch = self.yaml['ch'] = self.yaml.get('ch', ch)  # input channels
        if nc and nc != self.yaml['nc']:
            LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}")
            self.yaml['nc'] = nc  # override yaml value
        if anchors:
            LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors}')
            self.yaml['anchors'] = round(anchors)  # override yaml value
        """self.model是构建好的模型"""
        self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch])  # model, savelist
        self.names = [str(i) for i in range(self.yaml['nc'])]  # default names
        self.inplace = self.yaml.get('inplace', True)

上面的模型构建代码中,self.model是构建好的模型,可以发现网络构建的具体执行由函数parse_model()完成

self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch])

从上可以发现函数parse_model()的输入是cfg对应的配置文件,如yolov5s.yaml,ch表示初始输入数据维度(通道),具体看看函数是如何构建网络的,可以结合后面贴上的以yolov5s.yaml为例的内容看

def parse_model(d, ch):  # model_dict, input_channels(3)
    # Parse a YOLOv5 model.yaml dictionary
    LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10}  {'module':<40}{'arguments':<30}")
    anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation')
    
    """anchors, nc, gd, gw分别是yolov5s的
    
    nc: 80  # 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

	"""
    if act:
        Conv.default_act = eval(act)  # redefine default activation, i.e. Conv.default_act = nn.SiLU()
        LOGGER.info(f"{colorstr('activation:')} {act}")  # print
    na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors  # number of anchors
    no = na * (nc + 5)  # number of outputs = anchors * (classes + 5)
	
	"""开始构建网络"""
    layers, save, c2 = [], [], ch[-1]  # layers, savelist, ch out
    for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']):  # from, number, module, args
    	"""
    	d['backbone'] + d['head']表示yolov5s.yaml的backbone和head的信息
    	f, n, m, args分别对应如下列表中每个单元的信息,f表示从哪一层输入,n表示该模块的数量,m表示模块名称,如Conv,args表示该模块数据进入的具体信息,[64, 6, 2, 2],64表示输出维度,6表示卷积核大小,2表示卷积步长,第二个2看具体定义
    	[[-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, SPPF, [1024, 5]],  # 9
  		]
    	"""
        m = eval(m) if isinstance(m, str) else m  # eval strings
        for j, a in enumerate(args):
            with contextlib.suppress(NameError):
                args[j] = eval(a) if isinstance(a, str) else a  # eval strings

        n = n_ = max(round(n * gd), 1) if n > 1 else n  # depth gain
        """
        m表示模块名称,通用的模块放一块便于设置输入输出的参数信息c1,c2
        """
        if m in {
                Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
                BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x}:
            """
            c1,c2表示该模块的数据输入输出维度,ch[f]=ch[-1]=3,具体看前面ch定义,表示数据输入维度;args[0]对应上面的64,说明该层输出的数据维度为64
            """
            c1, c2 = ch[f], args[0]
            if c2 != no:  # if not output
                c2 = make_divisible(c2 * gw, 8)

            args = [c1, c2, *args[1:]]
            if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x}:
                args.insert(2, n)  # number of repeats
                n = 1
        elif m is nn.BatchNorm2d:
            args = [ch[f]]
        elif m is Concat:
            c2 = sum(ch[x] for x in f)
        # TODO: channel, gw, gd
        elif m in {Detect, Segment}:
            args.append([ch[x] for x in f])
            if isinstance(args[1], int):  # number of anchors
                args[1] = [list(range(args[1] * 2))] * len(f)
            if m is Segment:
                args[3] = make_divisible(args[3] * gw, 8)
        elif m is Contract:
            c2 = ch[f] * args[0] ** 2
        elif m is Expand:
            c2 = ch[f] // args[0] ** 2
        else:
            c2 = ch[f]

		"""
		m_ 把模块m (如Conv)和 m 的输入参数 args 进行实例化并通过nn.sequential来把模块连接起来构建成网络
		"""
        m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args)  # module
        t = str(m)[8:-2].replace('__main__.', '')  # module type
        np = sum(x.numel() for x in m_.parameters())  # number params
        m_.i, m_.f, m_.type, m_.np = i, f, t, np  # attach index, 'from' index, type, number params
        LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f}  {t:<40}{str(args):<30}')  # print
        save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1)  # append to savelist
        layers.append(m_)
        if i == 0:
            ch = []
        ch.append(c2)
        """ch用于记录各层的输出维度,所以上面每次循环时的ch[f]=ch[-1]都会等于上一层的输出维度,可以保证数据维度对应"""
    return nn.Sequential(*layers), sorted(save)
    """
    最终由layers.append(m_)把各个模块实例化连接后再通过nn.Sequential(*layers)搭建成网络,由此得到model,完成模型构建
    """

下面是yolov5s.yaml的具体内容:

# YOLOv5 🚀 by Ultralytics, GPL-3.0 license

# Parameters
nc: 80  # 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, 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, [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)
  ]

2、根据训练过程验证网络结构,结合训练时输出的网络结构信息:

**backbone的前3个C3数量对应yolov5s.yaml的配置369分别除了3,变为1/3后的123,和模型深度参数有关depth_multiple: 0.33  model depth multiple**                  
层数,第几层      from  n    params  module                                  arguments
               ch[-1]  数量  参数量 模块名称(m)                           网络结构参数,输入维度,输出维度,卷积核大小,卷积步长
  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    656896  models.common.SPPF                      [512, 512, 5]                 
 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]]

可以发现上面的层数模块信息与yolov5s.yaml是对应的,部分参数做了处理有所变化。

3、调试代码可看具体模块参数,网络具体结构如下,我在模块前大致标记了层数是第几层:

[ **0 P1 /2:** Conv(
  (conv): Conv2d(3, 32, kernel_size=(6, 6), stride=(2, 2), padding=(2, 2), bias=False)
  (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **1 P2 /4:** Conv(
  (conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **2 C3**(
  (cv1): Conv(
    (conv): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(32, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **3 P3 /8:** Conv(
  (conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **4 C3**(
  (cv1): Conv(
    (conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
    (1): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **5 P4 /16:** Conv(
  (conv): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **6 C3**(
  (cv1): Conv(
    (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
    (1): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
    (2): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **7 P5 /32:** Conv(
  (conv): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **8 C3**(
  (cv1): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **9 SPPF**(
  (cv1): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): MaxPool2d(kernel_size=5, stride=1, padding=2, dilation=1, ceil_mode=False)
), **10 Conv**(
  (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
  (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **11 Upsample**(scale_factor=2.0, mode=nearest), **12 Concat(), C3**(
  (cv1): Conv(
    (conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **14 Conv**(
  (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
  (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **15 Upsample**(scale_factor=2.0, mode=nearest), **16 Concat(), C3**(
  (cv1): Conv(
    (conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **18 Conv**(
  (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **19 Concat(), C3**(
  (cv1): Conv(
    (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **21 Conv**(
  (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
  (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (act): SiLU()
), **22 Concat(), C3**(
  (cv1): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv2): Conv(
    (conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (cv3): Conv(
    (conv): Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
    (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (act): SiLU()
  )
  (m): Sequential(
    (0): Bottleneck(
      (cv1): Conv(
        (conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
      (cv2): Conv(
        (conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (act): SiLU()
      )
    )
  )
), **24 Detect**(
  (m): ModuleList(
    (0): Conv2d(128, 18, kernel_size=(1, 1), stride=(1, 1))
    (1): Conv2d(256, 18, kernel_size=(1, 1), stride=(1, 1))
    (2): Conv2d(512, 18, kernel_size=(1, 1), stride=(1, 1))
  )
)]

从上面可以进一步看到各模块的内部结构,如卷积操作的卷积核大小、卷积步长、输出输入啊维度等,使用的激活函数,batchnorm的方式等等,非常直观,但是网络并不是按照上面的顺序串连起来的。上面只是打印了各模块的信息,具体如何串联要结合模块里的forward函数进行判断,这个在common.py代码中有,可自行查看。

4、结合上述信息,下面是我根据查的结构图在yolov5s.yaml的基础上结合网络的参数做了修改以符合 yolov5s 的网络结构:

在这里插入图片描述
可以看出backbone部分网络做下采样操作,在第四层、第六层和第九层分别对应8,16,32倍下采样的输出数据进行后续neck处理,neck部分先是第十层做上采样到4倍下采样再继续做下采样,相当于做了两遍数据融合。最后的head部分分别在17,20,23抽取输出特征进行分类计算损失。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/30275.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

游泳戴的耳机推荐,列举感受水下快乐的游泳耳机

​游泳是个真心好玩的活动&#xff0c;对一般人来说简直是大杀器&#xff01;它不仅对身体没有太大伤害&#xff0c;还能锻炼到身体的大部分肌肉&#xff0c;对心肺也超级有帮助。不过&#xff0c;问题来了&#xff1a; 之前很少见到有人戴耳机游泳&#xff0c;主要是担心进水…

mpi实现矩阵乘法,卷积,池化(gemm,covn,pooling)

矩阵乘法&#xff1a; 卷积&#xff1a; 池化&#xff1a; Mpi基本原理&#xff1a; 1.什么是MPI Massage Passing Interface:是消息传递函数库的标准规范&#xff0c;由MPI论坛开发。 一种新的库描述&#xff0c;不是一种语言。共有上百个函数调用接口&#xff0c;提供与C和F…

phpstorm+xdebug/php项目调试

前提&#xff1a;项目使用xampp集成 一、下载xdebug&#xff0c;当到xampp/php/exp目录下 二、配置php.ini [Xdebug] zend_extension"D:/xampp/php/ext/php_xdebug.dll" xdebug.collect_paramsOn xdebug.collect_returnOn xdebug.auto_traceOn xdebug.trace_output_…

android adb 获取电池信息以及设置

本文主要包含 1、设置adb 无线调试桥连接步骤 2、打印设备电池状态(当前电量、充电状态、充放电电流大小、电池种类等&#xff09; 3、更改电池充电状态、电量百分比、电池还原命令 4、断开adb 远程调试桥 -----------------------------------------------------------------…

【数据结构与算法分析】树上漫步之探究前序、中序、后序、广度优先遍历算法的实现与优化

文章目录 前言二叉树的遍历方式构建二叉树递归遍历二叉树非递归遍历二叉树层次遍历 示例二叉树结果总结 前言 二叉树是数据结构中最基本的数据结构之一&#xff0c;它在计算机科学中有着非常重要的应用。二叉树的遍历是指按照一定的顺序遍历二叉树中的所有节点&#xff0c;是二…

【STM32训练—WiFi模块】第二篇、STM32驱动ESP8266WiFi模块获取天气

目录 第一部分、前言 1、获取心知天气API接口 2、硬件准备 第二部分、电脑串口助手调试WIFI模块获取天气 1、ESP8266获取天气的流程 2、具体步骤 第三部分、STM32驱动ESP8266模块获取天气数据 1、天气数据的解析 1.1、什么函数来解析天气数据&#xff1f; 2.1、解析后…

C语言之运算符

C语言运算符 文末附运算符的优先表和ASCII表 一、算术运算符 加(&#xff09;减&#xff08;—&#xff09;乘&#xff08;*&#xff09;除&#xff08;/&#xff09;模&#xff08;余&#xff09;运算符&#xff08;%&#xff09;&#xff1a;不允许出现浮点型&#xff0c;…

Linux---详细讲解linux计算机体系结构

前言 Linux是一种开源的操作系统&#xff0c;它的核心思想是基于冯诺依曼体系结构。在本文中&#xff0c;我们将深入探讨Linux的基本原理和操作系统的概念。 Linux是一款基于Unix操作系统的开源软件&#xff0c;它的核心是由Linus Torvalds在1991年开发的。Linux的出现&#x…

CSS | CSS中height:100vh和height:100%的区别

目录 1、对于设置height:100%;有下面几种情况 2、对于设置height:100vh时有如下的情况 首先&#xff0c;我们得知道1vh它表示的是当前屏幕可见高度的1/100&#xff0c;而1%它表示的是父元素长或者宽的1% 1、对于设置height:100%;有下面几种情况 &#xff08;1&#xff09;当…

Win10 hyper-v与vmware不兼容解决方案

Win10 hyper-v与vmware不兼容怎么办 一、异常1.1 异常描述 - V M w a r e W o r k s t a t i o n 与 H y p e r − V 不兼容 \color{red}{VMware Workstation 与 Hyper-V 不兼容} VMwareWorkstation与Hyper−V不兼容1.2 异常原因 二、解决办法2.1 关闭Hyper-V启动2.2 关闭内核…

OpenGL 光照贴图

1.简介 现实世界中的物体通常并不只包含有一种材质&#xff0c;而是由多种材质所组成。想想一辆汽车&#xff1a;它的外壳非常有光泽&#xff0c;车窗会部分反射周围的环境&#xff0c;轮胎不会那么有光泽&#xff0c;所以它没有镜面高光&#xff0c;轮毂非常闪亮。 2.漫反射…

pyspark安装教程

pyspark安装教程 一、Windows下配置pyspark环境1.1 JDK下载安装1.2 Scala下载安装1.3 spark下载安装1.4 Hadoop下载安装1.5 pyspark下载安装 二、pyspark原理简介 一、Windows下配置pyspark环境 在python中使用pyspark并不是单纯的导入pyspark包就可以实现的&#xff0c;而是需…

【SpringCloud入门】-- Nacos快速入门之搭建服务与注册中心

目录 前言&#xff1a; 1.Nacos的下载与安装 2. 去MySQL建立一个名为nacos的数据库 3.介绍配置文件&#xff0c;conf目录下的 application.properties 4.nacos启动 5. nacos作为注册中心的作用 6.建立一个项目&#xff0c;实现向命名空间注册 前言&#xff1a; 上文我们已…

基于人工智能的AI理发师能帮托尼老师做什么?

BarberGPT是一个人工智能理发师&#xff0c;它可以让您在照片上尝试不同的发型。您只需要上传您的照片&#xff0c;标记您的头发&#xff0c;然后就可以看到惊人的变化。BarberGPT使用了先进的深度学习技术&#xff0c;可以根据您的脸型、肤色和发质生成适合您的发型。BarberGP…

MySql常见问题(长期更新)

基于mysql 8.0.3版本 一、忘记root密码1.1 、linux 系统下忘记密码1.2、Windows 系统下忘记密码1.3 Unix 和类 Unix 系统 二、账号问题2.1 远程访问账号设置 一、忘记root密码 1.1 、linux 系统下忘记密码 啥&#xff1f;你问我为什么会忘记密码&#xff1f;别问&#xff0c;…

Spring Boot高阶篇笔记

一、Spring Boot整合Redis缓存 JSR-107、Spring缓存抽象、整合Redis 1、JSR107 Java Caching定义了5个核心接口&#xff0c;分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应…

Oracle 查询优化改写(第一章)

第一章 单表查询 1.查询空值 2.将空值转换为实际值 不采用nvl&#xff08;&#xff09;函数&#xff0c;而使用COALESCE函数语法为COALESCE(表达式1,表达式2,...,表达式n)&#xff0c;n>2,此表达式的功能为返回第一个不为空的表达式&#xff0c;如果都为空则返回空值。 注…

tp6安装并使用rabbitMQ

最近因为业务需要,要用到MQ就去研究了一下,说实话,安装环境给我搞自闭了,大概是我太菜 刚开始使用yum换源,各种安装卸载始终找不到自己要用的版本,后来全部卸载,下载安装包 编译安装解百忧 我用的是erlang 25.3 的版本,MQ使用的是3.11.3的版本,符合官方要求,这里的版本是有强…

TCP为什么要三次握手与四次分手?

概要 TCP协议是五层协议中运输层的协议&#xff0c;下面依赖网络层、链路层、物理层&#xff0c;对于一个报文想发到另一台机器(假设是服务器)上对等层&#xff0c;每一个所依赖的层都会对报文进行包装&#xff0c;例如TCP协议就依赖网络层的IP协议&#xff0c;所以发送的报文会…

实习记录(二)Java常用工具库

一.Lombok 1.背景概述 Lombok是一个非常高效的专用于Java的自动构建插件库&#xff0c;其简化了 JavaBean 的编写&#xff0c;避免了冗余和样板式代码的出现&#xff0c;让编写的类更加简洁明了&#xff0c;可以帮助大家节省很多重复低效的代码编写。比如重复性的Setter、Gett…