yolo --- 快速上手

运行前环境查看

yolo settings

包括数据集路径、运行输出路径等

task 和 mode(不是model)

task: 

  • Detect: For identifying and localizing objects or regions of interest in an image or video.
  • Segment: For dividing an image or video into regions or pixels that correspond to different objects or classes.
  • Classify: For predicting the class label of an input image.
  • Pose: For identifying objects and estimating their keypoints in an image or video.
  • OBB: Oriented (i.e. rotated) bounding boxes suitable for satellite or medical imagery.

mode: 

  • Train: For training a YOLO11 model on a custom dataset.
  • Val: For validating a YOLO11 model after it has been trained.
  • Predict: For making predictions using a trained YOLO11 model on new images or videos.
  • Export: For exporting a YOLO11 model to a format that can be used for deployment.
  • Track: For tracking objects in real-time using a YOLO11 model.
  • Benchmark: For benchmarking YOLO11 exports (ONNX, TensorRT, etc.) speed and accuracy.

运行方式 - 命令行

下载好 ultralytics 项目并安装好 ultralytics 项目后,可以直接使用命令行(Command Line Interface, CLI)进行快速推理一张图片、视频、视频流、摄像头等等,举个例子:

yolo 任务名称 model=本地模型权重路径 source=图片路径

yolo predict model=yolo11n.pt source='https://ultralytics.com/images/bus.jpg'

运行方式 - Python 脚本

模型训练

模型配置文件路径:

数据集配置文件路径:

from ultralytics import YOLO
import multiprocessing

def main():
    # ---------- 加载模型 ----------
    model = YOLO('yolo11n.yaml')

    # ---------- 模型预测 ----------
    model.train(
        data='cs2.yaml',
        epochs=3
    )

if __name__ == '__main__':
    # 如果在 Windows 上运行且需要多进程支持,保留以下行
    multiprocessing.freeze_support()
    main()

模型验证

当我们训练得到一个 .pt 文件后,可能需要对其进行评估以获取该 .pt 的指标,代码如下:

from ultralytics import YOLO

# ---------- 加载模型 ----------
model = YOLO('runs/detect/train3/weights/best.pt')  

# ---------- 模型评估 ----------
model.val(data='coco128.yaml')

模型预测

from ultralytics import YOLO
import multiprocessing

def main():
    # ---------- 加载模型 ----------
    model = YOLO('runs/detect/train4/weights/best.pt')

    # ---------- 模型预测 ----------
    model.predict(
        source='datasets/cs2/images/train/001.png',
        save=True
    )

if __name__ == '__main__':
    # 如果在 Windows 上运行且需要多进程支持,保留以下行
    multiprocessing.freeze_support()
    main()

模型导出

from ultralytics import YOLO


# ---------- 加载模型 ----------
model = YOLO('runs/detect/train3/weights/best.pt')  

# ---------- 模型导出 ----------
model.export(format='onnx', simplify=True)

 

posted @ 2024-11-20 22:26  流水灯  阅读(23)  评论(0编辑  收藏  举报