Add `obj-det` pipeline support for `LayoutLMV2`
Author: mishig25
Pull request: yes

# 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`

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? 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](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [x] 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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->
@Narsil @NielsRogge 

Comments:
Currently, there are 2 layout models: [layoutlm](https://github.com/huggingface/transformers/tree/master/src/transformers/models/layoutlm) & [layoutlmv2](https://github.com/huggingface/transformers/tree/master/src/transformers/models/layoutlmv2).

Unlike layoutlmv2, layoutlm lacks its feature_extractor (which should be quite similar to layoutlmv2's one) -> therefore, layoutlm (layoutlmfortokenclassification) can;t be used in ObjectDetectionPipeline currently.
I propose to take care of it (add feature_extractor to layoutlm) in a separate PR

wdyt @Narsil @NielsRogge 

@mishig25 you can directly use `LayoutLMv2FeatureExtractor` for LayoutLMv1. You should just not use the `image` key it creates.

@NielsRogge sounds great.

Becuase of the `transformers` design, I will copy all of the code from `models/layoutlmv2/feature_extraction_layoutlmv2.py` -> `models/layoutlm/feature_extraction_layoutlm.py`

 one detail I'd like to know is:
1. [LayoutLMForTokenClassification](https://github.com/huggingface/transformers/blob/eae7a96b7d5810ee6723e41f9b316cc51672fbb1/src/transformers/models/layoutlm/modeling_layoutlm.py#L1082-L1087) exists for the Pytorch version of the architecture only (not for Tensorflow version of the model)
2. LayoutLMv2FeatureExtractor has torch dependency as [in here](https://github.com/huggingface/transformers/blob/f3248a67ca8ed8c1075f7bdcb7ab896a5c59f4d0/src/transformers/models/layoutlmv2/feature_extraction_layoutlmv2.py#L30-L33)
3. Because 1 & 2 -> therefore, I should not add any tensorflow related code to `layoutlm/feature_extraction_layoutlm.py`. Is it correct?

Ok, perhaps it might be better to do that in a separate PR. Because then you also need to add the tokenizer, etc.

Also, the feature extractor shouldn't have a PyTorch dependency (only depends on PIL), unless you use it for postprocessing.

@Narsil since I've made [this change](https://github.com/huggingface/transformers/pull/13622#discussion_r715404373),
[ci/circleci: run_tests_pipelines_torch ](https://app.circleci.com/pipelines/github/huggingface/transformers/28325/workflows/6bb6f52b-bbc1-4f38-936f-a1cb31490c1c/jobs/278922) 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?

@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

@Narsil I did rebase. It should be unrelated but the tests started failing after commit https://github.com/huggingface/transformers/pull/13622/commits/df86d519d89e69ec2b823cbb36bac58e6bfaac18
Thanks a lot for looking at this!

@LysandreJik could you please check this https://github.com/huggingface/transformers/pull/13622/commits/c93385e0af44b41373100a81ba44628e1bc60379 and https://github.com/huggingface/transformers/pull/13622/commits/defd5747afdc906d7acc86225d4fe41ca8336398 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)

@mishig25 Any blockers left for this ?

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

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](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.