fix(annotators): avoid divide-by-zero in HeatMapAnnotator on empty detections#2269
Merged
Borda merged 3 commits intoMay 26, 2026
Merged
Conversation
…tections When HeatMapAnnotator is called on a fresh annotator with empty detections (common on the first frames of a video before the model produces any output), self.heat_mask is all zeros, so temp / temp.max() raises RuntimeWarning: invalid value encountered in divide and produces nan/inf in-flight. Skip the normalisation when temp.max() == 0; the resulting all-zero heat mask filters out via the > 0 check below, so the scene is returned unchanged.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a RuntimeWarning: invalid value encountered in divide in HeatMapAnnotator.annotate(...) when called with empty detections on a fresh annotator, ensuring the scene is returned unchanged without emitting warnings.
Changes:
- Guard heatmap normalization to avoid dividing by zero when the heatmask maximum is
0. - Add a regression test that converts
RuntimeWarningto an error and verifies empty detections do not warn and do not modify the scene.
Quality Assessment (n/5):
- Code quality: 4/5
- Testing: 5/5
- Documentation: 5/5
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/supervision/annotators/core.py |
Skips normalization when temp.max() == 0 to prevent divide-by-zero warnings on empty heat masks. |
tests/annotators/test_core.py |
Adds a regression test ensuring HeatMapAnnotator does not warn and returns an unchanged image on empty detections. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2269 +/- ##
=======================================
Coverage 78% 78%
=======================================
Files 66 66
Lines 8410 8412 +2
=======================================
+ Hits 6552 6580 +28
+ Misses 1858 1832 -26 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
Author
|
Thanks for the autofix, the regression test still passes locally. |
…or coverage - Fix `kernel_size: int = 25` → `int | None = 25`; document None disables blur - Add Note to annotate docstring: empty detections returns scene unchanged - Add happy path test: single detection must produce visible heat output - Add stateful tests: empty→real and real→empty sequence coverage /review findings by foundry:linting-expert, foundry:doc-scribe, foundry:qa-specialist (report: .reports/review/2026-05-26T19-42-53Z/review-report.md) --- Co-authored-by: Claude Code <noreply@anthropic.com>
Borda
approved these changes
May 26, 2026
Borda
added a commit
to Ace3Z/supervision
that referenced
this pull request
May 27, 2026
…e-dtype Bring PR branch up to date with upstream develop (HeatMapAnnotator divide-by-zero fix, roboflow#2269). --- Co-authored-by: Claude Code <noreply@anthropic.com>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
HeatMapAnnotator.annotate(...)on a fresh annotator with empty detections (e.g. the first few frames of a video before the model produces output) raisesRuntimeWarning: invalid value encountered in divide.self.heat_maskis all zeros, sotemp / temp.max()divides by zero, and the resultingnan/infflows through the rest of the function.Minimal repro:
Skip the normalisation step when
temp.max() == 0; the resulting all-zerotempfilters out via theself.heat_mask.astype(np.uint8) > 0mask check below, so the scene is returned unchanged.tests/annotators/test_core.py::TestHeatMapAnnotator::test_annotate_with_no_detections_does_not_warncovers the regression: it convertsRuntimeWarningto an error and asserts the call succeeds with the scene unchanged.