inference icon indicating copy to clipboard operation
inference copied to clipboard

vlm_as_detector: empty Florence-2 results serialise as null image dimensions

Open davidnichols-ops opened this issue 2 months ago • 0 comments

vlm_as_detector: empty Florence-2 results serialise as null image dimensions

Follow-up promised in #2892. The automated review on #2892 flagged this as one of two P2 follow-ups (review comment).

Bug

An empty detection result from the Florence-2 parser serialises with "image": {"width": null, "height": null}, even though the block has the input image and knows its size. The same block, given a non-empty Florence-2 result, serialises the correct dimensions — so the block's output shape depends on whether anything was detected.

This is the Florence-2 half of the gap noted in review on #2892. #2892 fixed the other VLM parsers by returning empty_detections_with_image_metadata(...) (dimensions carried in sv.Detections.metadata, which survives zero rows). The two Florence-2 parsers were explicitly left as a follow-up.

Root cause

Both Florence-2 parsers write the dimensions only as a per-row data column:

  • inference/core/workflows/core_steps/formatters/vlm_as_detector/v1.py — parse_florence2_object_detection_response
  • inference/core/workflows/core_steps/formatters/vlm_as_detector/v2.py — parse_florence2_object_detection_response
dimensions = np.array([[image_height, image_width]] * len(detections))
...
detections.data.update(
    {
        INFERENCE_ID_KEY: inference_ids,
        DETECTION_ID_KEY: detection_ids,
        PREDICTION_TYPE_KEY: prediction_type,
        IMAGE_DIMENSIONS_KEY: dimensions,
    }
)

With zero detections, len(detections) == 0, so dimensions has shape (0,). serialise_sv_detections (common/serializers.py) reads dimensions from its per-row loop — image_dimensions = data.get(IMAGE_DIMENSIONS_KEY) — so with zero rows the loop body never executes and image_dimensions stays None. The serialiser's collection-metadata fallback added in #2892 only helps if the producer actually attached metadata, and the Florence-2 parsers do not.

Verification

The values below were recorded with a reproduction harness exercising the paths illustrated in the minimal example, against #2892's head (c25c6f07d11f1c782dab073674c3b2108495c11a), where the non-Florence parsers already carry the fix — this isolates the Florence-2 gap. No maintainer test run or CI result is claimed here.

Measured behaviour

ISSUE 1 - parse_florence2_object_detection_response: empty result

v1: len(detections)=0
    data['image_dimensions'].shape = (0,)
    detections.metadata              = {}
    serialise_sv_detections()['image'] = {'width': None, 'height': None}

v2: len(detections)=0
    data['image_dimensions'].shape = (0,)
    detections.metadata              = {}
    serialise_sv_detections()['image'] = {'width': None, 'height': None}

ISSUE 1 (contrast) - non-empty Florence result
v1: len(detections)=1 -> serialise_sv_detections()['image'] = {'width': 640, 'height': 480}
v2: len(detections)=1 -> serialise_sv_detections()['image'] = {'width': 640, 'height': 480}

Input image was 640x480.

Expected

Empty and non-empty results should serialise the same image.width / image.height — the dimensions come from the input image, not from the detections, so a zero-detection result is not a reason to lose them.

Environment

  • Inference: main + #2892 (c25c6f07d11f1c782dab073674c3b2108495c11a)
  • supervision: 0.29.1
  • numpy: 2.2.6
  • Python: 3.10.20
  • OS: macOS 26.6.2 (arm64)

Minimal reproducible example

import numpy as np

from inference.core.workflows.core_steps.common.serializers import serialise_sv_detections
from inference.core.workflows.core_steps.formatters.vlm_as_detector import v1, v2
from inference.core.workflows.execution_engine.entities.base import (
    ImageParentMetadata,
    WorkflowImageData,
)

image = WorkflowImageData(
    parent_metadata=ImageParentMetadata(parent_id="repro"),
    numpy_image=np.zeros((480, 640, 3), dtype=np.uint8),
)

for name, module in (("v1", v1), ("v2", v2)):
    detections = module.parse_florence2_object_detection_response(
        image=image,
        parsed_data={"bboxes": [], "labels": []},
        classes=None,
        inference_id="inference-id",
        florence_task_type="<OD>",
    )
    print(name, len(detections), serialise_sv_detections(detections)["image"])
    # v1 0 {'width': None, 'height': None}
    # v2 0 {'width': None, 'height': None}

The same flow through the block (VLMAsDetectorBlockV1 / V2, model_type="florence-2", task_type="object-detection", vlm_output='{"bboxes": [], "labels": []}') shows the same null dimensions in the block result.

Additional

Suggested fix

Attach the dimensions to collection metadata when the result is empty, mirroring empty_detections_with_image_metadata from #2892:

detections = attach_parents_coordinates_to_sv_detections(
    detections=detections, image=image
)
if len(detections) == 0:
    detections.metadata[IMAGE_DIMENSIONS_KEY] = [image_height, image_width]
return detections

Applied to both v1.py and v2.py, this makes the two cases agree:

v1: len(detections)=0 -> serialise_sv_detections()['image'] = {'width': 640, 'height': 480}
v2: len(detections)=0 -> serialise_sv_detections()['image'] = {'width': 640, 'height': 480}

Dependency on #2892

This fix depends on #2892's serialiser fallback (detections.metadata.get(IMAGE_DIMENSIONS_KEY)) being present. Without it, attaching metadata alone changes nothing observable, because the serialiser never consults collection metadata. I can submit this as a separate follow-up after #2892.

Affected tasks

Florence-2 task_type values that route to these parsers: object-detection, open-vocabulary-object-detection, object-detection-and-caption, phrase-grounded-object-detection, region-proposal, ocr-with-text-detection. Any of them returning zero detections hits this path.

Are you willing to submit a PR?

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

davidnichols-ops avatar Sep 11 '26 09:09 davidnichols-ops