2022 年下半年 Yolov7 才剛推出沒幾個月,2023 年初 Yolov8 馬上就推出來,此次 Yolov8 跟 Yolov5 同樣是 Ultralytics 這家公司所製作,並且一樣是使用 PyTorch 去做訓練,提供三大類型的訓練方式 : 物件偵測(Object Detection)、實例分割(Instance Segmentation)、影像分類(Image Classification)。
由下方圖片可以看到,Yolov8 目前推論的效果是贏過其他 Yolo 系列,並且提供五種模型大小 : YOLOv8n、YOLOv8s、YOLOv8m、YOLOv8l、YOLOv8x
from https://github.com/ultralytics/ultralytics
YoloV8 跟以往不同的地方是
- 無錨檢測(Anchor Free Detection)
- 新的卷積層(New Convolutions)
- 關閉馬賽克增強(Closing the Mosaic Augmentation)
YOLOv8 架構圖, made by GitHub user RangeKing
YOLOv8x 檢測和實例分割模型的輸出 form https://learnopencv.com/ultralytics-yolov8/
訓練自訂模型
此次YOLOv8跟以往訓練方式最大不同的是,它大幅優化API,讓一些不太會使用模型的人可以快速上手,不用再手動下載模型跟進入命令列選取py執行,真的是簡化很多
- 準備資料
首先準備要訓練的資料集,這裡資料夾格式跟 Yolov5 是一模一樣的,並且標記檔案為TXT,並且個人大力推薦使用 Robotflow 去做標記,他可以線上訓練模型,也可以在標記圖片時輔助標記,以及更重要的是他可以完全導出Yolo所需的格式,並且不需要撰寫yaml檔
資料夾格式
yaml 檔主要設定路徑跟標記類別數量( nc )、標記名稱(names)
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 後面參數 detect
、segment
、classify
,以及模型名稱
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
否則無法輸出
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
\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
最后
计算机视觉、图像处理、毕业辅导、作业帮助、代码获取,远程协助,代码定制,私聊会回复!