New model support RTDETR
Author: SangbumChoi
Pull request: yes
Labels: name: New model

name: run-slow

# What does this PR do?

This is the new model for RTDETR that is complete version from https://github.com/huggingface/transformers/pull/27247

There are several TO DOs

- [X] reslove conflicts
- [X] weight files for other 7 RTDETR
- [X] Edit testing script
- [X] (optional) enable training

## 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/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] 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.
- [X] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/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:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->

@amyeroberts @NielsRogge

Comments:
Looking good @SangbumChoi! Let us know when the PR is ready for review 🤗 

@amyeroberts Hi amy! I think I can pass all the test in this week. Similar to other last PRs that I opened, I resolved conversation which is very simple or fixed as your comment. However, I did not resolved conversation which might need you to confirm or TO DOs.

@amyeroberts Hi amy, Finally I have passed all the mandatory pass for this model. I think it is a good timing for to request 2nd review for the PR! Personally I think there are 3 things to check.
1. deepcopy issue
2. postprocessing logic in image test
3. modeloutput, objectdetection output order.

Also is it possible to have difference local pytest and ci test ?
Below is my test for local pytest
==================================== 55 passed, 52 skipped, 8 warnings in 39.38s =====================================

========================================= 55 passed, 52 skipped, 8 warnings in 43.14s =========================================

However, intermediate commit or suggestion made slight difference in conversion.

It is weird that local pytest pass the all pass but ci seems to be failed
found out that error is related to https://github.com/huggingface/transformers/pull/26849/files

(Turns out not merge the latset branch was the problem?)

@amyeroberts Finally, I have passed the all test. However, there are few comments that is still not resolved could you review it again?

@amyeroberts Hi amy! Finally, I think I have modified all the changes. Thanks again
Now it is time for real final review (You can see the unresolved comments also) :)

Here are the some future TO DOs that other contributors including myself for developing RT-DETR.
1. Adding more models beside r-50 backbone.
2. stablizing fine-tuning process (or might fix some bugs in fine-tuning)

BTW, I think we can close following PRs if we merge this in to main branch!
https://github.com/huggingface/transformers/pull/27247
https://github.com/huggingface/transformers/issues/26742

@amyeroberts ping again for reminder!

@NielsRogge Could you do a first (final) review? 

@NielsRogge Thanks for the first final review. I have addressed all the suggestion!
However, I did not change about the image processor part especially related to panoptic. It is true that RT-DETR does not use the panoptic annotation currently. However, IMO that removing all the panoptic related changes need to remove all the copied from other DETR which I thought it was hard to keep track the originality. Do you still want to remove the panoptic related parts?

@NielsRogge Could you do second final review and share some thoughts about https://github.com/huggingface/transformers/pull/29077#issuecomment-2050962687 ?

Sorry made a mistake, thought I assigned Amy for review but apparently I removed the request.

@jasonkit Do you also have logits for original RTDETR for comparison?

@SangbumChoi 
> Do you also have logits for original RTDETR for comparison?

Unfortunately no, I want to ask you for that actually.

