SAHI with YOLOv5 for Sliced Inference

inference_for_yolov5.ipynb - Colab

bash 复制代码
# arrange an instance segmentation model for test
import torch

# 保存原始函数
_original_torch_load = torch.load

def _patched_torch_load(*args, **kwargs):
    # 强制设置 weights_only=False
    kwargs['weights_only'] = False
    return _original_torch_load(*args, **kwargs)

# 替换 torch.load
torch.load = _patched_torch_load

from IPython.display import Image

# import required functions, classes
from sahi import AutoDetectionModel
from sahi.predict import get_prediction, get_sliced_prediction, predict
from sahi.utils.cv import read_image
from sahi.utils.file import download_from_url
from sahi.utils.yolov5 import download_yolov5s6_model

# download YOLOV5S6 model to 'models/yolov5s6.pt'
yolov5_model_path = "models/yolov5s6.pt"
download_yolov5s6_model(destination_path=yolov5_model_path)

# download test images into demo_data folder
# 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"
# )

detection_model = AutoDetectionModel.from_pretrained(
    model_type="yolov5",
    model_path=yolov5_model_path,
    confidence_threshold=0.3,
    device="cuda:0",  # or 'cuda:0'
)

# result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)

# result.export_visuals(export_dir="demo_data/")

# Image("demo_data/prediction_visual.png")


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

result.export_visuals(export_dir="demo_data/")

Image("demo_data/prediction_sliced.png")


object_prediction_list = result.object_prediction_list
object_prediction_list[0]


print(result.to_coco_annotations()[:3])
print(result.to_coco_predictions(image_id=1)[:3])
print(result.to_imantics_annotations()[:3])
print(result.to_fiftyone_detections()[:3])


model_type = "yolov5"
model_path = yolov5_model_path
model_device = "cuda:0"  # or 'cpu'
model_confidence_threshold = 0.4

slice_height = 256
slice_width = 256
overlap_height_ratio = 0.2
overlap_width_ratio = 0.2

source_image_dir = "demo_data/"

predict(
    model_type=model_type,
    model_path=model_path,
    model_device=model_device,
    model_confidence_threshold=model_confidence_threshold,
    source=source_image_dir,
    slice_height=slice_height,
    slice_width=slice_width,
    overlap_height_ratio=overlap_height_ratio,
    overlap_width_ratio=overlap_width_ratio,
)
相关推荐
MobotStone6 小时前
基于“行为评估”准则评估智能体(一):别只看结果,还要看它“怎么做”
人工智能
艾莉丝努力练剑7 小时前
【AI大模型接入SDK】LLM会话管理模块设计
网络·c++·人工智能·学习·大模型
牛哥带你学代码7 小时前
远程打游戏远控测评
人工智能·智能手机
小此方7 小时前
「C++AI大模型接入SDK」(一) API接入与本地两种方式对比、API Key获取、API报文详解与简单API的构建
开发语言·c++·人工智能
geneculture7 小时前
三体虚拟竞赛的极致推演:AI for Dao
人工智能·ai for science·信息科学·哲学与科学统一性·序位逻辑·ai for maths·ai for logic
Raas10011 小时前
MAI Gateway(魔芋企业级AI网关)技术揭秘:AI网关支持哪些模型?从原理到落地
大数据·人工智能·网关·gateway·mai gateway·企业级产品
豪气的程序猿14 小时前
电商图片工作流怎么选?Lingko AI 对比折叠键盘主图与详情页
人工智能
小刘快学习14 小时前
把 AI 账单拆到部门:企业 AI 网关的精准分账思路
人工智能
米小虾14 小时前
你让监控模型读的思维链,可能是攻击者写好的剧本
人工智能
deepseek2314 小时前
Iris 开源搜索智能体拆解:35B 与 397B 中文仅差 0.3 分,上下文管理胜过堆参数
人工智能·ai agent·开源模型