val也可以称为评估,评估我们模型的性能。此模式可用于调整评估时的超参数的模型,以提高其性能。

全部参数表

首先罗列一下官网提供的全部参数。
https://docs.ultralytics.com/modes/val/

KeyValueDescription
dataNonepath to data file, i.e. coco128.yaml
imgsz640image size as scalar or (h, w) list, i.e. (640, 480)
batch16number of images per batch (-1 for AutoBatch)
save_jsonFalsesave results to JSON file
save_hybridFalsesave hybrid version of labels (labels + additional predictions)
conf0.001object confidence threshold for detection
iou0.6intersection over union (IoU) threshold for NMS
max_det300maximum number of detections per image
halfTrueuse half precision (FP16)
deviceNonedevice to run on, i.e. cuda device=0/1/2/3 or device=cpu
dnnFalseuse OpenCV DNN for ONNX inference
plotsFalseshow plots during training
rectFalserectangular val with each batch collated for minimum padding
splitvaldataset split to use for validation, i.e. ‘val’, ‘test’ or ‘train’

受限于博主能力有限。下文的解释,如有问题,欢迎大家指出。
在这里插入图片描述

使用示例代码

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # 加载yolov8n
model = YOLO('path/to/best.pt')  # 也可以加载你自己的模型

# Validate the model
metrics = model.val()  
metrics.box.map    # 查看目标检测 map50-95 的性能
metrics.box.map50  # 查看目标检测 map50 的性能
metrics.box.map75  # 查看目标检测 map75 的性能
metrics.box.maps   # 返回一个列表包含每一个类别的 map50-95 

参数解释

  • data: 这个参数指定了数据文件的路径,比如coco128.yaml,它是一个配置文件,包含了训练数据集的相关信息,比如图像路径、标签等。
    官方的coco128.yaml就是这样,path指定数据集的根目录,train,val指定训练和测试数据集的位置。nc指定类别数目,names指定类别名字。
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017)
# Example usage: python train.py --data coco128.yaml
# parent
# ├── yolov5
# └── datasets
#     └── coco128  ← downloads here


# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../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)

# Classes
nc: 80  # number of classes
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']  # class names


# Download script/URL (optional)
download: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip
  • imgsz: 这个参数决定了输入图像的尺寸大小。可以是一个数字640,表示宽度和高度相等都是640,也可以是一个包含宽度和高度的列表,比如(640, 480)。

  • batch: 这个参数定义了每个训练批次中包含的图像数量。如果设置为-1,系统将自动确定批量大小。

  • save_json: 如果设置为True,训练过程中的结果将保存为JSON文件。这对于后续的结果分析和可视化很有用。

  • save_hybrid: 这个参数决定是否保存标签和额外预测结果的混合版本。这对于一些特定的任务,如目标检测中的多标签预测,可能会很有用。

  • conf: 这个参数是物体检测中的置信度阈值。只有当检测结果中物体的置信度高于这个阈值时,它们才会被视为有效的检测结果。

  • iou: 这个参数是非极大值抑制(NMS)算法中的交并比(IoU)阈值。当多个边界框之间的交并比高于这个阈值时,只保留置信度最高的边界框。

  • max_det: 这个参数限制了每张图像中允许的最大检测数量。它可以帮助控制输出结果的数量,防止出现过多的重复检测。

  • half: 如果设置为True,将使用半精度(FP16)进行模型的训练或推理。半精度可以减少模型占用的显存和计算资源,但可能会影响模型的精度。

  • device: 这个参数指定了模型运行的设备,可以是cuda设备(如cuda device=0/1/2/3)或者是cpu。根据设备的性能和可用性选择合适的选项。

  • dnn: 如果设置为True,将使用OpenCV的DNN模块进行ONNX模型的推理。这可以提供更快的推理速度,但需要安装和配置OpenCV。

  • plots: 如果设置为True,训练过程中会显示一些图表,如损失曲线和精度曲线。这对于监控训练过程和调试模型很有帮助。

  • rect: 这个参数决定在验证过程中是否使用矩形方式将每个批次的图像整合到最小填充区域中。

  • split: 这个参数指定了用于验证的数据集划分,可以是’val’、‘test’或者’train’。根据需要选择适当的划分来评估模型的性能。

在这里插入图片描述

from ultralytics import YOLO
model = YOLO('yolov8n.pt').export(format="onnx")
print(model)

在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