学习资源站

YOLOv11改进-主干_Backbone篇-华为最新目标检测网络VanillaNet(适配yolov11的N,S,M,L,X)

一、本文介绍

本文给大家来的改进机制是 华为最新VanillaNet网络 VanillaNet 是一种注重极简主义和效率的 神经网络 架构。它的设计简单,层数较少,避免了像深度架构和 自注意力 这样的复杂操作 (需要注意的是该网络结构的通道数会被放大,GFLOPs的量会很高) 。我将其替换整个YOLOv10的Backbone,在一些大目标和小目标检测上均有涨点,效果比上一篇RepViT的效果要好。我的实验数据集是一个包含1000张图片包含大中小的检测目标的数据集上(共有20+类别),下面我会附上基础版本和修改版本的训练对比图,同时我会手把手教你添加该网络结构。

(本文内容可根据yolov11的N、S、M、L、X进行二次缩放,轻量化更上一层)。



二、VanillaNet原理

论文地址: 官方论文地址

代码地址: 官方代码地址


2.2  VanillaNet的基本原理

VanillaNet 是一种注重极简主义和效率的神经 网络架构 。它的设计简单,层数较少,避免了像深度架构和自注意力这样的复杂操作。VanillaNet的关键特性包括 深度训练策略 ,最初使用激活函数训练两个卷积层,随后这个激活函数逐渐简化为恒等映射,允许层合并。此外,VanillaNet还使用 并行堆叠的激活函数 来提高非线性,从而提升简单网络的 性能

VanillaNet的原理包括以下几个关键点:

1. 深度训练策略: 初始阶段采用两个卷积层和一个激活函数进行训练,随着训练进程,激活函数逐渐转化为恒等映射,允许这些层合并,从而减少推断时间。

2. 串联激活函数: VanillaNet引入了 并行 堆叠激活函数来增强非线性,这对于简单网络的性能至关重要。

下面为大家展示了 VanillaNet-6模型的架构

它仅由6个卷积层构成。输入特征的尺寸会在每个阶段降低,而 通道数 则会翻倍,这一设计借鉴了如AlexNet和VGGNet这类经典神经网络的设计理念。通过这种方式,VanillaNet-6模型能够以较低的计算成本处理图像数据,同时通过增加通道数来保持足够的特征表达能力。


2.2.1 深度训练策略

深度训练策略 是指在 神经网络训练初期 采用 比标准配置更多的层和更复杂的结构 这通过增加模型的非线性和表示能力来提高训练效果。(这是一种现在比较流行的网络设计方式,在训练的时候使用复杂的结构,推理的时候使用简单的结构)。训练的后期,这些额外层会通过技术手段合并或简化,以减少模型的复杂性和提高推理时的效率。

这种策略允许 在初期利用深层结构的强大能力 ,在不牺牲推理速度的前提下,随着训练的进行,逐步优化模型结构,最终达到一个既能保持良好性能又能高效运行的平衡点。这样做的目的是在保持推理效率的同时,利用 深度结构 在训练过程中提供的额外能力。


2.2.2 串联激活函数

串联激活函数 是指在一个神经网络的相同层或连续层中使用 多个激活函数 ,这样可以增强网络处理非线性问题的能力。传统的神经网络可能只在每个卷积层后使用一个激活函数,而串联激活函数的做法是 将多个激活函数按序排列 ,每个函数的输出成为下一个函数的输入。这种串联可以创建更复杂的函数映射,从而允许模型捕捉到更丰富的数据表示和特征。


三、VanillaNet的核心代码

