Skip to content

Add out-of-bounds detection warning to InferenceSlicer#2186

Merged
Borda merged 13 commits intodevelopfrom
copilot/fix-repeat-detections-inferenceslicer
Mar 30, 2026
Merged

Add out-of-bounds detection warning to InferenceSlicer#2186
Borda merged 13 commits intodevelopfrom
copilot/fix-repeat-detections-inferenceslicer

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 30, 2026

When a user's callback accidentally runs inference on the full image instead of the provided slice, supervision blindly applies slice offsets to coordinates already in full-image space — producing the same detections tiled across a grid with only N unique confidence values for N×slices total detections.

The root cause is a common callback authoring mistake:

# BUG: uses `image` (full image) instead of `image_slice`
def callback(image_slice: np.ndarray) -> sv.Detections:
    result = model.infer(image)[0]
    return sv.Detections.from_inference(result)

Changes

  • InferenceSlicer._run_callback: After invoking the user callback, checks whether any returned detection coordinate exceeds the slice dimensions or is negative. Either condition indicates the callback is operating on the wrong image. Emits a SupervisionWarnings with an actionable message pointing to the likely cause.
  • InferenceSlicer.__init__: Adds _out_of_slice_bounds_warned flag so the warning fires at most once per slicer instance, avoiding log spam across hundreds of slices.
  • Tests: Covers warning on x/y overflow, warning on negative coordinates, single-emission across multiple slices, and no false positives for well-behaved callbacks.

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI linked an issue Mar 30, 2026 that may be closed by this pull request
2 tasks
…etections bug

When a user's callback accidentally runs inference on the full image instead
of the provided slice, detections get incorrect offsets applied, causing a
repeating grid pattern. Add a validation check in _run_callback that emits a
SupervisionWarnings warning when any detection coordinate exceeds the slice
dimensions or is negative. An instance flag prevents repeated warnings across
many slices.

Agent-Logs-Url: https://github.com/roboflow/supervision/sessions/ee0b16e9-1f0a-4237-ac89-05037c075366

Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix repeat detections with InferenceSlicer Add out-of-bounds detection warning to InferenceSlicer Mar 30, 2026
Copilot AI requested a review from Borda March 30, 2026 08:06
Borda and others added 5 commits March 30, 2026 17:15
- Wrap _out_of_slice_bounds_warned check-and-set in threading.Lock to prevent duplicate warnings under ThreadPoolExecutor with thread_workers > 1
- Change stacklevel=2 to stacklevel=1 — under executor.submit the stacklevel=2 frame points into concurrent.futures internals, not user code

---
Co-authored-by: Claude Code <noreply@anthropic.com>
…ests

- Assert exactly 1 warning fires with thread_workers=4 (validates Lock fix)
- Assert no warning for detection touching but not exceeding slice boundary (pins > vs >= semantics)
- Assert second slicer call does not re-warn (documents once-per-instance semantic)

---
Co-authored-by: Claude Code <noreply@anthropic.com>
---
Co-authored-by: Claude Code <noreply@anthropic.com>
---
Co-authored-by: Claude Code <noreply@anthropic.com>
@Borda Borda marked this pull request as ready for review March 30, 2026 15:27
@Borda Borda requested a review from SkalskiP as a code owner March 30, 2026 15:27
Copilot AI review requested due to automatic review settings March 30, 2026 15:27
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77%. Comparing base (8b60ee1) to head (5a9c811).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2186   +/-   ##
=======================================
+ Coverage       76%     77%   +1%     
=======================================
  Files           62      62           
  Lines         7575    7592   +17     
=======================================
+ Hits          5743    5810   +67     
+ Misses        1832    1782   -50     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a safety warning to sv.InferenceSlicer to help users detect a common callback mistake where inference is run on the full image instead of the provided slice, which would otherwise produce incorrectly offset/tiled detections.

Changes:

  • Add a once-per-instance out-of-slice-bounds check after callback execution, emitting SupervisionWarnings when detection coordinates are negative or exceed slice dimensions.
  • Add thread-safe “warned already” state to prevent warning spam across many slices / multiple threads.
  • Add unit tests covering overflow/negative coordinates, single-emission behavior (including multithreaded), and no false positives.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/supervision/detection/tools/inference_slicer.py Adds the bounds-check warning logic and a lock/flag to ensure it fires at most once per slicer instance.
tests/detection/tools/test_inference_slicer.py Adds tests validating warning emission behavior and non-emission for in-bounds detections.
docs/changelog.md Documents the new warning behavior in the changelog.

Comment thread src/supervision/detection/tools/inference_slicer.py Outdated
Comment thread src/supervision/detection/tools/inference_slicer.py Outdated
Comment thread docs/changelog.md Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread src/supervision/detection/tools/inference_slicer.py Outdated
Comment thread docs/changelog.md Outdated
Borda and others added 2 commits March 30, 2026 17:37
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
- Fix SyntaxError: `stacklevel=,` (missing value) → `stacklevel=2`, consistent with other warnings in the codebase
- Extract warning message into `msg` variable to satisfy E501 line-length limit

---
Co-authored-by: Claude Code <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comment thread src/supervision/detection/tools/inference_slicer.py Outdated
Borda and others added 2 commits March 30, 2026 19:18
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
@Borda Borda merged commit e6fab4b into develop Mar 30, 2026
27 checks passed
@Borda Borda deleted the copilot/fix-repeat-detections-inferenceslicer branch March 30, 2026 17:26
@Borda Borda mentioned this pull request Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repeat detections with InferenceSlicer

3 participants