Fix detection metadata length mismatch in workflows #1835
Merged
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.
Description
A frequent IndexError: boolean index did not match indexed array was occurring during workflow execution, specifically in steps following a model prediction (like detections_filter).
thread
Original Stacktrace:
Root Cause
The
supervisionlibrary'sfrom_inference()method, which converts raw model outputs intosv.Detectionsobjects, performs internal validation. For instance, it silently discards instance segmentation predictions that have apointsarray with fewer than 3 points, as they cannot form a valid polygon mask.However, several components in the
inferenceworkflow engine were generating auxiliary metadata arrays (such asdetection_id,parent_id, andimage_dimensions) using the length of the original raw prediction list rather than the length of the resultingsv.Detectionsobject.This created malformed
sv.Detectionsobjects where:xyxy,confidence) had lengthN..datadictionary arrays (e.g.,detection_id) had lengthM(whereM > N).When a subsequent workflow step (like a filter) tried to index into these detections using a boolean mask of length
N, NumPy/Supervision threw anIndexErrorbecause the mask length did not match the.dataarray length.Reasoning and Solution
To maintain consistency, the workflow engine must ensure that any metadata added to
sv.Detections.dataaligns perfectly with the detections thatsupervisionactually accepted.Changes:
filter_relevant_predictions(predictions: List[dict])ininference/core/workflows/core_steps/common/utils.py. This helper replicates the filtering logic found insupervision.Detections.from_inference.convert_inference_detections_batch_to_sv_detectionsto filter the raw predictions list if a length mismatch is detected after conversion.post_process_ocr_resultto ensure OCR metadata stays in sync with accepted detections.deserialize_detections_kindand its associated helpers indeserializers.pyto ensure detections loaded from runtime inputs are correctly aligned with their metadata.This ensures that
sv.Detectionsobjects produced by workflows are always valid and safe to filter, sort, or transform in subsequent steps.Type of change
How has this change been tested, please provide a testcase or example of how you tested the change?
Any specific deployment considerations
need new release