Try   HackMD

YOLOv8 基本環境與教學

tags: YOLO AI

2023年Ultralytics釋出YOLOv8,相較於以前的版本速度更加提升
以下簡單說明基本環境與訓練、預測的教學

1. 首先下載CUDA版本,以及cuDNN

我們可以從NVIDIA官網進行下載,選定你的作業系統以及想要下載的版本
https://developer.nvidia.com/cuda-downloads

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

另外小提醒,cuDNN需要註冊會員後才能下載

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

把 C:\Users<username>\Downloads\cuda\bin 資料夾內檔案複製到
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0\bin

把 C:\Users<username>\Downloads\cuda\include 資料夾內檔案複製到
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0\include

把 C:\Users<username>\Downloads\cuda\lib\x64 資料夾內檔案複製到
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0\lib\x64

2. 接著安裝Python版本,筆者使用的版本是Python3.9,注意要從Python官網下載才行

Python的版本需>=3.7
https://www.python.org/

3. 接下來透過pip進行安裝,可從PyTorch官網上選擇執行版本對應的指令

安裝PyTorch, 版本需>=1.7
https://pytorch.org/get-started/locally/

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝YOLOv8
pip install ultralytics

至此,基本環境已建置完成

4. 進行YOLOv8訓練

圖片影像資料可以從各地蒐集或從Kaggle上下載,但請注意版權應用
準備好圖片資料後,須將圖片分成訓練(train)、驗證(valid)、以及測試(test)三個資料夾,如以下結構

  • train (訓練資料)
    • images (圖片)
    • labels (標註)
  • valid (驗證資料)
    • images (圖片)
    • labels (標註)
  • test (測試資料)
    • images (圖片)
    • labels (標註)

標註的資料須依照YOLO的格式,這邊可用LabelImg來進行,或是藉由RoboFlow線上方式來進行標註

LabelImge下載,pip執行
pip install labelimg

RoboFlow官網
https://roboflow.com/

筆者較推薦使用RoboFlow來進行資料的標註

接著需選定一個模型來進行訓練,YOLOv8提供了一些基本的預訓練模型可供下載來使用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

模型下載
https://github.com/ultralytics/ultralytics

或者也可以從頭開始訓練模型,在YOLO安裝的資料夾下尋找各Model的yaml檔
如選擇yolov8n.yaml

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後準備一個yaml來描述這些資料的位置,如以下data.yaml的內容

path: C:/yolo/yolov8
train: ./train/images
val: ./valid/images
test: ./test/images

nc: 1
names: ['cat']

準備好資料即可開始訓練,訓練的指令如下,參數可參考YOLOv8官網的documents
yolo detect train data=data.yaml model=yolov8n.pt epochs=100 imgsz=640 conf=0 device=cpu

https://docs.ultralytics.com/

也可以使用Python指令來進行訓練

from ultralytics import YOLO model = YOLO("yolov8n.yaml") model.train(data="data.yaml", mode="detect", epochs=100, imgsz=640, device="cpu")

其中device參數若未指定則會使用GPU進行訓練,device=0即使用第一張GPU卡,也可device=0,1使用2張卡來進行運算,而device=cpu即使用CPU來運算
由於筆者測試的電腦顯示卡型號太舊,會出現以下錯誤
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
因此筆者下面都使用CPU來進行訓練與預測

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

訓練完後的資料會出現在runs/detect/train底下,訓練好的模型放在runs/detect/train/weights底下,我們接下來將使用best.pt來進行估測

5. 進行YOLO估測

使用以下指令進行估測
yolo detect predict model=best.pt source=test/images save=true device=cpu

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

當save=true時,會將預測的結果儲存在runs/detect/predict底下

同樣以Python指令執行時

from ultralytics import YOLO model = YOLO("best.pt") result = model.predict( source="test/images", mode="predict", save=True, device="cpu" )

以上即是簡單的進行訓練與測試教學