下面的代码是整个VanillaNet的核心代码,其中有个版本,对应的GFLOPs也不相同,使用方式看章节四。

  1. # Copyright (C) 2023. Huawei Technologies Co., Ltd. All rights reserved.
  2. # This program is free software; you can redistribute it and/or modify it under the terms of the MIT License.
  3. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT License for more details.
  4. import torch
  5. import torch.nn as nn
  6. from timm.layers import weight_init
  7. __all__ = ['vanillanet_5', 'vanillanet_6', 'vanillanet_7', 'vanillanet_8', 'vanillanet_9', 'vanillanet_10',
  8. 'vanillanet_11', 'vanillanet_12', 'vanillanet_13', 'vanillanet_13_x1_5', 'vanillanet_13_x1_5_ada_pool']
  9. class activation(nn.ReLU):
  10. def __init__(self, dim, act_num=3, deploy=False):
  11. super(activation, self).__init__()
  12. self.deploy = deploy
  13. self.weight = torch.nn.Parameter(torch.randn(dim, 1, act_num * 2 + 1, act_num * 2 + 1))
  14. self.bias = None
  15. self.bn = nn.BatchNorm2d(dim, eps=1e-6)
  16. self.dim = dim
  17. self.act_num = act_num
  18. weight_init.trunc_normal_(self.weight, std=.02)
  19. def forward(self, x):
  20. if self.deploy:
  21. return torch.nn.functional.conv2d(
  22. super(activation, self).forward(x),
  23. self.weight, self.bias, padding=(self.act_num * 2 + 1) // 2, groups=self.dim)
  24. else:
  25. return self.bn(torch.nn.functional.conv2d(
  26. super(activation, self).forward(x),
  27. self.weight, padding=self.act_num, groups=self.dim))
  28. def _fuse_bn_tensor(self, weight, bn):
  29. kernel = weight
  30. running_mean = bn.running_mean
  31. running_var = bn.running_var
  32. gamma = bn.weight
  33. beta = bn.bias
  34. eps = bn.eps
  35. std = (running_var + eps).sqrt()
  36. t = (gamma / std).reshape(-1, 1, 1, 1)
  37. return kernel * t, beta + (0 - running_mean) * gamma / std
  38. def switch_to_deploy(self):
  39. if not self.deploy:
  40. kernel, bias = self._fuse_bn_tensor(self.weight, self.bn)
  41. self.weight.data = kernel
  42. self.bias = torch.nn.Parameter(torch.zeros(self.dim))
  43. self.bias.data = bias
  44. self.__delattr__('bn')
  45. self.deploy = True
  46. class Block(nn.Module):
  47. def __init__(self, dim, dim_out, act_num=3, stride=2, deploy=False, ada_pool=None):
  48. super().__init__()
  49. self.act_learn = 1
  50. self.deploy = deploy
  51. if self.deploy:
  52. self.conv = nn.Conv2d(dim, dim_out, kernel_size=1)
  53. else:
  54. self.conv1 = nn.Sequential(
  55. nn.Conv2d(dim, dim, kernel_size=1),
  56. nn.BatchNorm2d(dim, eps=1e-6),
  57. )
  58. self.conv2 = nn.Sequential(
  59. nn.Conv2d(dim, dim_out, kernel_size=1),
  60. nn.BatchNorm2d(dim_out, eps=1e-6)
  61. )
  62. if not ada_pool:
  63. self.pool = nn.Identity() if stride == 1 else nn.MaxPool2d(stride)
  64. else:
  65. self.pool = nn.Identity() if stride == 1 else nn.AdaptiveMaxPool2d((ada_pool, ada_pool))
  66. self.act = activation(dim_out, act_num)
  67. def forward(self, x):
  68. if self.deploy:
  69. x = self.conv(x)
  70. else:
  71. x = self.conv1(x)
  72. x = torch.nn.functional.leaky_relu(x, self.act_learn)
  73. x = self.conv2(x)
  74. x = self.pool(x)
  75. x = self.act(x)
  76. return x
  77. def _fuse_bn_tensor(self, conv, bn):
  78. kernel = conv.weight
  79. bias = conv.bias
  80. running_mean = bn.running_mean
  81. running_var = bn.running_var
  82. gamma = bn.weight
  83. beta = bn.bias
  84. eps = bn.eps
  85. std = (running_var + eps).sqrt()
  86. t = (gamma / std).reshape(-1, 1, 1, 1)
  87. return kernel * t, beta + (bias - running_mean) * gamma / std
  88. def switch_to_deploy(self):
  89. if not self.deploy:
  90. kernel, bias = self._fuse_bn_tensor(self.conv1[0], self.conv1[1])
  91. self.conv1[0].weight.data = kernel
  92. self.conv1[0].bias.data = bias
  93. # kernel, bias = self.conv2[0].weight.data, self.conv2[0].bias.data
  94. kernel, bias = self._fuse_bn_tensor(self.conv2[0], self.conv2[1])
  95. self.conv = self.conv2[0]
  96. self.conv.weight.data = torch.matmul(kernel.transpose(1, 3),
  97. self.conv1[0].weight.data.squeeze(3).squeeze(2)).transpose(1, 3)
  98. self.conv.bias.data = bias + (self.conv1[0].bias.data.view(1, -1, 1, 1) * kernel).sum(3).sum(2).sum(1)
  99. self.__delattr__('conv1')
  100. self.__delattr__('conv2')
  101. self.act.switch_to_deploy()
  102. self.deploy = True
  103. class VanillaNet(nn.Module):
  104. def __init__(self, in_chans=3, num_classes=1000, dims=[96, 192, 384, 768],
  105. drop_rate=0, act_num=3, strides=[2, 2, 2, 1], deploy=False, ada_pool=None, **kwargs):
  106. super().__init__()
  107. self.deploy = deploy
  108. if self.deploy:
  109. self.stem = nn.Sequential(
  110. nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4),
  111. activation(dims[0], act_num)
  112. )
  113. else:
  114. self.stem1 = nn.Sequential(
  115. nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4),
  116. nn.BatchNorm2d(dims[0], eps=1e-6),
  117. )
  118. self.stem2 = nn.Sequential(
  119. nn.Conv2d(dims[0], dims[0], kernel_size=1, stride=1),
  120. nn.BatchNorm2d(dims[0], eps=1e-6),
  121. activation(dims[0], act_num)
  122. )
  123. self.act_learn = 1
  124. self.stages = nn.ModuleList()
  125. for i in range(len(strides)):
  126. if not ada_pool:
  127. stage = Block(dim=dims[i], dim_out=dims[i + 1], act_num=act_num, stride=strides[i], deploy=deploy)
  128. else:
  129. stage = Block(dim=dims[i], dim_out=dims[i + 1], act_num=act_num, stride=strides[i], deploy=deploy,
  130. ada_pool=ada_pool[i])
  131. self.stages.append(stage)
  132. self.depth = len(strides)
  133. self.apply(self._init_weights)
  134. self.width_list = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))]
  135. def _init_weights(self, m):
  136. if isinstance(m, (nn.Conv2d, nn.Linear)):
  137. weight_init.trunc_normal_(m.weight, std=.02)
  138. nn.init.constant_(m.bias, 0)
  139. def change_act(self, m):
  140. for i in range(self.depth):
  141. self.stages[i].act_learn = m
  142. self.act_learn = m
  143. def forward(self, x):
  144. unique_tensors = {}
  145. if self.deploy:
  146. x = self.stem(x)
  147. else:
  148. x = self.stem1(x)
  149. x = torch.nn.functional.leaky_relu(x, self.act_learn)
  150. x = self.stem2(x)
  151. width, height = x.shape[2], x.shape[3]
  152. unique_tensors[(width, height)] = x
  153. for i in range(self.depth):
  154. x = self.stages[i](x)
  155. width, height = x.shape[2], x.shape[3]
  156. unique_tensors[(width, height)] = x
  157. result_list = list(unique_tensors.values())[-4:]
  158. return result_list
  159. def _fuse_bn_tensor(self, conv, bn):
  160. kernel = conv.weight
  161. bias = conv.bias
  162. running_mean = bn.running_mean
  163. running_var = bn.running_var
  164. gamma = bn.weight
  165. beta = bn.bias
  166. eps = bn.eps
  167. std = (running_var + eps).sqrt()
  168. t = (gamma / std).reshape(-1, 1, 1, 1)
  169. return kernel * t, beta + (bias - running_mean) * gamma / std
  170. def switch_to_deploy(self):
  171. if not self.deploy:
  172. self.stem2[2].switch_to_deploy()
  173. kernel, bias = self._fuse_bn_tensor(self.stem1[0], self.stem1[1])
  174. self.stem1[0].weight.data = kernel
  175. self.stem1[0].bias.data = bias
  176. kernel, bias = self._fuse_bn_tensor(self.stem2[0], self.stem2[1])
  177. self.stem1[0].weight.data = torch.einsum('oi,icjk->ocjk', kernel.squeeze(3).squeeze(2),
  178. self.stem1[0].weight.data)
  179. self.stem1[0].bias.data = bias + (self.stem1[0].bias.data.view(1, -1, 1, 1) * kernel).sum(3).sum(2).sum(1)
  180. self.stem = torch.nn.Sequential(*[self.stem1[0], self.stem2[2]])
  181. self.__delattr__('stem1')
  182. self.__delattr__('stem2')
  183. for i in range(self.depth):
  184. self.stages[i].switch_to_deploy()
  185. self.deploy = True
  186. def vanillanet_5(pretrained=False, in_22k=False, **kwargs):
  187. model = VanillaNet(dims=[128 * 4, 256 * 4, 512 * 4, 1024 * 4], strides=[2, 2, 2], **kwargs)
  188. return model
  189. def vanillanet_6(pretrained=False, in_22k=False, **kwargs):
  190. model = VanillaNet(dims=[128 * 4, 256 * 4, 512 * 4, 1024 * 4, 1024 * 4], strides=[2, 2, 2, 1], **kwargs)
  191. return model
  192. def vanillanet_7(pretrained=False, in_22k=False, **kwargs):
  193. model = VanillaNet(dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 1024 * 4, 1024 * 4], strides=[1, 2, 2, 2, 1], **kwargs)
  194. return model
  195. def vanillanet_8(pretrained=False, in_22k=False, **kwargs):
  196. model = VanillaNet(dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 1024 * 4, 1024 * 4],
  197. strides=[1, 2, 2, 1, 2, 1], **kwargs)
  198. return model
  199. def vanillanet_9(pretrained=False, in_22k=False, **kwargs):
  200. model = VanillaNet(dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 512 * 4, 1024 * 4, 1024 * 4],
  201. strides=[1, 2, 2, 1, 1, 2, 1], **kwargs)
  202. return model
  203. def vanillanet_10(pretrained=False, in_22k=False, **kwargs):
  204. model = VanillaNet(
  205. dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 1024 * 4, 1024 * 4],
  206. strides=[1, 2, 2, 1, 1, 1, 2, 1],
  207. **kwargs)
  208. return model
  209. def vanillanet_11(pretrained=False, in_22k=False, **kwargs):
  210. model = VanillaNet(
  211. dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 1024 * 4, 1024 * 4],
  212. strides=[1, 2, 2, 1, 1, 1, 1, 2, 1],
  213. **kwargs)
  214. return model
  215. def vanillanet_12(pretrained=False, in_22k=False, **kwargs):
  216. model = VanillaNet(
  217. dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 1024 * 4, 1024 * 4],
  218. strides=[1, 2, 2, 1, 1, 1, 1, 1, 2, 1],
  219. **kwargs)
  220. return model
  221. def vanillanet_13(pretrained=False, in_22k=False, **kwargs):
  222. model = VanillaNet(
  223. dims=[128 * 4, 128 * 4, 256 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 512 * 4, 1024 * 4,
  224. 1024 * 4],
  225. strides=[1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1],
  226. **kwargs)
  227. return model
  228. def vanillanet_13_x1_5(pretrained=False, in_22k=False, **kwargs):
  229. model = VanillaNet(
  230. dims=[128 * 6, 128 * 6, 256 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 1024 * 6,
  231. 1024 * 6],
  232. strides=[1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1],
  233. **kwargs)
  234. return model
  235. def vanillanet_13_x1_5_ada_pool(pretrained=False, in_22k=False, **kwargs):
  236. model = VanillaNet(
  237. dims=[128 * 6, 128 * 6, 256 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 512 * 6, 1024 * 6,
  238. 1024 * 6],
  239. strides=[1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1],
  240. ada_pool=[0, 38, 19, 0, 0, 0, 0, 0, 0, 10, 0],
  241. **kwargs)
  242. return model


