1.7 KiB
1.7 KiB
YOLOv5 訓練與部署流程
環境設置
使用 CMD 操作。
建立一個可以運行 YOLOv5 的 conda 環境。
資料集準備
- 從 Roboflow 下載 YOLOv8 格式的資料集,放到專案目錄(例如
data/下) - 修改資料集內的
data.yaml,依照以下格式調整路徑:
path: C:/Users/rd_de/yolov5git/data/your-dataset
train: train/images
val: valid/images
test: test/images
nc: 3 # 類別數量
names: ['class1', 'class2', 'class3']
訓練模型
先 cd 到 yolov5/ 目錄,再執行:
python train.py \
--data C:/Users/rd_de/yolov5git/data/10-02+10-01+10-038class/data.yaml \
--weights for720best.pt \
--img 640 \
--batch-size 8 \
--epochs 300 \
--device 0
訓練完成後,結果與權重檔位於:
runs/train/expX/weights/best.pt
推論測試
python detect.py \
--weights runs/train/exp9/weights/best.pt \
--source test14data/test/images \
--img 640 \
--conf 0.25 \
--device 0
轉換 ONNX
python exporting/yolov5_export.py --data data/mepretrained_paths_720.yaml
簡化 ONNX 模型:
python -m onnxsim \
runs/train/exp24/weights/best.onnx \
runs/train/exp24/weights/best_simplified.onnx
Kneron Toolchain(Docker)
啟動 Kneron Toolchain 容器(在 WSL 中執行):
docker run --rm -it \
-v $(wslpath -u 'C:\Users\rd_de\golfaceyolov5\yolov5'):/workspace/yolov5 \
kneron/toolchain:latest
使用onnx2nefxxx.py來轉檔(520、630、720) 520需要使用 removenode.py
從容器複製編譯好的 .nef 模型到本機:
docker cp <container_id>:/data1/kneron_flow/runs/train/exp6/weights/models_630.nef \
C:\Users\rd_de\golfaceyolov5\yolov5\runs\train\exp6\weights