Skip to content

Conversation

@souhhmm
Copy link
Contributor

@souhhmm souhhmm commented Nov 30, 2024

Hopefully fixes #1694.

Test code:

import supervision as sv
import numpy as np

CLASSES = [0, 1, 2]

prediction = sv.Detections.empty()
prediction = prediction[np.isin(prediction["class_name"], CLASSES)]

print(prediction)

New output:

Detections(xyxy=array([], shape=(0, 4), dtype=float32), mask=None, confidence=array([], dtype=float32), class_id=array([], dtype=int64), tracker_id=None, data={}, metadata={})

@LinasKo
Copy link
Contributor

LinasKo commented Dec 3, 2024

I agree with the solution, though I'll move it up.

It happens due to a chain of events:

  • getitem for "class_name" returns None.
  • np.isin returns an array of shape () with value False.
  • __getitem__ is called, numpy uses this array to index into Detections, but since detections are empty, it expands the dimensions instead.

# Inside __getitem__
print(self)
print(f"{repr(index)=}")
print(f"{repr(self.xyxy)=}")
print(f"{repr(self.xyxy[index])=}")
Detections(xyxy=array([], shape=(0, 4), dtype=float32), mask=None, confidence=array([], dtype=float32), class_id=array([], dtype=int64), tracker_id=None, data={}, metadata={})
repr(index)='array(False)'
repr(self.xyxy)='array([], shape=(0, 4), dtype=float32)'
repr(self.xyxy[index])='array([], shape=(0, 0, 4), dtype=float32)'

Treating the detections as a special case will do for now.

@LinasKo
Copy link
Contributor

LinasKo commented Dec 3, 2024

@souhhmm, any chance you could:

  1. Pull from the branch (I made a small change)
  2. Add a unit test in test/detection/test_core.py, either below or inside test_getitem, which checks for this exact situation?
prediction = sv.Detections.empty()
prediction = prediction[np.isin(prediction["class_name"], CLASSES)]

It should return True when result.is_empty() is called.

@souhhmm
Copy link
Contributor Author

souhhmm commented Dec 3, 2024

Thanks for your feedback @LinasKo, I've added the test inside test_getitem. Let me know if it needs to changed/fixed.

@LinasKo LinasKo merged commit ae8e6b2 into roboflow:develop Dec 4, 2024
10 checks passed
@LinasKo
Copy link
Contributor

LinasKo commented Dec 4, 2024

Merged. Thank you @souhhmm! 🤝

@souhhmm souhhmm deleted the fix/xyxy-shape-crash branch December 4, 2024 16:03
Saharsh1005

This comment was marked as off-topic.

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.

Crash when filtering empty detections: xyxy shape (0, 0, 4).

3 participants