supervision icon indicating copy to clipboard operation
supervision copied to clipboard

Cost Matrix contains NaN in ByteTrack() code

Open tobieabel opened this issue 2 years ago • 11 comments

Search before asking

  • [X] I have searched the Supervision issues and found no similar bug report.

Bug

I got the following error message when using supervision ByteTrack(). Something in the image is causing a NaN value to appear in the cost matrix in matching.py. I have got around the issue for now by inserting the below line of code in bold to remove the NaN value from the matrix in line 32 of matching.py:

cost_matrix[cost_matrix > thresh] = thresh +  1e-4
**cost_matrix = np.nan_to_num(cost_matrix)**
row_ind, col_ind = linear_sum_assignment(cost_matrix)
indices = np.column_stack((row_ind, col_ind))

console output:

/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/venv/lib/python3.11/site-packages/supervision/detection/utils.py:56: RuntimeWarning: invalid value encountered in divide return area_inter / (area_true[:, None] + area_detection - area_inter) Traceback (most recent call last): File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/Main.py", line 192, in start() File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/Main.py", line 171, in start obj_detections, class_results, Incident = start.Run_Models(frame) ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/Main.py", line 76, in Run_Models obj_detections = self.tracker.update_with_detections(obj_detections) # pass the detections through the tracker to add tracker ID as additional field to detections object ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/venv/lib/python3.11/site-packages/supervision/tracker/byte_tracker/core.py", line 235, in update_with_detections tracks = self.update_with_tensors( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/venv/lib/python3.11/site-packages/supervision/tracker/byte_tracker/core.py", line 367, in update_with_tensors matches, u_unconfirmed, u_detection = matching.linear_assignment( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/venv/lib/python3.11/site-packages/supervision/tracker/byte_tracker/matching.py", line 33, in linear_assignment row_ind, col_ind = linear_sum_assignment(cost_matrix) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: matrix contains invalid numeric entries

Environment

  • Supervision v0.15.0
  • Python 3.11

Minimal Reproducible Example

I'm using the following code to run YOLO8 object detector through supervision to plot the tracking on the annotated frame

def Run_Models(self,frame:np.ndarray) ->sv.detection: obj_det_result = self.object_det_model.predict(frame, device = "mps", verbose = False, conf=0.4,iou=0.7)[0]#send every frame to YOLO model obj_detections = sv.Detections.from_ultralytics(obj_det_result) # pass the results from the model to the sv libary as they are easier to manipulate obj_detections= obj_detections[obj_detections.class_id == 0] # filter the list of detections so it only shows category '0' which is people obj_detections = self.tracker.update_with_detections(obj_detections) # pass the detections through the tracker to add tracker ID as additional field to detections object

Additional

No response

Are you willing to submit a PR?

  • [ ] Yes I'd like to help by submitting a PR!

tobieabel avatar Oct 12 '23 10:10 tobieabel

Hello there, thank you for opening an Issue ! 🙏🏻 The team was notified and they will get back to you asap.

github-actions[bot] avatar Oct 12 '23 10:10 github-actions[bot]

Hi @tobieabel 👋🏻 Thanks for reporting the issue. Is it possible for you to provide us with a video example that we could use for debugging? I mean the video that is causing that issue for you.

SkalskiP avatar Oct 12 '23 10:10 SkalskiP

Sorry for the late reply, the video is attached, but I think the issue may have been caused by the object detector I was using at the time, which was the standard YOLOv8n model. Once I retrained it to get better detection results the issue seemed to go away.

https://github.com/roboflow/supervision/assets/34671814/8d7cf557-f70c-4f5d-9a0d-867bad690b59

tobieabel avatar Nov 02 '23 15:11 tobieabel

Sorry I can't attached a much longer video - the size is too large

tobieabel avatar Nov 02 '23 15:11 tobieabel

Hi @tobieabel 👋🏻 Does this problem still occur?

SkalskiP avatar Jan 26 '24 08:01 SkalskiP

no, it was only that one time. When I use a better performing model the tracking doesn't suffer from that problem anymore

tobieabel avatar Jan 26 '24 12:01 tobieabel

@rolson24, do you have any idea what could be the cause of this old issue?

SkalskiP avatar May 07 '24 11:05 SkalskiP

Hi @SkalskiP Based on the warning at the beginning of the console output:

/Users/tobieabel/PycharmProjects/Demo_4_Fight_and_Fall/venv/lib/python3.11/site-packages/supervision/detection/utils.py:56: RuntimeWarning: invalid value encountered in divide
return area_inter / (area_true[:, None] + area_detection - area_inter)

The only thing I can think of is that the bounding boxes coming from the YOLO model were either NaN, or they were bounding boxes that had 0 area. I'm leaning towards the boxes likely having 0 area (either the width is 0 or the height is 0) because I doubt Ultralytics would return boxes with null coordinates, but they might have returned boxes with 0 area.

I think the best option to possibly fix this is to simply set all of the NaN values to a cost value above the match threshold in the linear_assignment function. This would keep all of the detections around, but just not match any of the 0 area detections with any existing tracks. Then these 0 area detections, could get initialized as new tracks. Alternatively, we could just filter them right away at the beginning of update_with_tensors by calculating the area of each bounding box and then filtering out the ones with 0 area.

rolson24 avatar May 07 '24 13:05 rolson24

Thanks @rolson24 🙏🏻 I think that bounding boxes with zero area are the most likely scenario. Would you be interested in writing a PR that would protect us from this?

SkalskiP avatar May 08 '24 11:05 SkalskiP

@SkalskiP Sure!

rolson24 avatar May 08 '24 14:05 rolson24

@rolson24 thanks 🙏🏻

SkalskiP avatar May 08 '24 14:05 SkalskiP

Closing the issue as this is fixed.

LinasKo avatar Jun 14 '24 13:06 LinasKo