-
Notifications
You must be signed in to change notification settings - Fork 25.7k
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
Conversation
if is_torch_available(): | ||
import torch | ||
from torch import nn | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
transformers/src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Lines 239 to 244 in 01509bf
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
…v2.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
There was a problem hiding this 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.
if is_torch_available(): | ||
import torch | ||
from torch import nn | ||
|
There was a problem hiding this comment.
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
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
…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>
…nto layout_detection
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
There was a problem hiding this 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.
There was a problem hiding this 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.
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
…v2.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com>
src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py
Outdated
Show resolved
Hide resolved
@Narsil since I've made this change, However, I've tried adding What am I missing? |
@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 |
Attempt huggingface#2. Limit scope of error exception to detectron2 `Lxmert` is special. Typo.
@LysandreJik could you please check this c93385e and defd574 commits where Nicolas is disabling some tests for a reason:
|
There was a problem hiding this 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
if is_torch_available(): | ||
import torch | ||
from torch import nn | ||
|
There was a problem hiding this comment.
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.
@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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
transformers/tests/test_pipelines_object_detection.py
Lines 60 to 62 in e610734
@require_datasets | |
def run_pipeline_test(self, model, tokenizer, feature_extractor): | |
object_detector = ObjectDetectionPipeline( |
transformers/tests/test_pipelines_object_detection.py
Lines 51 to 57 in e610734
@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
There was a problem hiding this comment.
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) 👍
@mishig25 Any blockers left for this ? |
|
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. |
What does this PR do?
Note: I'm using terms
document-understanding
&layout-detection
interchangeably and imo, termlayout-detection
sounds more accurate.As we have discussed in huggingface/hub-docs#21, reusing
object-detection
pipeline forlayout-detection
architectures (specifically, `LayoutLMv2ForTokenClassification).An important detail in reusing so is that:
LayoutLMv2ForTokenClassification
needsLayoutLMv2Processor
to preprocess input imageObjectDetectionPipeline.preprocess
does exactly whatLayoutLMv2Processor
does when the selected model has architecture...ForTokenClassification
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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