Ultralytics 文档:将 YOLO26 与 SAHI 结合用于切片推理
欢迎阅读 Ultralytics 关于如何将 YOLO26 与 SAHI(切片辅助超推理)结合使用的文档。本综合指南旨在为你提供实施 SAHI 与 YOLO26 结合所需的所有基础知识。我们将深入探讨什么是 SAHI,为什么切片推理对大规模应用至关重要,以及如何将这些功能与 YOLO26 集成以增强 目标检测 性能。
SAHI 简介
SAHI (Slicing Aided Hyper Inference) 是一款创新库,旨在优化用于大规模和高分辨率图像的目标检测算法。其核心功能在于将图像分割成易于处理的切片,对每个切片运行目标检测,然后将结果拼接回原样。SAHI 与包括 YOLO 系列在内的多种目标检测模型兼容,从而在确保计算资源优化使用的同时提供灵活性。
Watch: Inference with SAHI (Slicing Aided Hyper Inference) using Ultralytics YOLO26
SAHI 的主要功能
- 无缝集成:SAHI 可与 YOLO 模型轻松集成,这意味着你无需进行大量代码修改即可开始切片和检测。
- 资源效率:通过将大型图像分解为更小的部分,SAHI 优化了内存使用,使你能够在资源有限的硬件上运行高质量检测。
- 高 准确度:SAHI 通过使用智能算法在拼接过程中合并重叠的检测框,从而保持检测精度。
什么是切片推理?
切片推理是指将大尺寸或高分辨率图像细分为更小的片段(切片),对这些切片进行目标检测,然后重新编译这些切片以在原始图像上重建对象位置的实践。该技术在计算资源受限或处理可能导致内存问题的超高分辨率图像时非常有用。
切片推理的优势
-
降低计算负担:较小的图像切片处理速度更快,消耗的内存更少,从而实现低端硬件上的更顺畅操作。
-
保持检测质量:由于每个切片都是独立处理的,只要切片足够大以捕获感兴趣的对象,目标检测的质量就不会降低。
-
增强可扩展性:该技术允许更轻松地将目标检测扩展到不同尺寸和分辨率的图像,使其成为从卫星图像到医学诊断等广泛应用场景的理想选择。
| YOLO26 without SAHI | YOLO26 with SAHI |
|---|---|
![]() | ![]() |
安装与准备
安装
首先,安装最新版本的 SAHI 和 Ultralytics:
pip install -U ultralytics sahi导入模块并下载资源
以下是如何下载一些测试图像:
from sahi.utils.file import download_from_url
# 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",
)使用 YOLO26 进行标准推理
实例化模型
你可以像这样实例化一个 YOLO26 模型进行目标检测:
from sahi import AutoDetectionModel
detection_model = AutoDetectionModel.from_pretrained(
model_type="ultralytics",
model_path="yolo26n.pt",
confidence_threshold=0.3,
device="cpu", # or 'cuda:0'
)执行标准预测
使用图像路径执行标准推理。
from sahi.predict import get_prediction
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)
result.export_visuals(export_dir="demo_data/", hide_conf=True)可视化结果
导出并可视化预测的边界框和掩码:
from PIL import Image
# Open the predicted image
processed_image = Image.open("demo_data/prediction_visual.png")
# Display the predicted image
processed_image.show()使用 YOLO26 进行切片推理
通过指定切片尺寸和重叠率来执行切片推理:
from PIL import Image
from sahi.predict import get_sliced_prediction
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,
)
# Export results
result.export_visuals(export_dir="demo_data/", hide_conf=True)
# Open the predicted image
processed_image = Image.open("demo_data/prediction_visual.png")
# Display the predicted image
processed_image.show()处理预测结果
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]批量预测
用于对图像目录进行批量预测:
from sahi.predict import predict
predict(
model_type="ultralytics",
model_path="yolo26n.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,
)现在你已准备好将 YOLO26 与 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 仓库.
FAQ
如何将 YOLO26 与 SAHI 集成以在目标检测中进行切片推理?
将 Ultralytics YOLO26 与 SAHI(切片辅助超推理)集成进行切片推理,可以通过将高分辨率图像划分为易于管理的切片来优化你的目标检测任务。此方法可改善内存使用并确保高检测精度。要开始使用,你需要安装 ultralytics 和 sahi 库:
pip install -U ultralytics sahi然后,下载测试图像:
from sahi.utils.file import download_from_url
# 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",
)有关更详细的说明,请参考我们的 切片推理指南.
为什么我应该在大型图像的目标检测中使用 SAHI 与 YOLO26?
将 SAHI 与 Ultralytics YOLO26 结合使用于大型图像的目标检测具有多项优势:
- 降低计算负担:较小的切片处理速度更快,消耗的内存更少,从而能够在资源有限的硬件上运行高质量检测。
- 保持检测精度:SAHI 使用智能算法来合并重叠的框,从而保持检测质量。
- 增强可扩展性:通过在不同图像尺寸和分辨率下扩展目标检测任务,SAHI 成为了卫星图像分析和医学诊断等各种应用的理想选择。
了解更多关于 切片推理的好处 请参阅我们的文档。
将 YOLO26 与 SAHI 结合使用时,可以可视化预测结果吗?
是的,将 YOLO26 与 SAHI 结合使用时,你可以可视化预测结果。以下是你如何导出并可视化结果的方法:
from PIL import Image
result.export_visuals(export_dir="demo_data/", hide_conf=True)
processed_image = Image.open("demo_data/prediction_visual.png")
processed_image.show()此命令会将可视化后的预测结果保存到指定目录,然后你可以加载图像以在笔记本或应用程序中查看它。有关详细指南,请查看 标准推理部分.
SAHI 提供了哪些功能来改进 YOLO26 目标检测?
SAHI(切片辅助超推理)提供了多项功能,可以补充 Ultralytics YOLO26 的目标检测能力:
- 无缝集成:SAHI 与 YOLO 模型轻松集成,仅需最少的代码调整。
- 资源效率:它将大型图像分割为较小的切片,从而优化内存使用和速度。
- 高准确性:通过在拼接过程中有效地合并重叠的检测框,SAHI 保持了高检测精度。
要深入了解,请阅读关于 SAHI 的 主要功能.
如何使用 YOLO26 和 SAHI 处理大规模推理项目?
要使用 YOLO26 和 SAHI 处理大规模推理项目,请遵循以下最佳实践:
- 安装所需库:确保你拥有最新版本的 ultralytics 和 sahi。
- 配置切片推理:确定适合你特定项目的最佳切片尺寸和重叠率。
- 运行批量预测:使用 SAHI 的功能对图像目录执行批量预测,这可以提高效率。
批量预测示例:
from sahi.predict import predict
predict(
model_type="ultralytics",
model_path="path/to/yolo26n.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,
)有关更详细的步骤,请访问我们关于 批量预测.

