YOLO Vision 2026:

Ultralytics 文档:使用 YOLO26 和 SAHI 进行切片推理#

在 Colab 中打开用于切片推理的 SAHI

欢迎阅读 Ultralytics 关于如何将 YOLO26 与 SAHI(Slicing Aided Hyper Inference)结合使用的文档。本全面指南旨在为你提供实现 SAHI 与 YOLO26 配合使用所需的所有基础知识。我们将深入探讨什么是 SAHI、为什么切片推理对大规模应用至关重要,以及如何将这些功能与 YOLO26 集成以增强目标检测性能。

SAHI tiled inference for small objects

SAHI 简介#

SAHI (切片辅助超推理) 是一款创新库,旨在优化大规模和高分辨率图像的目标检测算法。其核心功能在于将图像分割成可处理的切片,在每个切片上运行目标检测,然后将结果拼接在一起。SAHI 兼容多种目标检测模型,包括 YOLO 系列,从而在确保优化计算资源利用率的同时提供灵活性。



Watch: How to use SAHI with Ultralytics YOLO26 to Detect Small Objects | Slicing Aided Hyper Inference 🚀

SAHI 的主要特性#

  • 无缝集成:SAHI 可以与 YOLO 模型轻松集成,这意味着你无需进行大量代码修改即可开始切片和检测。
  • 资源效率:通过将大图像分解为较小的部分,SAHI 优化了内存使用,使你能够在资源受限的硬件上进行高质量检测。
  • 准确率:SAHI 在拼接过程中采用智能算法合并重叠的检测框,从而维持检测准确率。

什么是切片推理?#

切片推理是指将大图像或高分辨率图像细分为较小片段 (切片)、对这些切片进行目标检测,然后重新编译切片以重构原始图像上的目标位置的实践。该技术在计算资源有限或处理可能导致内存问题的超高分辨率图像时非常宝贵。

切片推理的优势#

  • 降低计算负担:较小的图像切片处理速度更快,消耗的内存更少,从而在低端硬件上实现更顺畅的运行。

  • 保持检测质量:由于每个切片都是独立处理的,只要切片足够大以捕捉目标物体,目标检测的质量就不会降低。

  • 增强可扩展性:该技术允许更容易地跨不同尺寸和分辨率的图像扩展目标检测,使其成为从卫星图像到医疗诊断等各种应用的理想选择。

YOLO26 without SAHIYOLO26 with SAHI
YOLO26 without SAHIYOLO26 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 and COCO prediction formats
result.to_coco_annotations()[:3]
result.to_coco_predictions(image_id=1)[:3]
其他导出格式

PredictionResult 还可以使用 result.to_imantics_annotations()result.to_fiftyone_detections() 将检测结果转换为 imanticsFiftyOne 对象。这些方法需要相应的软件包,因此请先使用 pip install imantics fiftyone 进行安装。

批量预测#

对于图像目录进行批量预测:

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 仓库

常见问题解答#

  • 将 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 与 Ultralytics YOLO26 结合使用进行大图像目标检测具有以下几个好处:

    • 降低计算负担:较小的切片处理速度更快,消耗的内存更少,使得在资源受限的硬件上进行高质量检测变得可行。
    • 保持检测准确度:SAHI 使用智能算法合并重叠的检测框,从而保持了检测质量。
    • 增强可扩展性:通过跨不同图像尺寸和分辨率扩展目标检测任务,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 (切片辅助超推理) 提供了多项功能,可以作为 Ultralytics YOLO26 目标检测的补充:

    • 无缝集成:SAHI 可以轻松与 YOLO 模型集成,仅需极少的代码调整。
    • 资源效率:它将大图像划分为较小的切片,这优化了内存使用和速度。
    • 高准确度:通过在拼接过程中有效合并重叠的检测框,SAHI 保持了高检测准确度。

    为了更深入地理解,请阅读有关 SAHI 核心功能的内容。

  • 要使用 YOLO26 和 SAHI 处理大规模推理项目,请遵循以下最佳实践:

    1. 安装所需库:确保拥有最新版本的 ultralytics 和 sahi。
    2. 配置切片推理:针对你的具体项目确定最佳切片尺寸和重叠比例。
    3. 运行批量预测:利用 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,
    )

    有关更详细的步骤,请访问我们的批量预测部分。

评论