Skip to content

Conversation

@qubvel
Copy link
Contributor

@qubvel qubvel commented Apr 16, 2024

What does this PR do?

In certain scenarios, the evaluation loop concatenates tensors, making them unusable. For instance, in the object detection evaluation loop, the label structure is as follows:

batch_1 = [
    {
        'size': tensor([ 771, 1333]),
        'image_id': tensor([1592]),
        'class_labels': tensor([0]),
        'boxes': tensor([[0.2268, 0.6586, 0.1567, 0.1480]]),
        'area': tensor([23827.1230]),
        'iscrowd': tensor([0]),
        'orig_size': tensor([561, 970])
    },
    ...
]
batch_2 = [
    {
        'size': tensor([1333,  763]),
        'image_id': tensor([44]),
        'class_labels': tensor([0, 1]),
        'boxes': tensor([[0.4216, 0.4584, 0.3794, 0.1979], [0.4216, 0.4584, 0.3794, 0.1979]]),
        'area': tensor([76371.3984, 76371.3984]),
        'iscrowd': tensor([0, 0]),
        'orig_size': tensor([926, 530])
    },
    ...
]

Each dictionary represents one image, with all its bounding boxes.

The problem arises when the trainer runs the evaluation loop and must concatenate each result. It concatenates the labels of every evaluation batch, using the internal nested_concat.

The result of the nested concatenation of these labels will look like this:

[
    {
        'size': tensor([ 771, 1333, 1333,  763]),
        'image_id': tensor([1592, 44]),
        'class_labels': tensor([0, 0, 1]),
        'boxes': tensor([[0.2268, 0.6586, 0.1567, 0.1480], [0.4216, 0.4584, 0.3794, 0.1979], [0.4216, 0.4584, 0.3794, 0.1979]]),
        'area': tensor([23827.1230, 76371.3984, 76371.3984]),
        'iscrowd': tensor([0, 0]),
        'orig_size': tensor([561, 970, 926, 530])
    },
    ...
]

This results in the concatenation of all boxes, making it impossible to distinguish which boxes belong to each image.

This PR introduces an additional strategy for storing batches. By setting eval_do_concat_batches=False in the training arguments, batches will be stored as separate list items rather than being concatenated.

Fixes # (issue)
#25939
https://discuss.huggingface.co/t/possible-fix-for-trainer-evaluation-with-object-detection/72307
https://discuss.huggingface.co/t/add-metrics-to-object-detection-example/31213/12

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@muellerzr and @pacman100
@amyeroberts

Copy link
Contributor

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful - the simplification in the trainer code speaks for itself. Thanks for adding this and making our trainer more flexible for different datasets!

All looks good to me, but let's get another approval from @muellerzr or @pacman100 to confirm this change to trainer is OK before merging

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Contributor

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very, very nice abstraction here! The code is much easier to read, I love it! 😍

Let's make sure those tests pass please, and I left a few recommendations for the documentation. Otherwise LG(reat)2M afterwords

@qubvel
Copy link
Contributor Author

qubvel commented Apr 17, 2024

All tests are green, all comments addressed 🙂

@amyeroberts amyeroberts merged commit c15aad0 into huggingface:main Apr 17, 2024
ydshieh pushed a commit that referenced this pull request Apr 23, 2024
* Add evaluation loop container for interm. results

* Add tests for EvalLoopContainer

* Formatting

* Fix padding_index in test and typo

* Move EvalLoopContainer to pr_utils to avoid additional imports

* Fix `eval_do_concat_batches` arg description

* Fix EvalLoopContainer import
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.

4 participants