四、手把手教你添加VanillaNet网络结构

4.1 修改一

第一步还是建立文件,我们找到如下 ultralytics /nn文件夹下建立一个目录名字呢就是'Addmodules'文件夹( 用群内的文件的话已经有了无需新建) !然后在其内部建立一个新的py文件将核心代码复制粘贴进去即可


4.2 修改二

第二步我们在该目录下创建一个新的py文件名字为'__init__.py'( 用群内的文件的话已经有了无需新建) ,然后在其内部导入我们的检测头如下图所示。


4.3 修改三

第三步我门中到如下文件'ultralytics/nn/tasks.py'进行导入和注册我们的模块( 用群内的文件的话已经有了无需重新导入直接开始第四步即可)

从今天开始以后的教程就都统一成这个样子了,因为我默认大家用了我群内的文件来进行修改!!


4.4 修改四

添加如下两行代码!!!


4.5 修改五

找到七百多行大概把具体看图片,按照图片来修改就行,添加红框内的部分,注意没有()只是函数名。

  1. elif m in {vanillanet_5, vanillanet_6, vanillanet_7, vanillanet_8, vanillanet_9, vanillanet_10, vanillanet_11, vanillanet_12, vanillanet_13, vanillanet_13_x1_5, vanillanet_13_x1_5_ada_pool}:
  2. m = m(*args)
  3. c2 = m.width_list # 返回通道列表
  4. backbone = True


