源码地址:
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复现
- 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()