Skip to content

FFDetrDetector fails when last batch has size 1 (AttributeError: 'tuple' object has no attribute 'with_nms') #30

@Teekyy

Description

@Teekyy

Bug Summary

FFDetrDetector.extract_widgets() crashes when the final batch has exactly one page and total pages > 1.
This happens due to inconsistent return types from model.predict().

Root Cause

model.predict() returns:
• List[sv.Detections] when batch size > 1
• sv.Detections when batch size == 1

However, extract_widgets() assumes predictions is always a list and calls:
results.extend(predictions)

When predictions is a single Detections object, it gets iterated over, causing tuples to be added to results. This later breaks when calling .with_nms().

Problematic Code

commonforms/commonforms/inference.py

predictions = self.model.predict(b, threshold=confidence)
if len(pages) == 1 or batch_size == 1:
    predictions = [predictions]
results.extend(predictions)

This logic checks the total number of pages, not the current batch size.
When the final batch has size 1 (e.g. len(pages) % batch_size == 1), predictions is not wrapped.

Failing Cases

Occurs when:
len(pages) % batch_size == 1 and len(pages) > 1

Examples (page count of pdf):
4, 7, 10, 13, ...

Additional Issue

prepare_form() exposes batch_size but it currently has no effect for FFDetr because it isn’t forwarded. This also makes it impossible to use batch_size=1 as a workaround:

results = detector.extract_widgets(
    pages, confidence=confidence, image_size=image_size
)

FFDNet’s extract_widgets() doesn’t accept batch_size, so forwarding should be conditional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions