目录
前提:ubuntu20.04, python3.9
1. yolov8的安装
yolov8 官方说明:Home - Ultralytics YOLOv8 Docs
yolov8 官方仓库: https://github.com/ultralytics/ultralytics
anaconda官网网址下载: Anaconda | The World’s Most Popular Data Science Platform
$ conda create -n yolov8 python=3.9 -y
$ conda activate yolov8
$ pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 数据集的训练和预测
coco数据集与其他数据格式的转换:
2.1 数据集的训练
from ultralytics import YOLO
model = YOLO("xxx/weights/yolov8n.pt")
results = model.train(data="/xxx/train_cfg.yaml", epochs=100, batch=4)
# train_cfg.yaml coco
train: /xxx/dataSet/train.txt # train images
val: //xxx/val.txt # val images
test: //xxx/test.txt #
# Classes
nc: 2 # number of classes
names: ['apple', 'orange']
2.2 数据集的预测
from ultralytics import YOLO
import os
import cv2
# Load a model
model = YOLO("/xxx/weights/best.pt")
# Use the model
path = "/xxx/test"
inputs = list()
for i_name in os.listdir(path):
i_path = os.path.join(path, i_name)
i_img = cv2.imread(i_path, 1)
inputs.append(i_img)
model.predict(inputs, save=True, imgsz=320, conf=0.25)
3. 模型转换成ncnn
3.1 安装必须库
# 安装 onnx, onnxsim, ncnn
conda activate yolov8
pip install onnx -i https://pypi.doubanio.com/simple
pip install onnxsim -i https://pypi.doubanio.com/simple
cd /home/xxy
git clone https://github.com/Tencent/ncnn.git
cd ncnn
mkdir build && cd build
cmake ..
make
make install
3.2 转换
# 使用 onnx, onnxsim, ncnn
cd /xxx/ultralytics/runs/detect/${train_xxx}/weights
conda activate yolov8
# pt -> onnx
python export.py
# onnx -> onnxsim
python3 -m onnxsim best.onnx best-sim.onnx
# onnxsim -> ncnn
cd /xxxy/ncnn/build/tools/onnx
./onnx2ncnn /xxx/best-sim.onnx /xxx/best-sim.param /xxx/best-sim.bin
# export.py
from ultralytics import YOLO
model = YOLO("/xxx/ultralytics/runs/detect/${train_xxx}/weights/best.pt")
success = model.export(format="onnx") # 将模型导出为 ONNX 格式