Object detection pipeline
Author: mishig25
Pull request: yes
Labels: name: Core: Pipeline

# What does this PR do?
* Object detection pipeline
* Give an image or list of images, outputs obj det annotations in form:
```python
[
    [
        {'score': 0.9..., 'label': 'remote', 'box': [{'x': 66, 'y': 118}, ...},
    ],
    ...
]
```
* See [colab](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb#scrollTo=3ynXL-OtGskG) for more details
<!--
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.
-->


## 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. [huggingface_hub#74](huggingface/hub-docs#6)
- [ ] 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.
@NielsRogge

<!-- 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

 -->


Comments:
@LysandreJik ofc as a personal matter I would rather this would be merged after the large refactor, but tbh this should be handled like any other PR, the first to be ready should be approved.

Moving this code to the new PR should be just as easy as the other ones (the code is currently rather well separated in terms of concerns). The biggest concern regarding code separation is one I raise here which is the scope of `post_process`. I would advocate it should take on more (so the pipelines delegates ALL work to the model) but it might be difficult for various reasons I don't measure.

Re-requesting review from @Narsil and @LysandreJik 

@Narsil,
1. Changed box format to be {xmin,ymin,xmax,ymax} [here](https://github.com/huggingface/transformers/blob/de23a8cfca50f5f166793fdd9d31458bf94f360d/src/transformers/pipelines/object_detection.py#L147-L164)
2. Added `self.framework == pt` guard on pytorch specific code [here](https://github.com/huggingface/transformers/blob/de23a8cfca50f5f166793fdd9d31458bf94f360d/src/transformers/pipelines/object_detection.py#L147-L164)
3. As suggested by comment [here](https://github.com/huggingface/transformers/pull/12886#discussion_r698575417), `post_process` is handling more responsibility. As a side effect, [this shadowing](https://github.com/huggingface/transformers/pull/12886#discussion_r698567466) concern disappears

@LysandreJik 
1. RGBA images are being handled when I updated `load_image` method (copied updates from image classification) [here](https://github.com/huggingface/transformers/blob/de23a8cfca50f5f166793fdd9d31458bf94f360d/src/transformers/pipelines/object_detection.py#L64-L83)
2. Added `ObjectDetectionPipeline` to [transformers/__init__.py](https://github.com/huggingface/transformers/blob/3f22f6d8393bd20dae5f875ec39f2adbd33d1d33/src/transformers/__init__.py)
3. Updated the testing file to match with updated testing scheme [here](https://github.com/huggingface/transformers/blob/4a8449ee18506d749da3291ac1df4b5dfefd8f62/tests/test_pipelines_object_detection.py)

Please let me know if you encounter any questions or concerns 👍 

@LysandreJik I think its ready to be merged. Please let me know if you there's anything else I need to take care of :) 

Hi @mishig25 ,

I think you need to fix all the tests.

`import torch` need to be protected behind `is_torch_available` for instance.
For code quality you can `pip install -e .[dev]` and then `make fixup`.
The PT tests also seem to require `timm` which are not available in the tests. So you need a `@require_timm` decorator.



~~@Narsil I'm confused about the tf tests failing.~~
~~For example, in this[ failed test](https://app.circleci.com/pipelines/github/huggingface/transformers/27659/workflows/e994e3b6-f627-477f-ba14-24bda195f91c/jobs/268944), I see the test failing for pipelines I haven't made any changes (also, I made sure my branch is up-to-date with the master):
here is an example for **test_pipelines_translation**~~
~~_____________ ERROR collecting tests/test_pipelines_translation.py _____________ImportError while importing test module '/home/circleci/transformers/tests/test_pipelines_translation.py'....E   ModuleNotFoundError: No module named 'torch'~~
~~Please let me know what step I'm missing~~

Since the PR was approved by two HF members and tests passed, I've merged it when the merge option became available. Please let me know if it is a correct procedure (i.e. should I have waited until a transfomers maintainer merged it?)

That's correct: as long as you have approval of one core maintainer (more for big PRs), addressed all comments, and all tests pass, you can merge your PR. :-)