Ultralytics YOLO27:

结合使用 YOLO26 与 SAHI 进行切片推理#

Open SAHI for Sliced Inference In Colab

本指南展示了如何结合使用 YOLO26 与 SAHI(切片辅助超推理)。我们将深入探讨什么是 SAHI、为什么切片推理对于大规模应用至关重要,以及如何将这些功能与 YOLO26 集成以获得增强的目标检测性能。

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()

    此命令会将可视化预测结果保存到指定目录,然后你可以加载图像,在 notebook 或应用程序中查看。详细指南请参阅标准推理部分

  • 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,
    )

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

评论