yolov8训练教程

img

2022 年下半年 Yolov7 才剛推出沒幾個月,2023 年初 Yolov8 馬上就推出來,此次 Yolov8 跟 Yolov5 同樣是 Ultralytics 這家公司所製作,並且一樣是使用 PyTorch 去做訓練,提供三大類型的訓練方式 : 物件偵測(Object Detection)、實例分割(Instance Segmentation)、影像分類(Image Classification)。

img

由下方圖片可以看到,Yolov8 目前推論的效果是贏過其他 Yolo 系列,並且提供五種模型大小 : YOLOv8nYOLOv8sYOLOv8mYOLOv8lYOLOv8x

img

from https://github.com/ultralytics/ultralytics

YoloV8 跟以往不同的地方是

  • 無錨檢測(Anchor Free Detection)
  • 新的卷積層(New Convolutions)
  • 關閉馬賽克增強(Closing the Mosaic Augmentation)

img

YOLOv8 架構圖, made by GitHub user RangeKing

img

YOLOv8x 檢測和實例分割模型的輸出 form https://learnopencv.com/ultralytics-yolov8/

訓練自訂模型

此次YOLOv8跟以往訓練方式最大不同的是,它大幅優化API,讓一些不太會使用模型的人可以快速上手,不用再手動下載模型跟進入命令列選取py執行,真的是簡化很多

  1. 準備資料

首先準備要訓練的資料集,這裡資料夾格式跟 Yolov5 是一模一樣的,並且標記檔案為TXT,並且個人大力推薦使用 Robotflow 去做標記,他可以線上訓練模型,也可以在標記圖片時輔助標記,以及更重要的是他可以完全導出Yolo所需的格式,並且不需要撰寫yaml檔

img

資料夾格式

yaml 檔主要設定路徑跟標記類別數量( nc )、標記名稱(names)

img

yaml 檔格式

\2. 安裝

pip install ultralytics

\3. 開始訓練

python 模式

調整模型名稱即可自動切換模式

有些人在訓練時會出現 OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized. 這種錯誤,有兩種解法: 1.如下加上兩行程式碼 2.刪除 libiomp5md.dll

如出現 RuntimeError: DataLoader worker (pid(s),請在 train 增加 *workers=0* 參數,並嘗試調整數值

results = model.train(data="data.yaml", epochs=3,workers=0)
from ultralytics import YOLO
# import os
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" 

# Load a model
model = YOLO("yolov8n.pt")  # load an official model
results = model.train(data="data.yaml", epochs=3)
# Validate the model
results = model.val(data="data.yaml")  # no arguments needed, dataset and settings remembered

CLI 模式

要切換模式時需更改 yolo 後面參數 detectsegmentclassify ,以及模型名稱

yolo detect train data=coco128.yaml model=yolov8n.pt epochs=100 imgsz=640

\4. 預測

python 模式

results = model(source='0') 為啟動相機,results = model('folder')資料夾名稱
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  # load an official model
model = YOLO("path/to/best.pt")  # load a custom model

# Predict with the model
results = model.predict(source="https://ultralytics.com/images/bus.jpg",save=True)  # predict on an image

下方為結果圖,注意要打上save=True 否則無法輸出

img

CLI 模式

yolo detect predict model=yolov8n.pt source="https://ultralytics.com/images/bus.jpg"  # predict with official model
yolo detect predict model=path/to/best.pt source="https://ultralytics.com/images/bus.jpg"  # predict with custom model

\5. 導出

如需導出其他版本像是 ONNX、OpenVINO、TensorRT,只需在 Format 後填寫指定參數即可

python 模式

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  # load an official model
model = YOLO("path/to/best.pt")  # load a custom trained

# Export the model
model.export(format="onnx")

CLI 模式

yolo export model=yolov8n.pt format=onnx  # export official model
yolo export model=path/to/best.pt format=onnx  # export custom trained model

img

\6. 完整程式碼

from ultralytics import YOLO
model = YOLO("yolov8n.pt")  # load a pretrained YOLOv8n model
model.train(data="coco128.yaml")  # train the model
model.val()  # evaluate model performance on the validation set
model.predict(source="https://ultralytics.com/images/bus.jpg",save=True)  # predict on an image
model.export(format="onnx")  # export the model to ONNX format

最后

计算机视觉、图像处理、毕业辅导、作业帮助、代码获取,远程协助,代码定制,私聊会回复!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALiLiLiYa

一角两角不嫌少

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值