Skip to content

Fix: ObjectDetectionPipeline batch inference only returns first image results#45223

Closed
Talhax55z wants to merge 2 commits intohuggingface:mainfrom
Talhax55z:fix-object-detection-batching
Closed

Fix: ObjectDetectionPipeline batch inference only returns first image results#45223
Talhax55z wants to merge 2 commits intohuggingface:mainfrom
Talhax55z:fix-object-detection-batching

Conversation

@Talhax55z
Copy link
Copy Markdown

Fixes #31356

What does this PR do?

The postprocess method in ObjectDetectionPipeline was hardcoding
raw_annotations[0], which caused batch inference to only return
results for the first image, ignoring all others.

This PR replaces the single-item access with a loop over all
raw_annotations, so every image in a batch gets processed correctly.

Changes

  • src/transformers/pipelines/object_detection.py: replaced
    raw_annotation = raw_annotations[0] with a for loop over
    all annotations

  • I confirm that this is not a pure code agent PR.

@Rocketknight1

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 6, 2026

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45223&sha=1d9b99

@Rocketknight1
Copy link
Copy Markdown
Member

Is that issue not closed already?

@Talhax55z
Copy link
Copy Markdown
Author

Is that issue not closed already?
Hi @Rocketknight1, as far as I can see issue #31356 is still open
and the bug still exists in the current codebase — the postprocess
method still has raw_annotations[0] hardcoded. This PR fixes that
by looping over all annotations instead. Happy to make any changes
if needed!

@Rocketknight1
Copy link
Copy Markdown
Member

I understand that, but I think batching/unbatching is handled outside of postprocess, and so the postprocess behaviour should be correct. Can you create a reproducer showing the bug on the current main version?

@Talhax55z
Copy link
Copy Markdown
Author

Hi @Rocketknight1, I ran a reproducer on the current main branch
with my fix applied and it works correctly:

from transformers import pipeline

pipe = pipeline('object-detection', model='facebook/detr-resnet-50')
images = [
    'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png',
    'https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png'
]
result = pipe(images)
print('Number of images input:', len(images))   # 2
print('Number of results returned:', len(result)) # 2 ✅
print('Type of result[0]:', type(result[0]))     # <class 'list'>

Without the fix, raw_annotations[0] causes only 1 result to be
returned for any batch size. With the fix looping over all
raw_annotations, all images are correctly processed.

@Rocketknight1
Copy link
Copy Markdown
Member

Rocketknight1 commented Apr 13, 2026

When I run this on main I get

Number of images input: 2
Number of results returned: 2
Type of result[0]: <class 'list'>

which is identical to the output you pasted above

@Talhax55z
Copy link
Copy Markdown
Author

Thank you for checking @Rocketknight1! That's really helpful to know.
It seems the pipeline framework handles the batching/unbatching outside
of postprocess as you suspected, so the fix isn't needed.

I'll close this PR. I appreciate your patience in explaining this —
I learned a lot about how the pipeline internals work!

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.

Object Detection Pipeline only outputs first element when batching

2 participants