# YoloV8 Ultralytics [toc] ## 常用連結: - [Yolov8 QuickStart](https://docs.ultralytics.com/quickstart/) - [Yolov8 Github](https://github.com/ultralytics/ultralytics) - [Yolov8 偵測教學說明](https://docs.ultralytics.com/tasks/detect/#predict) ## 訓練程式 `yolov8_train.py` ```python= import os from ultralytics import YOLO if __name__ == '__main__': # Load a model 模型讀取 model = YOLO('yolov8n.pt') # 讀取預訓練模型,若本地不存在該指定模型,則下載 # Train the model 模型訓練 # model.train(資料集.yaml, device=顯示卡ID或指定為CPU) model.train(data=os.path.join("datasets", "pill.v1i.yolov8", 'data.yaml'), device=0) ``` ### model 建立 整體建立於 [`model.py`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py) ,並且呼叫 [`model.detect.train`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/train.py) 可以理解為: 1. 根據指定模型(預訓練模型或是用 `model.yaml`) 先分出任務 (detect, seg, classify) 2. 根據使用目的,呼叫對應功能,在此呼叫 `train` `train` 部分可理解邏輯如下: 1. 其實是透過建立 `trainer` 來開始訓練, `trainer` 基底可參考[連結](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/trainer.py), 2. 根據任務可以去分化資料集讀取方式等功能,如 `detect` ,定義於 [`model.detect.train`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/train.py) 3. 訓練是根據指定的 `data.yaml` 內對應的資料夾路徑搜尋對應位置進行訓練 4. `data.yaml` 中 `train,val,test` 分別為訓練、驗證及測試圖片位置, `nc` 是 class 數量 ```yaml= train: ../train/images val: ../valid/images test: ../test/images nc: 23 names: ['Acetylcysteine_just', 'Acetylcysteine_opposite', 'Actein_just', 'Actein_opposite', 'Ambroxol_just', 'Ambroxol_opposite', 'Bambuterol_just', 'Bambuterol_opposite', 'Benzonatate', 'Cold_just', 'Cold_opposite', 'Dyphylline_just', 'Dyphylline_opposite', 'Fenoterol_just', 'Fenoterol_opposite', 'Hexoprenaline_just', 'Hexoprenaline_opposite', 'Ketotifen_just', 'Ketotifen_opposite', 'Montelukast_just', 'Montelukast_opposite', 'Theophylline_just', 'Theophylline_opposite'] roboflow: workspace: joey3639570 project: pill-2fdiw version: 1 license: CC BY 4.0 url: https://universe.roboflow.com/joey3639570/pill-2fdiw/dataset/1 ``` ## 預測程式 `yolov8_predict.py` ```python= import os from ultralytics import YOLO if __name__ == '__main__': # Load a model 讀取指定路徑模型 model = YOLO(os.path.join("runs", "detect", "train2", "weights", 'best.pt')) # 建立輸入資料夾路徑參數 images_dir_path = os.path.join("datasets", "pill.v1i.yolov8", 'test', "images") # 建立輸出用資料夾 os.makedirs("output", exist_ok=True) # 列出輸入資料夾中的檔案 images_path = os.listdir(images_dir_path) # 對資料夾中每個檔案進行處理 for image_path in images_path: # 取得完整相對路徑 full_image_path = os.path.join(images_dir_path, image_path) # 圖片放入模型取得結果 results = model.predict(full_image_path) # 將結果繪製後存成圖片 for result in results: result.save(filename=os.path.join("output", image_path)) ``` 同 train 部分,整體建立於 [`model.py`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py) ,但呼叫 [`model.detect.predict`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/predict.py) 可以理解為: 1. 根據指定模型(預訓練模型或是用 `model.yaml`) 先分出任務 (detect, seg, classify) 2. 根據使用目的,呼叫對應功能,在此呼叫 `predict` `predict` 部分可理解邏輯如下: 1. 其實是透過建立 `predictor` 來開始訓練, `predictor` 基底可參考[連結](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/predictor.py#L63), 2. 根據任務可以去分化資料集讀取方式等功能,如 `detect` ,定義於 [`model.detect.predict`](https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/predict.py) 3. `predict` 出來的物件是 `results` ,參考[連結](https://docs.ultralytics.com/modes/predict/#inference-sources)了解更多 :::info 若要將輸入源改成串流或是影片,可以參考[連結](https://docs.ultralytics.com/modes/predict/#inference-sources) :::