参考文章:pt转onnx转ncnn模型(yolov8部署安卓)_best.pt 转ncnn模型-CSDN博客
Yolov8-Ncnn模型部署Android,实现单一图片识别_yolov8转ncnn-CSDN博客
onnx转化为ncnn这条路径现在已经落后了,更多的是通过pnnx转化为ncnn。
一、pt转onnx
from ultralytics import YOLO
# Load a model
model = YOLO(r"E:\StudyData\python\yolov8\yolov8n.pt") # load a custom trained model
# Export the model to NCNN with arguments
success = model.export(format="onnx", simplify= True, opset=12)
修改head和block
class C2f(nn.Module):
#省略上面的代码
#.....
#源代码:
# def forward(self, x):
# """Forward pass through C2f layer."""
# y = list(self.cv1(x).chunk(2, 1))
# y.extend(m(y[-1]) for m in self.m)
# return self.cv2(torch.cat(y, 1))
#修改为:
def forward(self, x):
# 全部替换为
x = self.cv1(x)
x = [x, x[:, self.c:, ...]]
x.extend(m(x[-1]) for m in self.m)
x.pop(1)
return self.cv2(torch.cat(x, 1))
def __init__(self, nc=80, ch=()):
#....省略上面代码
#源代码:
# def forward(self, x):
# """Concatenates and returns predicted bounding boxes and class probabilities."""
# if self.end2end:
# return self.forward_end2end(x)
#
# for i in range(self.nl):
# x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)
# if self.training: # Training path
# return x
# y = self._inference(x)
# return y if self.export else (y, x)
#修改为:
def forward(self, x):
"""Concatenates and returns predicted bounding boxes and class probabilities."""
shape = x[0].shape # BCHW
for i in range(self.nl):
x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)
if self.training:
return x
elif self.dynamic or self.shape != shape:
self.anchors, self.strides = (x.transpose(0, 1) for x in make_anchors(x, self.stride, 0.5))
self.shape = shape
# 中间部分注释掉,return语句替换为
return torch.cat([xi.view(shape[0], self.no, -1) for xi in x], 2).permute(0, 2, 1)
然后执行上述python
如果没找到onnx模块,执行 pip install onnx
得到onnx
二、对onnx进行简化
pip install onnxsim
python3 -m onnxsim yolov8n.onnx yolov8n-sim.onnx
三、onnx-sim转ncnn
依次执行下面的命令行
sudo apt install build-essential libopencv-dev cmake
sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libvulkan-dev vulkan-utils libopencv-dev
git clone https://github.com/Tencent/ncnn.git
cd ncnn
git submodule update --init #暂时不知道什么作用
mkdir build
cd build
cmake ..
make -j8
make install
sudo ldconfig # 刷新
要注意是否成功安装了onnx2ncnn
找到ncnn/build/tools/onnx文件夹,能找到onnx2ncnn.cpp
把onnx放入文件夹下,执行下面的命令
./onnx2ncnn my.onnx my.param my.bin
成功得到bin和param文件。