【YOLO目標檢測實戰 】3.使用YOLO11訓練COCO128資料集

盛夏夜發表於2024-11-06

1 訓練YOLO11模型

  1. 準備訓練資料
mkdir datasets && cd datasets

wget https://ultralytics.com/assets/coco128.zip

unzip coco128.zip

cd ..
  1. 準備預訓練模型
mkdir weights && cd weights

wget https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.pt

wget https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11s.pt

cd ..
  1. 準備配置檔案
# 準備資料配置檔案
mkdir configs && cd configs

# 複製模型配置檔案
cp path/to/ultralytics-8.3.24/ultralytics/cfg/models/11/yolo11.yaml ./yolo11.yaml

# 複製資料配置檔案
cp path/to/ultralytics-8.3.24/ultralytics/cfg/datasets/coco128.yaml ./coco128.yaml

# 複製下載字型檔案(字型檔案在資料下載連結中下載)
cp path/to/Arial.ttf ~/.config/Ultralytics

cd ..
  1. 修改資料配置
 vim configs/coco128.yaml
path: path/to/datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)
  1. 訓練YOLO11模型
mkdir scripts && cd scripts

vim scripts/train.py
from ultralytics import YOLO

model = YOLO('configs/yolo11s.yaml').load('weights/yolo11s.pt')

model.train(
    epochs=100,
    batch=8,
    device='0',
    workers=8,
    imgsz=640,
    project='runs',
    amp=False,
    cache='ram',# 'ram', 'disk' or False
    data='configs/coco128.yaml'
)
python scripts/train.py

2 評估YOLO11模型

vim scripts/val.py
from ultralytics import YOLO

model = YOLO('runs/train/weights/best.pt')

metrics = model.val(project='runs')
metrics.box.map
metrics.box.map50
metrics.box.map75
metrics.box.maps
python scripts/val.py

3 YOLO11推理圖片

vim scripts/predict.py
from ultralytics import YOLO

model = YOLO('runs/train/weights/best.pt')

results = model(
    source='datasets/coco128/images/train2017',
    imgsz=640,
    conf=0.5,
    save=True,
    save_txt=True,
    project='runs'
)
python scripts/predict.py

資料下載

連結: https://pan.baidu.com/s/1SkTVrOnsjUnPzO2SkQaV2g 提取碼: wxqg

歡迎關注

微信公眾號:AI創客工廠

QQ群:392979889

相關文章