yolov5
模型使用
- 工程
git clone https://github.com/ultralytics/yolov5.git
- 预训练模型
首次运行时,attempt_download函数会下载对应的模型。模型参数配置在models/*.yaml里。 - yolov5网络组成
Focus + Backbone + Detect - coco数据集训练
下载coco数据集,放在于yolov5同一目录下。
python3 train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
yolov5m 40
yolov5l 24
yolov5x 16
模型转换部署
pytorch模型部署,一般会转化为onnx。下面来讲讲关于yolov5的转换过程。
- Focus部分修改
./models/common.py中
class Focus(nn.Module):
# Focus wh information into c-space
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
super(Focus, self).__init__()
self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
self.contract = Contract(gain=2)
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
# 使用Contract中的方法
# x = self.contract(x)
N, C, H, W = x.size() # assert (H / s == 0) and (W / s == 0), 'Indivisible gain'
s = 2
x = x.view(N, C, H // s, s, W // s, s) # x(1,64,40,2,40,2)
x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40)
x = x.view(N, C * s * s, H // s, W // s) # x(1,256,40,40)
return self.conv(x)
# 转换slice会有问题,注释掉
# return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
- Detect部分修改
./models/yolo.py中
class Detect(nn.Module):
stride = None # strides computed during build
export = True # onnx export 此处改为True
.......
.......
.......
def forward(self, x):
# x = x.copy() # for profiling
z = [] # inference output
self.training |= self.export

本文详细介绍YOLOv5模型的使用与部署流程,包括模型结构、预训练模型下载、模型训练、模型转换为ONNX格式的方法及C++部署过程中的后处理技巧,并对比了YOLO系列模型。
最低0.47元/天 解锁文章
8561

被折叠的 条评论
为什么被折叠?



