Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

【自取】最近整理的,有需要可以领取学习:

在我的本科论文中,我使用了Yolov5,并试图改变它。您可以对Yolov5进行一些定制更改,例如更轻的Yolov5-MobileNetv3或比Yolov5s更好的Yolov5-EfficientNet(不确定的是,您可以使用大型数据集进行实验)。

[En]

In my undergraduate thesis, I used Yolov5 and tried to change it. You can make some customized changes to Yolov5, such as a lighter Yolov5-MobileNetv3 or a better Yolov5-EfficientNet than Yolov5s (in doubt, you can experiment with a large dataset).

首先,在修改之前,先看看Yolov5的网络结构。整个情况看起来很复杂,但不必惊慌。本文的主要修改是Backbone(特征提取网络),可以抽象为三个部分,即只需要修改一个地方。

[En]

First of all, before modifying, take a look at the network structure of Yolov5. The whole looks complicated, but there is no need to panic. The main modification of this article, Backbone (feature extraction Network), can be abstracted into only three parts, that is, only one place needs to be modified.

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
然后了解我们需要修改的代码。需要修改的代码主要集中在yolov5的Model文件夹中。YAML主要是修改代码后对应的配置文件。新的模块被添加到Common.py,而yolo.py允许模型支持读取相应的配置文件。
[En]

Then learn about the code we need to modify. The code that needs to be modified is mainly concentrated in the model folder of yolov5. Yaml is mainly the corresponding configuration file after modifying the code. New modules are added to common.py, while yolo.py allows the model to support reading the corresponding configuration files.

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
前情提要:已完成。第一步是选择一些性能较好的特征提取网络,如MobileNet、EfficientNet等。事实上,大多数较好的特征提取网络在经过三次或三次以上的下采样后可以得到三种不同大小的特征图像。在Yolov5中,将对三种大小的特征图像进行融合,FPN和APN的操作在这里不做详细介绍。主要需要注意的是,特征提取网络需要提取三种不同大小的特征图像。我们选择特征提取网络的最后三个下采样的输出到Yolov5网络,完成对特征提取网络的修改。
[En]

Previously on finished. The first step is to select some feature extraction networks with good performance, such as MobileNet, EfficientNet and so on. In fact, most of the better feature extraction networks can get three different sizes of feature images after three or more downsampling. In Yolov5, the three sizes of feature images will be fused, and the operations of FPN and APN will not be carried out in detail here. The main thing to note is that the feature extraction network needs to extract three different sizes of feature images. We select the output of the last three downsamples of the feature extraction network to the Yolov5 network to complete the modification of the feature extraction network.

以MobileNetv3-Small为例(我们甚至不需要自己搭建网络,直接挪用pytorch官方网络,以下网络任君选择)pytorch官网

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
输出网络结构,观察网络。mobilenetv3中主要分为features、avgpool、classify三部分组成,作用分别为特征提取、全局池化、分类器。我们只需要关注特征提取部分,并且着重关注于最后三次降采样部分,所以我们从最后开始往前进行观察。
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
MobileNet中的倒数第一次下采样发生在第九个模块。(如何快速看到降采样,简单来讲就是stride为2的地方。当然实际还有kernel_size等于5或者其他情况,但是一般比较新的网络kernel_size为5伴随的还有2的padding,所以偷懒可以只看stride) 因此9-11对应YOLOv5倒数第一次降采样。
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
倒数第二次下采样4-8
[En]

The penultimate downsampling 4-8

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
倒数第二次下采样0-3
[En]

The penultimate downsampling 0-3

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
在确定网络提取方法后,第二步是最后在Common.py中添加模块。如您所见,添加MobileNet的三个部分非常简单。
[En]

After determining the network extraction method, the second step is to add the module in common.py finally. As you can see, it’s very simple to add three parts of MobileNet.

from torchvision import models

class MobileNet1(nn.Module):

    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.mobilenet_v3_small(pretrained=True)
        modules = list(model.children())
        modules = modules[0][:4]
        self.model = nn.Sequential(*modules)

    def forward(self, x):
        return self.model(x)

class MobileNet2(nn.Module):

    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.mobilenet_v3_small(pretrained=True)
        modules = list(model.children())
        modules = modules[0][4:9]
        self.model = nn.Sequential(*modules)

    def forward(self, x):
        return self.model(x)

