Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add obj-det pipeline support for LayoutLMV2 #13622

Closed
wants to merge 34 commits into from

Conversation

mishig25
Copy link
Contributor

What does this PR do?

Note: I'm using terms document-understanding & layout-detection interchangeably and imo, term layout-detection sounds more accurate.

As we have discussed in huggingface/hub-docs#21, reusing object-detection pipeline for layout-detection architectures (specifically, `LayoutLMv2ForTokenClassification).

An important detail in reusing so is that:

  • LayoutLMv2ForTokenClassification needs LayoutLMv2Processor to preprocess input image
  • Since processor is just a combination of (tokenizer + feature_extractor), ObjectDetectionPipeline.preprocess does exactly what LayoutLMv2Processor does when the selected model has architecture ...ForTokenClassification

Fixes # (issue)

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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Narsil @NielsRogge

Comment on lines +30 to +33
if is_torch_available():
import torch
from torch import nn

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you still import LayoutLMv2FeatureExtractor when you don't have torch installed?

Copy link
Contributor

Choose a reason for hiding this comment

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

With lazy loading, all classes are None if the various requirements are not met I think

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check this @mishig25?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this answers your question. Added import torch lines because the newly added post_process method depends on torch

def post_process(self, outputs, target_sizes, offset_mapping, bbox):
"""
Converts the output of :class:`~transformers.LayoutLMv2ForTokenClassification` into the format expected by the
COCO api. Only supports PyTorch.
Args:

Should I move the import torch statement inside post_process method ?

Copy link
Contributor

Choose a reason for hiding this comment

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

cc'ing @LysandreJik here to check what's the best option

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the import torch statement at the top level. Moving it inside the post_process function means it might crash once a program is well into progress, which can be painful. I'd rather it fail early on.

…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

Some suggestions to improve, but overall seems to work !

Cheers.

src/transformers/models/auto/modeling_auto.py Show resolved Hide resolved
Comment on lines +30 to +33
if is_torch_available():
import torch
from torch import nn

Copy link
Contributor

Choose a reason for hiding this comment

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

With lazy loading, all classes are None if the various requirements are not met I think

tests/test_pipelines_object_detection.py Outdated Show resolved Hide resolved
tests/test_pipelines_object_detection.py Outdated Show resolved Hide resolved
src/transformers/pipelines/object_detection.py Outdated Show resolved Hide resolved
src/transformers/pipelines/object_detection.py Outdated Show resolved Hide resolved
src/transformers/pipelines/object_detection.py Outdated Show resolved Hide resolved
mishig25 and others added 11 commits September 20, 2021 09:41
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
@Narsil Narsil mentioned this pull request Sep 21, 2021
5 tasks
Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM.

I left a comment again about the last test which purpose is not entirely clear to me but I think it's OK to move forward as-is too.

tests/test_feature_extraction_layoutlmv2.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

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

LGTM.

I left a comment again about the last test which purpose is not entirely clear to me but I think it's OK to move forward as-is too.

tests/test_pipelines_object_detection.py Outdated Show resolved Hide resolved
Copy link
Contributor

@NielsRogge NielsRogge left a comment

Choose a reason for hiding this comment

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

LGTM! Only a minor change in the post_process method and it's OK.

mishig25 and others added 2 commits September 23, 2021 15:48
…v2.py

Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
@mishig25
Copy link
Contributor Author

@Narsil since I've made this change,
ci/circleci: run_tests_pipelines_torch is failing for a reason: requires_backends(self, "detectron2")

However, I've tried adding @require_detectron2 decorator both to class ObjectDetectionPipelineTests & method run_pipeline_test and wasn't able to make the test pass.

What am I missing?

@Narsil
Copy link
Contributor

Narsil commented Sep 24, 2021

@mishig25 It seems it's unrelated tests. Did you rebase ?

It seems like other generic tests are failing because Layout is declaring himself as ForQuestionAnswering etc... and those tests are ran without detectron2...

Feel free to ignore, I'll take a look

@mishig25
Copy link
Contributor Author

mishig25 commented Sep 24, 2021

@Narsil I did rebase. It should be unrelated but the tests started failing after commit df86d51
Thanks a lot for looking at this!

@mishig25
Copy link
Contributor Author

@LysandreJik could you please check this c93385e and defd574 commits where Nicolas is disabling some tests for a reason:

Basically Layout displays itself as a valid model for many pipelines but it isn't (like other vision models it's fine) I just disabled those tests as they're not supposed to work
(ForQuestionAnswering, ForTextClassification and so on)

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

This looks good! The tests removed look fine to me.

Only left a comment relative to a test that would not run anymore for DETR, which is an issue

Comment on lines +30 to +33
if is_torch_available():
import torch
from torch import nn

Copy link
Member

Choose a reason for hiding this comment

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

I would keep the import torch statement at the top level. Moving it inside the post_process function means it might crash once a program is well into progress, which can be painful. I'd rather it fail early on.

Comment on lines 60 to 67
@require_detectron2
@require_datasets
@require_pytesseract
def run_pipeline_test(self, model, tokenizer, feature_extractor):
object_detector = ObjectDetectionPipeline(model=model, feature_extractor=feature_extractor)
object_detector = ObjectDetectionPipeline(
model=model, tokenizer=tokenizer, feature_extractor=feature_extractor
)
outputs = object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png", threshold=0.0)
Copy link
Member

Choose a reason for hiding this comment

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

Adding detectron2 here means that previous object detection models (DETR) will not be run. The CircleCI run that has the detectron2 dependency only runs the LayoutLM-v2 tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LysandreJik would removing require_detectron2 & require_pytesseract decorators from run_pipeline_test work? (since these decorators are being required at a class level)

@require_datasets
def run_pipeline_test(self, model, tokenizer, feature_extractor):
object_detector = ObjectDetectionPipeline(

@require_detectron2
@require_vision
@require_timm
@require_torch
@require_pytesseract
@is_pipeline_test
class ObjectDetectionPipelineTests(unittest.TestCase, metaclass=PipelineTestCaseMeta):

If not, please let me know what would be the best way to handle this problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LysandreJik could you comment on #13622 (comment) 👍

@Narsil
Copy link
Contributor

Narsil commented Oct 4, 2021

@mishig25 Any blockers left for this ?

@LysandreJik
Copy link
Member

LysandreJik commented Oct 14, 2021

Feel free to merge when ready @mishig25 @NielsRogge corrected me that some conversation still needs to happen before this is merged.

@huggingface huggingface deleted a comment from github-actions bot Nov 9, 2021
@github-actions
Copy link

github-actions bot commented Dec 4, 2021

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

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.

None yet

4 participants