YOLOv5改进之替换Backbone为EfficientNet B0

BestYOLO是一个基于YOLOv5v7.0改进的目标检测框架,致力于科研和竞赛的应用。该框架集成了使用torchvision.models作为Backbone的YOLOv5算法,并支持使用efficientnet_b0模型作为Backbone。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

BestYOLO:https://github.com/WangRongsheng/BestYOLO

BestYOLO是一个以科研和竞赛为导向的最好的YOLO实践框架!

目前BestYOLO是一个完全基于YOLOv5 v7.0 进行改进的开源库,该库将始终秉持以落地应用为导向,以轻便化使用为宗旨,简化各种模块的改进。目前已经集成了基于torchvision.models 模型为Backbone的YOLOv5目标检测算法,同时也将逐渐开源更多YOLOv5应用程序。

替换为efficientnet_b0模型

修改common.py

在最后添加:

from torchvision import models
'''
模型:efficientnet_b0
'''
class efficientnet_b01(nn.Module):
    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.efficientnet_b0()
        modules = list(model.children())
        modules = modules[0][:4]
        self.model = nn.Sequential(*modules)
    def forward(self, x):
        return self.model(x)
    
class efficientnet_b02(nn.Module):
    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.efficientnet_b0()
        modules = list(model.children())
        modules = modules[0][4:6]
        self.model = nn.Sequential(*modules)
    def forward(self, x):
        return self.model(x)
    
class efficientnet_b03(nn.Module):
    def __init__(self, ignore) -> None:
        super().__init__()
        model = models.efficientnet_b0()
        modules = list(model.children())
        modules = modules[0][6:]
        self.model = nn.Sequential(*modules)
    def forward(self, x):
        return self.model(x)

如果不需要开启预训练权重,删除pretrained=True即可。

修改yolo.py

elif m is Expand:下面添加:

elif m is efficientnet_b01 or m is efficientnet_b02 or m is efficientnet_b03:
	c2 = args[0]

修改.yaml配置

# YOLOv5 🚀 by Ultralytics, GPL-3.0 license

# Parameters
nc: 80  # 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, efficientnet_b01, [40]],  # 0
   [-1, 1, efficientnet_b02, [112]],  # 1
   [-1, 1, efficientnet_b03, [1280]],  # 2
   [-1, 1, SPPF, [1024, 5]],  # 3
  ]

# YOLOv5 v6.0 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 1], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, C3, [512, False]],  # 7

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 0], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, C3, [256, False]],  # 11 (P3/8-small)

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 7], 1, Concat, [1]],  # cat head P4
   [-1, 3, C3, [512, False]],  # 14 (P4/16-medium)

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 3], 1, Concat, [1]],  # cat head P5
   [-1, 3, C3, [1024, False]],  # 17 (P5/32-large)

   [[11, 14, 17], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

.yaml配置文件中的depth_multiplewidth_multiple可以同时设置为1.0试试,说不定会有不错的效果。

具体指标

modelslayersparametersmodel size(MB)
efficientnet_b0443624153113.0
YOLOv5(You Only Look Once version 5)是一个基于PyTorch的实时物体检测模型系列,它默认使用Darknet作为其主干网络。如果你想将EfficientNet(一种高效且深度的卷积神经网络架构)替换为主干网络,你需要做以下几步: 1. **安装依赖库**:首先确保已经安装了`yolov5`库以及`timm`(Timm库用于加载预训练的EfficientNet模型),你可以通过pip进行安装: ``` pip install yolov5 timm ``` 2. **修改配置文件**:YOLOv5的配置是在`.yaml`文件中管理的。打开`yolov5s.yaml`, `yolov5m.yaml`, 或者 `yolov5l.yaml`等对应的配置文件,找到`model`部分,通常会在`backbone`键下指定主干网络结构。将`backbone: 'darknet53'`更改为`backbone: 'efficientnet-b0'`或其他EfficientNet变体,如`'efficientnet-b3'`。 3. **下载预训练模型**:由于EfficientNet不是YOLOv5的标准选择,你需要预先从Timm或者其他来源下载预训练的EfficientNet权重。这通常需要通过`timm.models.efficientnet.from_pretrained()`函数来完成。例如: ```python from timm.models import efficientnet_b0 weights_path = efficientnet_b0(pretrained=True).state_dict().keys() ``` 然后将这些权重保存到合适的位置,并在YOLOv5配置中指定路径。 4. **替换权重**:将EfficientNet的预训练权重复制到YOLOv5模型的对应层。这一步可能涉及到模型结构调整,因为YOLOv5EfficientNet的结构可能存在差异。 5. **训练新模型**:更新完配置文件并加载了新的预训练模型之后,你可以用修改后的模型数据集重新进行训练,注意调整学习率其他超参数以适应新的主干网络。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落难Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值