For my conversion, I just skip the check (however the output look correct when compare to `rtdetr_50vd` 's result, just difference in the ordering)

I can share my **Converted (NOT the original) model's output** on`rtdetr_r101vd` here for your reference
```
logits:
tensor([[-4.6162, -4.9189, -4.6656],
        [-4.4701, -4.4997, -4.9658],
        [-5.6641, -7.9000, -5.0725]])
        
boxes:
tensor([[0.7707, 0.4124, 0.4585],
        [0.2589, 0.5492, 0.4735],
        [0.1688, 0.1993, 0.2108]])
```

@jasonkit Let me check on that part after work!

@jasonkit This is the original result which is same as the prediction :)
```
(tensor([[[-4.6162, -4.9189, -4.6656],
          [-4.4701, -4.4997, -4.9659],
          [-5.6641, -7.9000, -5.0725]]], device='cuda:0',
        grad_fn=<SliceBackward0>),
 tensor([[[0.7707, 0.4124, 0.4585],
          [0.2589, 0.5492, 0.4735],
          [0.1688, 0.1993, 0.2108]]], device='cuda:0', grad_fn=<SliceBackward0>))
```
[rt_detr.ipynb.zip](https://github.com/huggingface/transformers/files/15274407/rt_detr.ipynb.zip)


@jasonkit Thanks for the help, https://huggingface.co/sbchoi I uploaded two R101 network 👍🏼 

@amyeroberts Hi amy, jasonkit also helped to enhance this PR! FYI, fine-tuning on custom dataset quality has been more stabilized and also two other pretrained networks which are [R101, R101-O365](https://huggingface.co/sbchoi) has been added!

I think it is the final request for the reviewing PR.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_29077). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@NielsRogge Hi niels!
> Only thing left to do is update the checkpoints and have a final slow model tests run on these

Can you help me on this task?

For someone who want to convert resnet18d and resnet34d

RTDETR has there own backbone configuration and the code
https://github.com/lyuwenyu/RT-DETR/blob/2b88d5d53bcbfbb70329bc9c007fdf7e76cf90dc/rtdetr_paddle/ppdet/modeling/backbones/resnet.py#L541
We could successfully convert larger model (R50, R101) because it is exactly same.
Unfortunately, original RTDETR with smaller backbone is not exactly same as the timm backbone pipeline, which requires additional downsample layer in the first layer. (This is because smaller model expansion is 1 otherwise it is 4).
In order to make this compatible we need to change
https://github.com/huggingface/pytorch-image-models/blob/84cb225ecb33da1a985b9d297c0a4439b6f59aac/timm/models/resnet.py#L326
following line to add downample layer when the inplanes is also same as planes * block_fn.expansion.
FYI,
https://github.com/lyuwenyu/RT-DETR/blob/2b88d5d53bcbfbb70329bc9c007fdf7e76cf90dc/rtdetr_paddle/ppdet/modeling/backbones/resnet.py#L406
RTDETR : shortcut = False if stage_idx == 0 else True,
TIMM : shortcut = stage_idx != 0 or inplanes != planes * block_fn.expansion:

@amyeroberts Could you rerun this CI? I have rebased to the main including https://github.com/huggingface/transformers/pull/30932/files

@SangbumChoi Sure! There's still some difficult clashes with library versions we're experiencing on our CI, so failing on main. As soon as those are resolved, I'll rerun and then hopefully everything will be green so we can merge 🤞 

Pinging @rwightman here regarding the Resnet-D variant which would be required here.

TLDR, this new model (RT-DETR) leverages the `AutoBackbone` class (more specifically `TimmBackbone`) in the Transformers library to use ResNet-D models from timm. But as pointed out [here](https://github.com/huggingface/transformers/pull/29077#issuecomment-2123898589), some checkpoints have a small modification for the ResNet backbone. Wondering if you're aware of this modification, whether this is supported in Timm (as I know there are various implementations of ResNet and its variants in Timm).

This is for the information to @rwightman. In my personal understanding, I think paddle version resnet
https://github.com/PaddlePaddle/PaddleDetection/blob/61cef7a7fe45c009fca76a13c47dcc516d535df9/ppdet/modeling/backbones/resnet.py#L406
is not exactly same to official resnet in the case of smaller model due to expansion_rate.

@SangbumChoi @NielsRogge That Paddle ResNet supports a/b/c/d variants. Looks like their D should match timm D with one possible exception noticed on the first pass, they might be using deformable convs by default and I've avoided that, usually need a custom kernel for those to be fast. If DCN is disabled I think they'd match.


@rwightman I think unfortunately RTDETR backbone with resnet34d and resnet18d is not compatible due to the upper mention :( 

@NielsRogge Since your comment 
https://github.com/huggingface/transformers/pull/29077#discussion_r1636075533
https://github.com/huggingface/transformers/pull/29077#discussion_r1636081513
Seems valid. There might be change in the config.json in all pretrained weight (I will re-upload and ping/tell you again!)

@NielsRogge All done :)

@amyeroberts Hi amy thanks for the comment. I just want to let you know that all the suggestion is done in [commit](https://github.com/huggingface/transformers/pull/29077/commits/be243ec87ecdf0e7735d0dc211bd9cc62f0324d7).

FYI, modeling_rt_detr_resnet is from Resnet :[ modeling_resnet.py](https://github.com/huggingface/transformers/blob/b7672826cad31e30319487af876e608d8af7d37b/src/transformers/models/resnet/modeling_resnet.py#L184). The comment for can be also updated if there is no backward compatibility issue. Let me know if this is required for another PR!

@NielsRogge Thankfully, I think we don't need additional update for the config.json in model :)

> There should also be a tests to make sure that the backbone can be set as expected e.g. like [here](https://github.com/huggingface/transformers/blob/3c4a8dca0cfe25c3f5036c6ff5f2e78358a65325/tests/models/deformable_detr/test_modeling_deformable_detr.py#L522) and [here](https://github.com/huggingface/transformers/blob/3c4a8dca0cfe25c3f5036c6ff5f2e78358a65325/tests/models/deformable_detr/test_modeling_deformable_detr.py#L553)

@SangbumChoi You still need to add tests to make sure both timm and HF backbones can be loaded in! 

@amyeroberts Oops my bad forgot the top comment :) Let me work on it

@amyeroberts Hi amy I added the test script you suggested! Maybe can you rerun the CI?

@SangbumChoi Could you rebase to include the upstream changes on `main`? This should resolve the CI failues 

@amyeroberts Hi, amy. Thanks for the final review, it turns out that there was some issue beside rebase the current main branch. 

https://github.com/huggingface/transformers/pull/29077/commits/b2d37a33303e36c2526567d41ac4d64e9671abc8

test_image_processor required appropriate dictionary in order to pass the test. So I changed at the upper commit!

@SangbumChoi Great, thanks for handling that! Could you do a final empty commit for the slow tests? 

`git commit --allow-empty -m "[run-slow] rt_detr, rt_detr_resnet"`

@amyeroberts I have handled the typo + appropriate testing in 
https://github.com/huggingface/transformers/pull/29077/commits/62d4b70680925ef447761ed2b8fbd88a93cd307c
and also empty commit for run slow 
https://github.com/huggingface/transformers/pull/29077/commits/50727ab29199015266ded1ea77c572b3dd1efe48

@amyeroberts Hi amy, Could you do a final review? (I think for the rt_detr_resnet has no slow test so it is good to merge even though the CI fails?)

cc @ydshieh This higlights a case where we might want to update the `[run_slow]` logic (or maybe not :) ). Here, there's more than one model under the model folder - rt_detr and rt_detr_resnet. How to select which model isn't completely obvious - should we just do `rt_detr` and let both model's tests be run? 

FYI) @ydshieh, @amyeroberts 
There was a similar comment from @NielsRogge suggesting that seperating `rt_detr_resnet` like on-going PR `vitpose`. Since this rt_detr_resnet architecture is not a standard resnet layer and also only used in this rt_detr I made in same folder like `mask2former-swin`. (For the purpose of not making minor architecture as a seperate folder.)

@SangbumChoi I think it's find to have the rt detr resnet under this model's folder, as @NielsRogge suggested. We just need to make sure our other tools can adapt to these kinds of patterns :) 

> How to select which model isn't completely obvious - should we just do rt_detr and let both model's tests be run?


Hi @amyeroberts. IMO, it's fine to just specify the folder name and run everything inside it.
(But if you have use cases where we really need to select part of the files to run the tests, we can discuss)