如何在colab配置YOLO v8并训练自己的数据集

训练前的参数配置

在 /content/ultralytics/ultralytics/yolo/cfg/default.yaml中修改了以下参数:但似乎不改也没事

# Train settings -------------------------------------------------------------------------------------------------------
model:/content/ultralytics/yolov8s.pt  # path to model file, i.e. yolov8n.pt, yolov8n.yaml
data: /content/Letters.v1i.yolov5pytorch.zip/data.yaml # path to data file, i.e. coco128.yaml
epochs: 100  # number of epochs to train for
patience: 50  # epochs to wait for no observable improvement for early stopping of training
batch: 16  # number of images per batch (-1 for AutoBatch)
imgsz: 640  # size of input images as integer or w,h
save: True  # save train checkpoints and predict results
save_period: -1 # Save checkpoint every x epochs (disabled if < 1)

训练过程

执行路径/content/ultralytics/ultralytics/yolo/v8/detect/train.py中的train.py:

!python train.py

如果在运行train.py时提示没有module:ultralytics 可以在前面自己安装

pip install ultralytics
# from ultralytics.nn.tasks import DetectionModel因为这句报错

需要在路径/content/ultralytics/ultralytics/yolo/v8/detect/train.py下将自己待训练的数据集路经填入data处。并修改model 为yolo8s.pt

def train(cfg=DEFAULT_CFG, use_python=False):
    model = cfg.model or 'yolov8s.pt'
    data = cfg.data or '/content/datasets/Letters.v1i.yolov5pytorch.zip/data.yaml'  # or yolo.ClassificationDataset("mnist")
    device = cfg.device if cfg.device is not None else ''

路径/content/ultralytics/ultralytics/yolo/engine/trainer.py 下的这个文件不知道data要不要改

# Ultralytics YOLO , GPL-3.0 license
"""
Train a model on a dataset

Usage:
    $ yolo mode=train model=yolov8s.pt data=/content/datasets/data.yaml imgsz=640 epochs=100 batch=16
"""
import os

推理

推理 设置好task、mode、model和测试图片路径source即可
task: "detect" # choices=['detect', 'segment', 'classify', 'init'] # init is a special case. Specify task to run.
mode: "predict" # choices=['train', 'val', 'predict', 'export'] # mode to run task in.
model: E:\\DLTest\\YOLOv8\\runs\\detect\\best.pt 
source: MaskDataSet/test/images/ # source directory for images or videos
from IPython.display import Image  
# first, display our ground truth data
# The ground truth [Train data] is available in jpg file at location /content/yolov5/runs/train/exp2/test_batch0_labels.jpg 
print("GROUND TRUTH TRAINING DATA:")
Image(filename='/content/ultralytics/runs/detect/train7/val_batch0_pred.jpg')
from IPython.display import Image  
# first, display our ground truth data
# The ground truth [Train data] is available in jpg file at location /content/yolov5/runs/train/exp2/test_batch0_labels.jpg 
print("GROUND TRUTH TRAINING DATA:")
Image(filename='/content/ultralytics/runs/detect/train7/val_batch1_pred.jpg')

导出onnx文件:

在路径/content/ultralytics/ultralytics/yolo/engine/exporter.py下修改cfg.model

def export(cfg=DEFAULT_CFG):
    cfg.model = cfg.model or 'yolov8s.yaml'
    cfg.format = cfg.format or 'torchscript'

    from ultralytics import YOLO
    model = YOLO(cfg.model)
    model.export(**vars(cfg))


if __name__ == '__main__':
    """
    CLI:
    yolo mode=export model=yolov8s.yaml format=onnx
    """
    export()

结果展示

如何在colab配置YOLO v8并训练自己的数据集_第1张图片
如何在colab配置YOLO v8并训练自己的数据集_第2张图片
如何在colab配置YOLO v8并训练自己的数据集_第3张图片

你可能感兴趣的:(YOLO,深度学习,人工智能)