1.更换结果
模型参数下降了,但是精度同样下降了,可相比于把backbone换成mobilenetv3-small,还是好一点
2.更换步骤
2.1 创建文件MobileNetV3 .py
- 路径为ultralytics/nn
- 在nn文件夹目录中创建MobileNetV3 .py
-
MobileNetV3 的代码为:
from torch import nn
# ###### Mobilenetv3
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class SELayer(nn.Module):
def __init__(self, channel, reduction=4):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
h_sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x)
y = y.view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
class conv_bn_hswish(nn.Module):
def __init__(self, c1, c2, stride):
super(conv_bn_hswish, self).__init__()
self.conv = nn.Conv2d(c1, c2, 3, stride, 1, bias=False)
self.bn = nn.BatchNorm2d(c2)
# self.act = h_swish()
self.act = nn.Hardswish(inplace=True)
def forward(self, x):
return self.act(self.bn(self.conv(x)))
def fuseforward(self, x):
return self.act(self.conv(x))
class MobileNetV3_InvertedResidual(nn.Module):
def __init__(self, inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs):
super(MobileNetV3_InvertedResidual, self).__init__()
assert stride in [1, 2]
self.identity = stride == 1 and inp == oup
if inp == hidden_dim:
self.conv = nn.Sequential(
# dw
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
# h_swish() if use_hs else nn.ReLU(inplace=True),
nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Sequential(),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
else:
self.conv = nn.Sequential(
# pw
nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim),
# h_swish() if use_hs else nn.ReLU(inplace=True),
nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
# dw
nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
# Squeeze-and-Excite
SELayer(hidden_dim) if use_se else nn.Sequential(),
# h_swish() if use_hs else nn.ReLU(inplace=True),
nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
)
def forward(self, x):
y = self.conv(x)
if self.identity:
return x + y
else:
return y
2.2 在ultralytics\ultralytics\nn\tasks.py文件中加入该模块
在task.py文件开头写上以下代码:
from ultralytics.nn.backbone.MobileNetV3 import *
将task.py文件中的def _predict_once函数模块要替换为更换网络结构后的预测模块
换成以下代码:
def _predict_once(self, x, profile=False, visualize=False):
"""
Perform a forward pass through the network.
Args:
x (torch.Tensor): The input tensor to the model.
profile (bool): Print the computation time of each layer if True, defaults to False.
visualize (bool): Save the feature maps of the model if True, defaults to False.
Returns:
(torch.Tensor): The last output of the model.
"""
y, dt = [], [] # outputs
for m in self.model:
if m.f != -1: # if not from previous layer
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
if profile:
self._profile_one_layer(m, x, dt)
if hasattr(m, 'backbone'):
x = m(x)
for _ in range(5 - len(x)):
x.insert(0, None)
for i_idx, i in enumerate(x):
if i_idx in self.save:
y.append(i)
else:
y.append(None)
# for i in x:
# if i is not None:
# print(i.size())
x = x[-1]
else:
x = m(x) # run
y.append(x if m.i in self.save else None) # save output
if visualize:
feature_visualization(x, m.type, m.i, save_dir=visualize)
return x
值得注意的是,这一步换完后可能会报错,报错如下:
这时只要在_predict_once这个函数的上一行代码,也就是如下图所示,把embed去掉,就不会报错了,也可以把源代码注释,直接换成下图中我注释的代码
2.3 在task.py文件中的def parse_model函数模块中加入MobileNetV3:
elif m in {conv_bn_hswish, MobileNetV3_InvertedResidual}:
c1, c2 = ch[f], args[0]
if c2 != nc: # if not output
c2 = make_divisible(min(c2, max_channels) * width, 8)
args = [c1, c2, *args[1:]]
2.4 创建yolov8-MobileNetV3.yaml文件
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 3 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
# inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs
backbone:
# [from, repeats, module, args]
- [-1, 1, conv_bn_hswish, [16, 2]] # 0-P1/2
- [-1, 1, MobileNetV3_InvertedResidual, [16, 16, 3, 1, 0, 0]]
- [-1, 1, MobileNetV3_InvertedResidual, [24, 64, 3, 2, 0, 0]] # 2-p2/4
- [-1, 1, MobileNetV3_InvertedResidual, [24, 72, 3, 1, 0, 0]]
- [-1, 1, MobileNetV3_InvertedResidual, [40, 72, 5, 2, 1, 0]] # 4-p3/8
- [-1, 1, MobileNetV3_InvertedResidual, [40, 120, 5, 1, 1, 0]]
- [-1, 1, MobileNetV3_InvertedResidual, [40, 120, 5, 1, 1, 0]]
- [-1, 1, MobileNetV3_InvertedResidual, [80, 240, 3, 2, 0, 1]] # 7-p4/16
- [-1, 1, MobileNetV3_InvertedResidual, [80, 200, 3, 1, 0, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [80, 184, 3, 1, 0, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [80, 184, 3, 1, 0, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [112, 480, 3, 1, 1, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [112, 672, 3, 1, 1, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [160, 672, 5, 2, 1, 1]] # 13-p5/32
- [-1, 1, MobileNetV3_InvertedResidual, [160, 960, 5, 1, 1, 1]]
- [-1, 1, MobileNetV3_InvertedResidual, [160, 960, 5, 1, 1, 1]]
# YOLOv8.0n head
head:
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 12], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [256]] # 18
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 6], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [128]] # 21 (P3/8-small)
- [-1, 1, Conv, [128, 3, 2]]
- [[-1, 18], 1, Concat, [1]] # cat head P4
- [-1, 3, C2f, [256]] # 24 (P4/16-medium)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 15], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f, [512]] # 27 (P5/32-large)
- [[21, 24, 27], 1, Detect, [nc]] # Detect(P3, P4, P5)
ok,到此结束,谢谢
创作此博客的过程中借鉴了以下博客:
YOLOv8改进 更换轻量化模型MobileNetV3_yolov8轻量化-优快云博客
解决yolov8替换骨干网络efficientVit出现的报错TypeError: _predict_once() takes_yolov8改进efficientvit-优快云博客