本文为365天深度学习训练营中的学习记录博客
原作者:K同学啊|接辅导、项目定制
请根据YOLOv8n、YOLOv8s模型的结构输出,手写出YOLOv8l的模型输出
文件位置:./ultralytics/cfg/models/v8/yolov8.yaml
一、参数配置
# Parameters nc: 80 # number of classes scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n' # [depth, width, max_channels] n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
Parameters:
- nc:80 是类别数量,即模型可以识别的物体类别数。
- scales:包含了不同模型配置的尺度参数,用于调整模型的规模,通过尺度参数可以实现不同复杂度的模型设计。YOLOv8n、YOLOv8s、YOLOv8m、YOLOv8l、YOLOv8x五种模型的区别在于depth、width、max_channels这三个参数的不同。
- depth: 深度,控制子模块的数量, = int(number*depth)
- width: 宽度,控制卷积核的数量, = int(number*width)
- max_channels: 最大通道数
五种模型性能的详细参数如下所示:
二、模型整体结构
YOLOv5模型:
YOLO-v8整体模型结构:
1. Backbone模块
# YOLOv8.0n backbone backbone: # [from, repeats, module, args] - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2 - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4 - [-1, 3, C2f, [128, True]] - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8 - [-1, 6, C2f, [256, True]] - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, SPPF, [1024, 5]] # 9
这是YOLOv8的backbone,每一个模块算一行,每行由四个参数构成。分别是:
from:表示当前模块的输入来自那一层的输出,-1表示来自上一层的输出,层编号由0开始计数。
repeats:表示当前模块的理论重复次数,实际的重复次数还要由上面的参数depth_multiple共同决定,该参数影响整体网络模型的深度。
model:模块类名,通过这个类名在common.py中寻找相应的类,进行模块化的搭建网络。
args:是一个list,模块搭建所需参数,channel,kernel_size,stride,padding,bias等。
这个模块是YOLOv8的主干网络(backbone),用于提取输入图像的特征以便后续的目标检测任务。
YOLOv8的主干网络包括卷积层(Conv)、深度可分离卷积层(C2f)以及空间金字塔池化层(SPPF)等卷积部分。它们在不同层数级上增强了网络的表示能力和视野范围,可以更好地适应各种尺寸的输入图像。
网络的输入为一幅图像,输出为多个不同层数级的特征图(feature maps),将输出的特征图传递给头部(head)以产生物体检测的结果。
2. head模块
# YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 6], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 12], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 9], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)
这是YOLOv8s的head,数据格式和backbone一样。
3. 模型结构输出
cmd命令行输入:
yolo task=detect mode=train model=yolov8n.yaml data=mydata.yaml epochs=100 batch=4
yolov8n.yaml可以换成其他模型的yaml,如yolov8s.yaml、yolov8l.yaml、yolov8m.yaml、yolov8x.yaml
(1)yolov8n.yaml模型:
from n params module arguments 0 -1 1 464 ultralytics.nn.modules.conv.Conv [3, 16, 3, 2] 1 -1 1 4672 ultralytics.nn.modules.conv.Conv [16, 32, 3, 2] 2 -1 1 7360 ultralytics.nn.modules.block.C2f [32, 32, 1, True] 3 -1 1 18560 ultralytics.nn.modules.conv.Conv [32, 64, 3, 2] 4 -1 2 49664 ultralytics.nn.modules.block.C2f [64, 64, 2, True] 5 -1 1 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2] 6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True] 7 -1 1 295424 ultralytics.nn.modules.conv.Conv [128, 256, 3, 2] 8 -1 1 460288 ultralytics.nn.modules.block.C2f [256, 256, 1, True] 9 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 11 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 12 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1] 13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 14 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 15 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1] 16 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2] 17 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 18 -1 1 123648 ultralytics.nn.modules.block.C2f [192, 128, 1] 19 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2] 20 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 21 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1] 22 [15, 18, 21] 1 752092 ultralytics.nn.modules.head.Detect [4, [64, 128, 256]] YOLOv8n summary: 225 layers, 3,011,628 parameters, 3,011,612 gradients, 8.2 GFLOPs
(2)yolov8s.yaml模型:
from n params module arguments 0 -1 1 928 ultralytics.nn.modules.conv.Conv [3, 32, 3, 2] 1 -1 1 18560 ultralytics.nn.modules.conv.Conv [32, 64, 3, 2] 2 -1 1 29056 ultralytics.nn.modules.block.C2f [64, 64, 1, True] 3 -1 1 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2] 4 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True] 5 -1 1 295424 ultralytics.nn.modules.conv.Conv [128, 256, 3, 2] 6 -1 2 788480 ultralytics.nn.modules.block.C2f [256, 256, 2, True] 7 -1 1 1180672 ultralytics.nn.modules.conv.Conv [256, 512, 3, 2] 8 -1 1 1838080 ultralytics.nn.modules.block.C2f [512, 512, 1, True] 9 -1 1 656896 ultralytics.nn.modules.block.SPPF [512, 512, 5] 10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 11 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 12 -1 1 591360 ultralytics.nn.modules.block.C2f [768, 256, 1] 13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 14 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 15 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1] 16 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2] 17 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 18 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1] 19 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2] 20 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 21 -1 1 1969152 ultralytics.nn.modules.block.C2f [768, 512, 1] 22 [15, 18, 21] 1 2117596 ultralytics.nn.modules.head.Detect [4, [128, 256, 512]] YOLOv8s summary: 225 layers, 11,137,148 parameters, 11,137,132 gradients, 28.7 GFLOPs
(3)yolov8l.yaml模型:
from n params module arguments 0 -1 1 1856 ultralytics.nn.modules.conv.Conv [3, 64, 3, 2] 1 -1 1 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2] 2 -1 3 279808 ultralytics.nn.modules.block.C2f [128, 128, 3, True] 3 -1 1 295424 ultralytics.nn.modules.conv.Conv [128, 256, 3, 2] 4 -1 6 2101248 ultralytics.nn.modules.block.C2f [256, 256, 6, True] 5 -1 1 1180672 ultralytics.nn.modules.conv.Conv [256, 512, 3, 2] 6 -1 6 8396800 ultralytics.nn.modules.block.C2f [512, 512, 6, True] 7 -1 1 2360320 ultralytics.nn.modules.conv.Conv [512, 512, 3, 2] 8 -1 3 4461568 ultralytics.nn.modules.block.C2f [512, 512, 3, True] 9 -1 1 656896 ultralytics.nn.modules.block.SPPF [512, 512, 5] 10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 11 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 12 -1 3 4723712 ultralytics.nn.modules.block.C2f [1024, 512, 3] 13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 14 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 15 -1 3 1247744 ultralytics.nn.modules.block.C2f [768, 256, 3] 16 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2] 17 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 18 -1 3 4592640 ultralytics.nn.modules.block.C2f [768, 512, 3] 19 -1 1 2360320 ultralytics.nn.modules.conv.Conv [512, 512, 3, 2] 20 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 21 -1 3 4723712 ultralytics.nn.modules.block.C2f [1024, 512, 3] 22 [15, 18, 21] 1 5585884 ultralytics.nn.modules.head.Detect [4, [256, 512, 512]] YOLOv8l summary: 365 layers, 43,632,924 parameters, 43,632,908 gradients, 165.4 GFLOPs
三、总结
了解了YOLOv8的各种模型结构,了解了YOLOv8各种模型的参数配置。