Ultralytics Docs: Using YOLO11 with SAHI for Sliced Inference
Welcome to the Ultralytics documentation on how to use YOLO11 with SAHI (Slicing Aided Hyper Inference). This comprehensive guide aims to furnish you with all the essential knowledge you'll need to implement SAHI alongside YOLO11. We'll deep-dive into what SAHI is, why sliced inference is critical for large-scale applications, and how to integrate these functionalities with YOLO11 for enhanced object detection performance.
SAHI 简介
SAHI(切片辅助超推理)是一个创新库,旨在优化大规模高分辨率图像的物体检测算法。其核心功能在于将图像分割成易于管理的切片,在每个切片上运行物体检测,然后将结果拼接在一起。SAHI 与包括YOLO 系列在内的一系列物体检测模型兼容,从而在确保优化使用计算资源的同时提供了灵活性。
观看: Inference with SAHI (Slicing Aided Hyper Inference) using Ultralytics YOLO11
SAHI 的主要特点
- 无缝集成:SAHI 可毫不费力地与YOLO 模型集成,这意味着您无需修改大量代码即可开始切片和检测。
- 资源效率:通过将大图像分解成较小的部分,SAHI 可优化内存使用,让您在资源有限的硬件上运行高质量的检测。
- High Accuracy: SAHI maintains the detection accuracy by employing smart algorithms to merge overlapping detection boxes during the stitching process.
什么是切片推理?
切片推理是指将大尺寸或高分辨率图像细分为较小的片段(切片),在这些切片上进行物体检测,然后重新编译切片以重建原始图像上的物体位置。在计算资源有限的情况下,或者在处理极高分辨率图像(否则可能导致内存问题)时,这种技术非常宝贵。
切片推理的优势
-
减轻计算负担:较小的图像切片处理速度更快,内存消耗更少,可在低端硬件上更流畅地运行。
-
保持检测质量:由于每个切片都是独立处理的,因此只要切片足够大,能够捕捉到感兴趣的物体,物体检测的质量就不会降低。
-
增强的可扩展性:该技术可以更容易地在不同尺寸和分辨率的图像上进行物体检测,因此非常适合从卫星图像到医疗诊断的广泛应用。
YOLO11 without SAHI | YOLO11 with SAHI |
---|---|
安装和准备
安装
要开始使用,请安装最新版本的 SAHI 和Ultralytics :
导入模块和下载资源
Here's how to import the necessary modules and download a YOLO11 model and some test images:
from sahi.utils.file import download_from_url
from sahi.utils.yolov8 import download_yolov8s_model
# Download YOLO11 model
model_path = "models/yolo11s.pt"
download_yolov8s_model(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",
)
Standard Inference with YOLO11
实例化模型
You can instantiate a YOLO11 model for object detection like this:
from sahi import AutoDetectionModel
detection_model = AutoDetectionModel.from_pretrained(
model_type="yolov8",
model_path=yolov8_model_path,
confidence_threshold=0.3,
device="cpu", # or 'cuda:0'
)
执行标准预测
使用图像路径或 numpy 图像执行标准推理。
from sahi.predict import get_prediction
# 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)
可视化结果
导出并可视化预测的边界框和遮罩:
Sliced Inference with YOLO11
通过指定切片尺寸和重叠率来执行切片推理:
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,
)
处理预测结果
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="yolov8",
model_path="path/to/yolo11n.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,
)
That's it! Now you're equipped to use YOLO11 with SAHI for both standard and sliced inference.
引文和致谢
如果您在研究或开发工作中使用了 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}
}
We extend our thanks to the SAHI research group for creating and maintaining this invaluable resource for the computer vision community. For more information about SAHI and its creators, visit the SAHI GitHub repository.
常见问题
How can I integrate YOLO11 with SAHI for sliced inference in object detection?
Integrating Ultralytics YOLO11 with SAHI (Slicing Aided Hyper Inference) for sliced inference optimizes your object detection tasks on high-resolution images by partitioning them into manageable slices. This approach improves memory usage and ensures high detection accuracy. To get started, you need to install the ultralytics and sahi libraries:
Then, download a YOLO11 model and test images:
from sahi.utils.file import download_from_url
from sahi.utils.yolov8 import download_yolov8s_model
# Download YOLO11 model
model_path = "models/yolo11s.pt"
download_yolov8s_model(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",
)
有关更详细的说明,请参阅我们的切片推理指南。
Why should I use SAHI with YOLO11 for object detection on large images?
Using SAHI with Ultralytics YOLO11 for object detection on large images offers several benefits:
- 减轻计算负担:较小的切片处理速度更快,内存消耗更少,因此可以在资源有限的硬件上运行高质量的检测。
- 保持检测精度:SAHI 采用智能算法合并重叠的方框,从而保持检测质量。
- 增强的可扩展性:通过在不同尺寸和分辨率的图像上扩展目标检测任务,SAHI 成为卫星图像分析和医疗诊断等各种应用的理想选择。
在我们的文档中了解更多切片推理的优势。
Can I visualize prediction results when using YOLO11 with SAHI?
Yes, you can visualize prediction results when using YOLO11 with SAHI. Here's how you can export and visualize the results:
from IPython.display import Image
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")
该命令会将可视化预测结果保存到指定目录,然后您就可以在笔记本或应用程序中加载图像进行查看。有关详细指南,请查看标准推理部分。
What features does SAHI offer for improving YOLO11 object detection?
SAHI (Slicing Aided Hyper Inference) offers several features that complement Ultralytics YOLO11 for object detection:
- 无缝集成:SAHI 可与YOLO 模型轻松集成,只需对代码进行最少的调整。
- 资源效率:它能将大图像分割成较小的片段,从而优化内存使用和速度。
- 高精度:通过在拼接过程中有效合并重叠的检测框,SAHI 可保持较高的检测精度。
如需更深入了解,请阅读 SAHI 的主要特点。
How do I handle large-scale inference projects using YOLO11 and SAHI?
To handle large-scale inference projects using YOLO11 and SAHI, follow these best practices:
- 安装所需的库:确保有最新版本的ultralytics 和 sahi。
- 配置切片推理:确定特定项目的最佳切片尺寸和重叠率。
- 运行批量预测:利用 SAHI 的功能对图像目录进行批量预测,从而提高效率。
批量预测示例
from sahi.predict import predict
predict(
model_type="yolov8",
model_path="path/to/yolo11n.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,
)
更多详细步骤,请访问我们的批量预测部分。