supervision
supervision copied to clipboard
Getting segmentation fault when running InferenceSlicer with OBB model with thread_workers > 1
Search before asking
- [X] I have searched the Supervision issues and found no similar bug report.
Bug
InferenceSlicer throws Segmentation fault with thread_workers = 4:
Segmentation fault (core dumped)
Environment
Supervision 0.24.0 Python 3.11
Minimal Reproducible Example
from ultralytics import YOLO
import cv2
import sys
import torch
import numpy as np
from pathlib import Path
import math
import supervision as sv
def callback(image_slice: np.ndarray) -> sv.Detections:
result = model(image_slice, conf=0.6)
print("Results:", result)
return sv.Detections.from_ultralytics(result[0])
model = YOLO('yolo11x-obb.pt')
print("GPU:", torch.cuda.is_available())
if torch.cuda.is_available():
device = torch.device("cuda")
print("Using GPU:", torch.cuda.get_device_name())
# load image
file_name = Path(sys.argv[1])
image = cv2.imread(sys.argv[1]) #Image.open(file_name, mode='r')
print("Loaded image:", file_name)
image_wh = (image.shape[1], image.shape[0])
slice_wh = (1024, 1024)
overlap_ratio_wh = (0.2, 0.2)
overlap_ratio_w, overlap_ratio_h = overlap_ratio_wh
slice_w, slice_h = slice_wh
overlap_wh = (math.ceil(slice_w * overlap_ratio_w), math.ceil(slice_h * overlap_ratio_h))
slicer = sv.InferenceSlicer(
callback=callback,
overlap_filter=sv.OverlapFilter.NON_MAX_MERGE,
iou_threshold=0.15,
slice_wh=slice_wh,
overlap_ratio_wh=None,
overlap_wh=overlap_wh,
thread_workers=2
)
detections = slicer(image)
labels = [
f"{class_name} {confidence:.1f}"
for class_name, confidence
in zip(detections['class_name'], detections.confidence)
]
label_annotator = sv.LabelAnnotator(text_scale=0.2, text_thickness=1, text_padding=0, text_position=sv.Position.TOP_LEFT)
bbox_annotator = sv.BoxAnnotator(color=sv.ColorPalette.DEFAULT.colors[6], thickness=2)
obb_annotator = sv.OrientedBoxAnnotator(color=sv.ColorPalette.DEFAULT.colors[6], thickness=2)
print(f"Image shape: {image_wh[0]}w x {image_wh[1]}h")
print(f"Tile size: {slice_wh[0]}w x {slice_wh[1]}h")
print(f"Overlap: {overlap_wh[0]}w x {overlap_wh[1]}h. Ratio {overlap_ratio_wh}")
print(f"Overlap Filter: {sv.OverlapFilter.NON_MAX_MERGE}")
print(f"Found {len(detections)} objects")
annotated_image = obb_annotator.annotate(scene=image.copy(), detections=detections)
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections, labels=labels)
cv2.imwrite(file_name.stem + "-output.jpg", annotated_image)

Additional
To reproduce:
python detect-image-slicer-bug.py airplane-graveyard-zoomed.jpg
.jpg
Are you willing to submit a PR?
- [ ] Yes I'd like to help by submitting a PR!
Hi @zxsk1974 👋
Thank you for reporting it. Unfortunately, many models aren't made to be run from multiple threads. We plan to shift to running in batches (linked PR), but it is unfortunately relatively low on the priority list.
@LinasKo batching will work too, can I try it on pre-release branch?
Not as a pre-release branch. I don't expect to merge this any time soon.
However, I brought the branch up-to-date with the latest supervision version.
Feel free to install via
pip install git+https://github.com/LinasKo/supervision.git@feature/batched-inference-slicer.
Alternatively, you may fork it from my repo. Install it in the same way, but from your own namespace.
Let us know if it works - that'd give more reason to revisit the PR 🙂
Interesting, but sometimes the Slicer with 2 threads works till the end of detection and than throws the error, but not segmentation fault. This is still released version of SV. The error:
Traceback (most recent call last):
File "/home/sergey/Workspace/detection-tiling-custom-obb/detect-image-slicer.py", line 89, in
Could be an ultralytics version issue. Unless you need yolo11, I'd try ultralytics==8.2.103.
@LinasKo apologies for the delay - I got pulled into another area of the project. I just tried your branch build and it worked very well. I observed 25-30% speed up from batch size 1 to batch size 10, than almost no speedup up to batch size 100+, since GPU utilization became bottle neck. I noticed that your branch was forked from 0.24.0 version, so if you merge 0.25.1 changes into your branch, I can test it again. Please let me know when it will merged into release branch. Thank you again.
Hi @zxsk1974 👋🏻 no worries! Which version of the code you just tested?
From this branch - batched-inference-slicer: pip install git+https://github.com/LinasKo/supervision.git@feature/batched-inference-slicer.
@LinasKo please let me know what version of SV it will be.
@LinasKo , @SkalskiP - apparently this feature didn't make it to 0.25.1. Please let me know what version it will be released.