在YOLOV8中复现LeYOLO(添加yaml、block、conv)

源码地址:

yolov8:https://github.com/ultralytics/ultralytics
le-yolo:https://github.com/LilianHollard/LeYOLO

如何在yolov8中添加自己模块

参考:https://blog.csdn.net/qq_40938217/article/details/140805666

LeYOLO论文解读

参考:https://blog.csdn.net/qq_40938217/article/details/140848567

LeYOLO复现

  1. leyolo本来就是基于yolov8项目实现的,为了方便项目的管理,本人希望将该项目加入到v8中一起管理。
  • 首先将LeYOLO-main\ultralytics\cfg\cfg和LeYOLO-main\weights中的yaml和权重文件放到yolov8中
  • ultralytics/nn/modules/conv.py中添加如下代码
def activation_function(act="RE"):
    res = nn.Hardswish()
    if act == "RE":
        res = nn.ReLU6(inplace=True)
    elif act == "GE":
        res = nn.GELU()
    elif act == "SI":
        res = nn.SiLU()
    elif act == "EL":
        res = nn.ELU()
    else:
        res = nn.Hardswish()
    return res
class mn_conv(nn.Module):
    def __init__(self, c1, c2, k=1, s=1, act="RE", p=None, g=1, d=1):
        super().__init__()
        padding = 0 if k==s else autopad(k,p,d)
        self.c = nn.Conv2d(c1, c2, k, s, padding, groups=g)
        self.bn = nn.BatchNorm2d(c2)
        self.act = activation_function(act)#nn.ReLU6(inplace=True) if act=="RE" else nn.Hardswish()
    
    def forward(self, x):
        return self.act(self.bn(self.c(x)))
def autopad(k, p=None, d=1):  # kernel, padding, dilation
    """Pad to 'same' shape outputs."""
    if d > 1:
        k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k]  # actual kernel-size
    if p is None:
        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad
    return p

ultralytics/nn/modules/block.py

from .conv import Conv, DWConv, GhostConv, LightConv, RepConv, autopad, mn_conv
class MobileNetV3_BLOCK(nn.Module):
    def __init__(self, c1, c2, k=3, e=None, sa="None", act="RE", stride=1, pw=True):
        #input_channels, output_channels, repetition, stride, expension ratio
        super().__init__()
        #act = nn.ReLU6(inplace=True) if NL=="RE" else nn.Hardswish()
        c_mid = e if e != None else c1
        self.residual = c1 == c2 and stride == 1

        features = [mn_conv(c1, c_mid, act=act)] if pw else [] #if c_mid != c1 else []
        features.extend([mn_conv(c_mid, c_mid, k, stride, g=c_mid, act=act),
                         #attn,
                         nn.Conv2d(c_mid, c2, 1),
                         nn.BatchNorm2d(c2),
                         #nn.SiLU(),
                         ])
        self.layers = nn.Sequential(*features)
    def forward(self, x):
        #print(x.shape)
        if self.residual:
            return x + self.layers(x)
        else:
            return self.layers(x)

ultralytics/nn/tasks.py
对于添加模块不懂的看上面贴的加入自己模块方法就行,是通用的方法,但是下面这步操作是针对于LeYOLO特别的一步,很多模块需要进行下面这步操作

if m in (MobileNetV3_BLOCK):
      if isinstance(args[3],int): #might use "None"
          args[3] = make_divisible(min(args[3], max_channels) * width, 8)

训练代码

from ultralytics import YOLO

model = YOLO("ultralytics/cfg/models/leyolo/leyolosmall.yaml").Load("leyolo_weights/LeYOLOSmall.pt") 

model.train(data="ultralytics/cfg/datasets/coco8.yaml", epochs=200, workers=1,imgsz=96,device=3,batch=64,resume=True)

model.val()

Leyolo在设备端有强大的潜力,希望大家训练顺利

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值