4.6 修改六

下面的两个红框内都是需要改动的。

  1. if isinstance(c2, list):
  2. m_ = m
  3. m_.backbone = True
  4. else:
  5. m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
  6. t = str(m)[8:-2].replace('__main__.', '') # module type
  7. m.np = sum(x.numel() for x in m_.parameters()) # number params
  8. m_.i, m_.f, m_.type = i + 4 if backbone else i, f, t # attach index, 'from' index, type


4.7 修改七

如下的也需要修改,全部按照我的来。

代码如下把原先的代码替换了即可。

  1. if verbose:
  2. LOGGER.info(f'{i:>3}{str(f):>20}{n_:>3}{m.np:10.0f} {t:<45}{str(args):<30}') # print
  3. save.extend(x % (i + 4 if backbone else i) for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
  4. layers.append(m_)
  5. if i == 0:
  6. ch = []
  7. if isinstance(c2, list):
  8. ch.extend(c2)
  9. if len(c2) != 5:
  10. ch.insert(0, 0)
  11. else:
  12. ch.append(c2)


4.8 修改八

修改八和前面的都不太一样,需要修改前向传播中的一个部分, 已经离开了parse_model方法了。

可以在图片中开代码行数,没有离开task.py文件都是同一个文件。 同时这个部分有好几个前向传播都很相似,大家不要看错了, 是70多行左右的!!!,同时我后面提供了代码,大家直接复制粘贴即可,有时间我针对这里会出一个视频。

​​

代码如下->

  1. def _predict_once(self, x, profile=False, visualize=False, embed=None):
  2. """
  3. Perform a forward pass through the network.
  4. Args:
  5. x (torch.Tensor): The input tensor to the model.
  6. profile (bool): Print the computation time of each layer if True, defaults to False.
  7. visualize (bool): Save the feature maps of the model if True, defaults to False.
  8. embed (list, optional): A list of feature vectors/embeddings to return.
  9. Returns:
  10. (torch.Tensor): The last output of the model.
  11. """
  12. y, dt, embeddings = [], [], [] # outputs
  13. for m in self.model:
  14. if m.f != -1: # if not from previous layer
  15. x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
  16. if profile:
  17. self._profile_one_layer(m, x, dt)
  18. if hasattr(m, 'backbone'):
  19. x = m(x)
  20. if len(x) != 5: # 0 - 5
  21. x.insert(0, None)
  22. for index, i in enumerate(x):
  23. if index in self.save:
  24. y.append(i)
  25. else:
  26. y.append(None)
  27. x = x[-1] # 最后一个输出传给下一层
  28. else:
  29. x = m(x) # run
  30. y.append(x if m.i in self.save else None) # save output
  31. if visualize:
  32. feature_visualization(x, m.type, m.i, save_dir=visualize)
  33. if embed and m.i in embed:
  34. embeddings.append(nn.functional.adaptive_avg_pool2d(x, (1, 1)).squeeze(-1).squeeze(-1)) # flatten
  35. if m.i == max(embed):
  36. return torch.unbind(torch.cat(embeddings, 1), dim=0)
  37. return x

到这里就完成了修改部分,但是这里面细节很多,大家千万要注意不要替换多余的代码,导致报错,也不要拉下任何一部,都会导致运行失败,而且报错很难排查!!!很难排查!!!


注意!!! 额外的修改!

关注我的其实都知道,我大部分的修改都是一样的,这个网络需要额外的修改一步,就是s一个参数,将下面的s改为640!!!即可完美运行!!


打印计算量问题解决方案

我们找到如下文件'ultralytics/utils/torch_utils.py'按照如下的图片进行修改,否则容易打印不出来计算量。


注意事项!!!

如果大家在验证的时候报错形状不匹配的错误可以固定验证集的图片尺寸,方法如下 ->

找到下面这个文件ultralytics/ models /yolo/detect/train.py然后其中有一个类是DetectionTrainer class中的build_dataset函数中的一个参数rect=mode == 'val'改为rect=False


五、VanillaNet的yaml文件

5.1 VanillaNet的yaml文件

训练信息:YOLO11-vanillanet summary: 248 layers, 3,743,999 parameters, 3,743,983 gradients, 13.6 GFLOP

使用说明:# 下面 [-1, 1, vanillanet_5, [0.25]] 参数位置的0.25是通道放缩的系数, YOLOv11N是0.25 YOLOv11S是0.5 YOLOv11M是1. YOLOv11l是1 YOLOv11是1.5大家根据自己训练的YOLO版本设定即可.

# 本文支持版本有 vanillanet_5, vanillanet_6, vanillanet_7, vanillanet_8, vanillanet_9, vanillanet_10, vanillanet_11, vanillanet_12, vanillanet_13, vanillanet_13_x1_5, vanillanet_13_x1_5_ada_pool

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  8. s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  9. m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  10. l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  11. x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
  12. # 下面 [-1, 1, vanillanet_5, [0.25]] 参数位置的0.25是通道放缩的系数, YOLOv11N是0.25 YOLOv11S是0.5 YOLOv11M是1. YOLOv11l是1 YOLOv111.5大家根据自己训练的YOLO版本设定即可.
  13. # 本文支持版本有 vanillanet_5, vanillanet_6, vanillanet_7, vanillanet_8, vanillanet_9, vanillanet_10, vanillanet_11, vanillanet_12, vanillanet_13, vanillanet_13_x1_5, vanillanet_13_x1_5_ada_pool
  14. # YOLO11n backbone
  15. backbone:
  16. # [from, repeats, module, args]
  17. - [-1, 1, vanillanet_5, [0.25]] # 0-4 P1/2 这里是四层大家不要被yaml文件限制住了思维,不会画图进群看视频.
  18. - [-1, 1, SPPF, [1024, 5]] # 5
  19. - [-1, 2, C2PSA, [1024]] # 6
  20. # YOLO11n head
  21. head:
  22. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  23. - [[-1, 3], 1, Concat, [1]] # cat backbone P4
  24. - [-1, 2, C3k2, [512, False]] # 9
  25. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  26. - [[-1, 2], 1, Concat, [1]] # cat backbone P3
  27. - [-1, 2, C3k2, [256, False]] # 12 (P3/8-small)
  28. - [-1, 1, Conv, [256, 3, 2]]
  29. - [[-1, 9], 1, Concat, [1]] # cat head P4
  30. - [-1, 2, C3k2, [512, False]] # 15 (P4/16-medium)
  31. - [-1, 1, Conv, [512, 3, 2]]
  32. - [[-1, 6], 1, Concat, [1]] # cat head P5
  33. - [-1, 2, C3k2, [1024, True]] # 18 (P5/32-large)
  34. - [[12, 15, 18], 1, Detect, [nc]] # Detect(P3, P4, P5)


5.2 训练文件的代码

可以复制我的运行文件进行运行。

  1. import warnings
  2. warnings.filterwarnings('ignore')
  3. from ultralytics import YOLO
  4. if __name__ == '__main__':
  5. model = YOLO('yolov8-MLLA.yaml')
  6. # 如何切换模型版本, 上面的ymal文件可以改为 yolov8s.yaml就是使用的v8s,
  7. # 类似某个改进的yaml文件名称为yolov8-XXX.yaml那么如果想使用其它版本就把上面的名称改为yolov8l-XXX.yaml即可(改的是上面YOLO中间的名字不是配置文件的)!
  8. # model.load('yolov8n.pt') # 是否加载预训练权重,科研不建议大家加载否则很难提升精度
  9. model.train(data=r"C:\Users\Administrator\PycharmProjects\yolov5-master\yolov5-master\Construction Site Safety.v30-raw-images_latestversion.yolov8\data.yaml",
  10. # 如果大家任务是其它的'ultralytics/cfg/default.yaml'找到这里修改task可以改成detect, segment, classify, pose
  11. cache=False,
  12. imgsz=640,
  13. epochs=150,
  14. single_cls=False, # 是否是单类别检测
  15. batch=16,
  16. close_mosaic=0,
  17. workers=0,
  18. device='0',
  19. optimizer='SGD', # using SGD
  20. # resume='runs/train/exp21/weights/last.pt', # 如过想续训就设置last.pt的地址
  21. amp=True, # 如果出现训练损失为Nan可以关闭amp
  22. project='runs/train',
  23. name='exp',
  24. )


六、成功运行记录

下面是成功运行的截图,已经完成了有1个epochs的训练,图片太大截不全第2个epochs了。


七、本文总结

到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充 如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~