COCO 資料集

      在〈COCO 資料集〉中尚無留言

yolo26n.pt 模型可用來偵測圖片,這個模型包含了網路架構及訓練後的權重,此模型又稱為預訓練權重。

預設權重使用微軟的 COCO 資料集訓練而成的。COCO 收集了16萬4000張的圖片,共有 80 種物件。

yaml 及 pt

yolo26n.yaml 只是模型的設計圖(網路結構),沒有任何訓練資料;而 yolo26n.pt 則是已經訓練完成的模型,裡面包含結構和權重。換句話說,yolo26n.pt 就是用 yolo26n.yaml 這個架構訓練出來的成果。

安裝套件

請記得先安裝 Pytorch for cuda,然後才安裝 ultralytics。


pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130 --no-cache-dir

pip install pip install ultralytics labelimg

COCO 資料集下載

COCO 資料集可以由如下網址下載

https://github.com/wongkinyiu/yolov7 網址往下拉,可以看到 train, val, test, labels 的下載網址,分別如下。
train    :  http://images.cocodataset.org/zips/train2017.zip
val       :  http://images.cocodataset.org/zips/val2017.zip
test     :  http://images.cocodataset.org/zips/test2017.zip
labels :  https://github.com/WongKinYiu/yolov7/releases/download/v0.1/coco2017labels-segments.zip

請將上面的四個檔案下載到專案的根目錄下。

先把 coco2017labels-segments.zip 解壓縮至此,產生 coco 目錄。

將 train2017.zip 解壓縮至此,然後將 train2017 目錄改成 images,再將 images 移到 coco/train 目錄下。
將 val2017.zip 解壓縮至此,然後將 val2017 目錄改成 images,再將 images 移到 coco/val 目錄下。
將 test2017.zip 解壓縮至此,然後將 test2017 目錄改成 images,再將 images 移到 coco/test 目錄下。

將 coco/labels/train2017 移到 coco/train 之下,並改成 labels。
將 coco/labels/val2017 移到 coco/val 之下,並改成 labels。

設定 data.yaml

在 coco 下新增 data.yaml,內容如下。請注意一定要寫絕對路徑。請注意,本專案根目錄為 e:\python\yolo。

train: e:/python/yolo/coco/train/images
val: e:/python/yolo/coco/val/images
test: e:/python/yolo/coco/test/images
# number of classes
nc: 80

# class names
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
         'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
         'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
         'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
         'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
         'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
         'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
         'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
         'hair drier', 'toothbrush' ]

從零開始訓練

由「yolo26n.yaml」取得網路架構模型,再由 model.train() 重頭訓練,這種稱為 training from scratch。yolo26n.yaml 存放在專案下的 .venv\Lib\site-packages\ultralytics\cfg\models\26

從零開始訓練這種方法很硬,會持續數周的時間,而且訓練出來的不一定比官方出的預訓練好,一般人不會這麼作

import os
import shutil
import time
from ultralytics import YOLO
#訓練模型時,一定要放在 __name__ 區塊內
#否則會出現需使用 fork來執行子行程的錯誤
if __name__=='__main__':
    path="./runs/detect/train"
    if os.path.exists(path):
        shutil.rmtree(path)
    files=[
        "./coco/train/labels.cache",
        "./coco/val/labels.cache"   
    ]
    for file in files:
        if os.path.exists(file):
            os.remove(file)
    #model = YOLO("yolo26n.pt")
    model = YOLO("yolo26n.yaml")
    print(model.model.yaml)
    print("開始訓練 .........")
    t1=time.time()
    model.train(data="./coco/data.yaml", epochs=10, imgsz=640)
    t2=time.time()
    print(f'訓練花費時間 : {t2-t1}秒')
    print(f'模型匯出路徑 : {model.export()}')

yolo26n.yaml 亦可由如下方式查詢。

model = YOLO("yolo26n.yaml")
print(model.model.yaml)

結果如下

{'nc': 80, 'end2end': True, 'reg_max': 1, 'scales': {'n': [0.5, 0.25, 1024], 's': [0.5, 0.5, 1024], 'm': [0.5, 1.0, 512], 'l': [1.0, 1.0, 512], 'x': [1.0, 1.5, 512]}, 'backbone': [[-1, 1, 'Conv', [64, 3, 2]], [-1, 1, 'Conv', [128, 3, 2]], [-1, 2, 'C3k2', [256, False, 0.25]], [-1, 1, 'Conv', [256, 3, 2]], [-1, 2, 'C3k2', [512, False, 0.25]], [-1, 1, 'Conv', [512, 3, 2]], [-1, 2, 'C3k2', [512, True]], [-1, 1, 'Conv', [1024, 3, 2]], [-1, 2, 'C3k2', [1024, True]], [-1, 1, 'SPPF', [1024, 5, 3, True]], [-1, 2, 'C2PSA', [1024]]], 'head': [[-1, 1, 'nn.Upsample', ['None', 2, 'nearest']], [[-1, 6], 1, 'Concat', [1]], [-1, 2, 'C3k2', [512, True]], [-1, 1, 'nn.Upsample', ['None', 2, 'nearest']], [[-1, 4], 1, 'Concat', [1]], [-1, 2, 'C3k2', [256, True]], [-1, 1, 'Conv', [256, 3, 2]], [[-1, 13], 1, 'Concat', [1]], [-1, 2, 'C3k2', [512, True]], [-1, 1, 'Conv', [512, 3, 2]], [[-1, 10], 1, 'Concat', [1]], [-1, 1, 'C3k2', [1024, True, 0.5, True]], [[16, 19, 22], 1, 'Detect', ['nc']]], 'scale': 'n', 'yaml_file': 'yolo26n.yaml', 'channels': 3}

下載預訓練模型

到 https://github.com/ultralytics/ultralytics 網站,往下拉下載 YOLOv8n ,儲存在專案根目錄下。

yolo.exe 訓練權重

在 Terminal 執行如下指令,epochs 設定為 200 次。

yolo task=detect mode=train model=./yolov8n.pt data=./coco/data.yaml epochs=200 imgsz=640

使用 Python 訓練模型

在專案下新增 train.py 檔,由 YOLO 戴入預訓練模型 yolov8n.pt 產生 model 物件,再由 model.train() 即可開始訓練。

請注意這段程式碼一定要寫在 if __name__ 的區塊中,否則會出現需使用 fork 執行子行程的錯誤。

import os
import shutil
import time
from ultralytics import YOLO
#訓練模型時,一定要放在 __name__ 區塊內
#否則會出現需使用 fork來執行子行程的錯誤
if __name__=='__main__':
    path="./runs/detect/train"
    if os.path.exists(path):
        shutil.rmtree(path)
    files=[
        "./coco/train/labels.cache",
        "./coco/val/labels.cache"   
    ]
    for file in files:
        if os.path.exists(file):
            os.remove(file)
    model = YOLO("yolo26n.pt")
    print("開始訓練 .........")
    t1=time.time()
    model.train(data="./coco/data.yaml", epochs=200, imgsz=640)
    t2=time.time()
    print(f'訓練花費時間 : {t2-t1}秒')
    print(f'模型匯出路徑 : {model.export()}')

使用 RTX 3080Ti 訓練時間約 48 小時

無法訓練的人,請由本站下載 yolov8_coco.zip ,解開後置於 ./runs/detect/train/weights 之下。

中斷接續訓練

上面訓練要花費二、三天的時間,中途若中斷,可以由中斷點再接續訓練,指令如下

from ultralytics import YOLO
if __name__=='__main__':
    model = YOLO('path/to/last.pt')  # load a partially trained model
    # Resume training
    results = model.train(resume=True)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *