Ultralytics 文档:使用YOLOv8 和 SAHI 进行切片推理
欢迎访问Ultralytics 文档,了解如何使用YOLOv8 和SAHI(切片辅助超推理)。本综合指南旨在为您提供在YOLOv8 的同时实施 SAHI 所需的全部基本知识。我们将深入探讨什么是 SAHI、为什么切片推理对大规模应用至关重要,以及如何将这些功能与YOLOv8 集成以提高对象检测性能。
SAHI 简介
SAHI(切片辅助超推理)是一个创新库,旨在优化大规模高分辨率图像的物体检测算法。其核心功能在于将图像分割成易于管理的切片,在每个切片上运行物体检测,然后将结果拼接在一起。SAHI 与包括YOLO 系列在内的一系列物体检测模型兼容,从而在确保优化使用计算资源的同时提供了灵活性。
SAHI 的主要特点
- 无缝集成:SAHI 可毫不费力地与YOLO 模型集成,这意味着您无需修改大量代码即可开始切片和检测。
- 资源效率:通过将大图像分解成较小的部分,SAHI 可优化内存使用,让您在资源有限的硬件上运行高质量的检测。
- 高精度:SAHI 采用智能算法,在拼接过程中合并重叠的检测框,从而保持检测精度。
什么是切片推理?
切片推理是指将大尺寸或高分辨率图像细分为较小的片段(切片),在这些切片上进行物体检测,然后重新编译切片以重建原始图像上的物体位置。在计算资源有限的情况下,或者在处理极高分辨率图像(否则可能导致内存问题)时,这种技术非常宝贵。
切片推理的优势
-
减轻计算负担:较小的图像切片处理速度更快,内存消耗更少,可在低端硬件上更流畅地运行。
-
保持检测质量:由于每个切片都是独立处理的,因此只要切片足够大,能够捕捉到感兴趣的物体,物体检测的质量就不会降低。
-
增强的可扩展性:该技术可以更容易地在不同尺寸和分辨率的图像上进行物体检测,因此非常适合从卫星图像到医疗诊断的广泛应用。
YOLOv8 无 SAHI | YOLOv8 与 SAHI |
---|---|
安装和准备
安装
要开始使用,请安装最新版本的 SAHI 和Ultralytics :
导入模块和下载资源
下面介绍如何导入必要的模块并下载YOLOv8 模型和一些测试图像:
from sahi.utils.yolov8 import download_yolov8s_model
from sahi import AutoDetectionModel
from sahi.utils.cv import read_image
from sahi.utils.file import download_from_url
from sahi.predict import get_prediction, get_sliced_prediction, predict
from pathlib import Path
from IPython.display import Image
# Download YOLOv8 model
yolov8_model_path = "models/yolov8s.pt"
download_yolov8s_model(yolov8_model_path)
# Download test images
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')
标准推理与YOLOv8
实例化模型
您可以像这样实例化YOLOv8 模型,用于对象检测:
detection_model = AutoDetectionModel.from_pretrained(
model_type='yolov8',
model_path=yolov8_model_path,
confidence_threshold=0.3,
device="cpu", # or 'cuda:0'
)
执行标准预测
使用图像路径或 numpy 图像执行标准推理。
# With an image path
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)
# With a numpy image
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)
可视化结果
导出并可视化预测的边界框和遮罩:
切片推理与YOLOv8
通过指定切片尺寸和重叠率来执行切片推理:
result = get_sliced_prediction(
"demo_data/small-vehicles1.jpeg",
detection_model,
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2
)
处理预测结果
SAHI 提供了一个 PredictionResult
对象,可以转换成各种注释格式:
# Access the object prediction list
object_prediction_list = result.object_prediction_list
# Convert to COCO annotation, COCO prediction, imantics, and fiftyone formats
result.to_coco_annotations()[:3]
result.to_coco_predictions(image_id=1)[:3]
result.to_imantics_annotations()[:3]
result.to_fiftyone_detections()[:3]
批量预测
用于对图像目录进行批量预测:
predict(
model_type="yolov8",
model_path="path/to/yolov8n.pt",
model_device="cpu", # or 'cuda:0'
model_confidence_threshold=0.4,
source="path/to/dir",
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
就是这样!现在,你可以使用YOLOv8 和 SAHI 进行标准推断和切片推断了。
引文和致谢
如果您在研究或开发工作中使用了 SAHI,请引用 SAHI 原文并向作者致谢:
@article{akyon2022sahi,
title={Slicing Aided Hyper Inference and Fine-tuning for Small Object Detection},
author={Akyon, Fatih Cagatay and Altinuc, Sinan Onur and Temizel, Alptekin},
journal={2022 IEEE International Conference on Image Processing (ICIP)},
doi={10.1109/ICIP46576.2022.9897990},
pages={966-970},
year={2022}
}
我们对 SAHI 研究小组为计算机视觉社区创建和维护这一宝贵资源表示感谢。有关 SAHI 及其创建者的更多信息,请访问SAHI GitHub 存储库。