class MobileNet3(nn.Module):

    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.mobilenet_v3_small(pretrained=True)
        modules = list(model.children())
        modules = modules[0][9:]
        self.model = nn.Sequential(*modules)
    def forward(self, x):
        return self.model(x)

第三步,修改yolo.py 在这部分添加这行代码,意思是在解析yaml时放入相应的模块。arg[0]表示yaml模块后跟着的第一个参数,这个参数要告诉模型,此模块输出的通道数。可以回到上面看一看,三个模块的输出通道数为24、48、576。

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
最后,我添加了模型的YAML,并选择yolov5n作为原型进行修改。
[En]

Finally, I added the yaml of the model, and I chose yolov5n as the prototype to modify it.

yolov5n


nc: 80
depth_multiple: 0.33
width_multiple: 0.25
anchors:
  - [10,13, 16,30, 33,23]
  - [30,61, 62,45, 59,119]
  - [116,90, 156,198, 373,326]

backbone:

  [[-1, 1, Conv, [64, 6, 2, 2]],
   [-1, 1, Conv, [128, 3, 2]],
   [-1, 3, C3, [128]],
   [-1, 1, Conv, [256, 3, 2]],
   [-1, 6, C3, [256]],
   [-1, 1, Conv, [512, 3, 2]],
   [-1, 9, C3, [512]],
   [-1, 1, Conv, [1024, 3, 2]],
   [-1, 3, C3, [1024]],
   [-1, 1, SPPF, [1024, 5]],
  ]

head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],
   [-1, 3, C3, [512, False]],

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],
   [-1, 3, C3, [256, False]],

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 14], 1, Concat, [1]],
   [-1, 3, C3, [512, False]],

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 10], 1, Concat, [1]],
   [-1, 3, C3, [1024, False]],

   [[17, 20, 23], 1, Detect, [nc, anchors]],
  ]

yolov5n-mobilenet


nc: 80
depth_multiple: 0.33
width_multiple: 0.25
anchors:
  - [10,13, 16,30, 33,23]
  - [30,61, 62,45, 59,119]
  - [116,90, 156,198, 373,326]

backbone:

  [[-1, 1, MobileNet1, [24]],
   [-1, 1, MobileNet2, [48]],
   [-1, 1, MobileNet3, [576]],
   [-1, 1, SPPF, [1024, 5]],
  ]

head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 1], 1, Concat, [1]],
   [-1, 3, C3, [512, False]],

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 0], 1, Concat, [1]],
   [-1, 3, C3, [256, False]],

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 7], 1, Concat, [1]],
   [-1, 3, C3, [512, False]],

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 3], 1, Concat, [1]],
   [-1, 3, C3, [1024, False]],

   [[11, 14, 17], 1, Detect, [nc, anchors]],
  ]

如果修改一下,很容易理解,yolov5n的背面可以根据序列号#来计数,而Conat的背面是更低的采样层,按照葫芦画猫,并将序列号改为我们的模块。

[En]

If you modify it, it is easy to understand that the back of yolov5n can be counted according to the serial number of #, while that of concat is the lower sampling layer, draw the cat according to the gourd, and change the serial number to our module.

最后,使用-cfg调用。

[En]

Finally, use the-cfg call.

python train.py --cfg yolov5n-mobileNet.yaml --weight yolov5n.pt

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

简单地说,Yolov5-MobileNetv3的性能,GFLOPS是指在计算量大大减少的同时,精度与没有预训练网络的Yolov5n相近。但是,在GPU环境下的计算速度并没有提高,主要是由于SE模块的特点,不再详述,更适合于CPU移动平台。

[En]

To briefly talk about the performance of Yolov5-MobileNetv3, GFLOPs means that while the amount of computation is greatly reduced, the accuracy is similar to that of yolov5n without pre-training network. However, the computing speed in the GPU environment is not improved, mainly due to the characteristics of the SE module, not to elaborate, more suitable for CPU mobile platform.

一个小节目,一个Yolov5的贡献者,只更改了一个数字。下一篇文章将向您展示如何使用TensorRT C++来加速yolov5。

[En]

A small show, a Yolov5 contributor who has changed only one number. The next article will show you how to use TensorRT C++ to accelerate yolov5.

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

Original: https://blog.csdn.net/rglkt/article/details/125509020
Author: rglkt
Title: Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6272/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部