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

[Refactor]: Refactor DETR and Deformable DETR #8763

Merged
merged 134 commits into from
Oct 20, 2022

Conversation

Li-Qingyun
Copy link
Contributor

Motivation

We (me, @jshilong, @LYMDLUT, and @KeiChiTse) are going to refactor DETR-like models, to enhance the usability and readability of our codebase.

jbwang1997 and others added 3 commits September 5, 2022 10:29
* Update

* Update

* force reinstall pycocotools

* Fix build_cuda

* docker install git

* Update

* comment other job to speedup process

* update

* uncomment

* Update

* Update

* Add comments for --force-reinstall
…b#8625)

* Refactor anchor head

* Update

* Update

* Update

* Add a series of boxes tools

* Fix box type to support n x box_dim boxes

* revert box type changes

* Add docstring

* refactor retina_head

* Update

* Update

* Fix comments

* modify docstring of coder and ioucalculator

* Replace with_boxlist with use_box_type
@Li-Qingyun
Copy link
Contributor Author

Short-term Plan

We plan to implement a base detector class for Transformer-based detector, TransformerDetector, move the transformer-related part in the original head module to the new detector module, and use TransformerEncoder and TransformerDecoder directly in the detector.

NOTE we are still exploring better refactoring solutions, and the short-term plans are only tried temporarily. It is welcome to give suggestions for refactor and report bugs, in the comments or reviews, or contact me.
(wechat:18545525156 or email: 21B905003@stu.hit.edu.cn).

@jshilong jshilong assigned jshilong and unassigned chhluo Sep 11, 2022
@ZwwWayne ZwwWayne merged commit 97e1dd9 into open-mmlab:refactor-detr Oct 20, 2022
jshilong pushed a commit that referenced this pull request Jan 19, 2023
* [Fix] Fix UT to be compatible with pytorch 1.6 (#8707)

* Update

* Update

* force reinstall pycocotools

* Fix build_cuda

* docker install git

* Update

* comment other job to speedup process

* update

* uncomment

* Update

* Update

* Add comments for --force-reinstall

* [Refactor] Refactor anchor head and base head with boxlist (#8625)

* Refactor anchor head

* Update

* Update

* Update

* Add a series of boxes tools

* Fix box type to support n x box_dim boxes

* revert box type changes

* Add docstring

* refactor retina_head

* Update

* Update

* Fix comments

* modify docstring of coder and ioucalculator

* Replace with_boxlist with use_box_type

* fix: fix config of detr-r18

* fix: modified import of MSDeformAttn in PixelDecoder of Mask2Former

* feat: add TransformerDetector as the base detector of DETR-like detectors

* refactor: refactor modules and configs of DETR

* refactor: refactor DETR-related modules in transformer.py

* refactor: refactor DETR-related modules in transformer.py

* fix: add type comments in detr.py

* correct trainloop in detr_r50 config

* fix: modify the parent class of DETRHead to BaseModule

* refactor: refactor modules and configs of Deformable DETR

* fix: modify the usage of num_query

* fix: modify the usage of num_query in configs

* refactor: replace input_proj of detr with ChannelMapper neck

* refactor: delete multi_apply in DETRHead.forward()

* Update detr_r18_8xb2-500e_coco.py

using channel mapper for r18

* change the name of detection_transfomer.py to base_detr.py

* refactor: modify construct binary masks section of forward_pretransformer

* refactor: utilize abstractmethod

* update ABCmeta to make sure reload class TransformerDetector

* some annotation

* some annotation

* some annotation

* refactor: delete _init_transformer in detectors

* refactor: modify args of deformable detr

* refactor: modify about super().__init__()

* Update detr_head.py

Remove the multi feat lvl in function 'predict_by_feat'

* Update detr.py

update init_weights

* some annotation for head

* to make sure the head args the same as detector

* to make sure the head args the same as detector

* some bug

* fix: fix bugs of num_pred in DeformableDETRHead

* add kwargs to transformer

* support MLP and sineembed position

* detele positional encodeing

* delete useless postnorm

* Revert "add kwargs to transformer"

This reverts commit a265c1a.

* Update detr_head.py

Update type and shape of args

* Update detr_head.py

fix args docstring in predict_by_feat

* Update base_detr.py

Update docstring for forward_pretransformer

* Update deformable_detr.py

Fix docstring

* to support conditional detr with reload forward_transformer

* fix: update config files of Two-stage and Box-refine

* replace all bs with batch_size in detr-related files

* update deformable.py and transformer.py

* update docstring in base_detr

* update docstring in base_detr, detr

* doc refine

* Revert "doc refine"

This reverts commit b69da4f.

* doc refine

* doc refine

* updabase_detr, detr, and le layers/transformdoc

* fix doc in base_detr

* add origin repo link

* add origin repo link

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* doc: add doc of the first edition of Deformable DETR

* batch_size to bs

* refine doc

* refine doc

* feat: add config comments of specific module

* refactor: refactor base DETR class TransformerDetector

* fix: fix wrong return typehint of forward_encoder in TransformerDetector

* refactor: refactor DETR

* refactor: refactor Deformable DETR

* refactor: refactor forward_encoder and pre_decoder

* fix: fix bugs of new edition

* refactor: small modifications

* fix: move get_reference_points to deformable_encoder

* refactor: merge init_&inter_reference to references in Deformable DETR

* modify docstring of get_valid_ratio in Deformable DETR

* add some docstring

* doc: add docstring of deformable_detr.py

* doc: add docstring of deformable_detr_head.py

* doc: modify docstring of deformable detr

* doc: add docstring of deformable_detr_head.py

* doc: modify docstring of deformable detr

* doc: add docstring of base_detr.py

* doc: refine docstring of base_detr.py

* doc: refine docstring of base_detr.py

* a little change of MLP

* a little change of MLP

* a little change of MLP

* a little change of MLP

* refine config

* refine config

* refine config

* refine doc string for detr

* little refine doc string for detr.py

* tiny modification

* doc: refine docstring of detr.py

* tiny modifications to resolve the conversations

* DETRHead.predict() draft

* tiny modifications to resolve conversations

* refactor: modify arg names and forward strategies of bbox_head

* tiny modifications to resolve the conversations

* support MLP

* fix docsting of function pre_decoder

* fix docsting of function pre_decoder

* fix docstring

* modifications for resolving conversations

* refactor: eradicate key_padding_mask args

* refactor: eradicate key_padding_mask args

* fix: fix bug of deformable detr and resolve some conversations

* refactor: rename base class with DetectionTransformer and other modifications

* fix: fix config of detr

* fix the bug of init

* fix: fix init_weight of DETR and Deformable DETR

* resolve conflict

* fix auto-merge bug

* fix pre-commit bug

* refactor: move the position of encoder and decoder

* delete Transformer in ci test

* delete Transformer in ci test

Co-authored-by: jbwang1997 <jbwang1997@gmail.com>
Co-authored-by: KeiChiTse <xqz20@mails.tsinghua.edu.cn>
Co-authored-by: LYMDLUT <70597027+LYMDLUT@users.noreply.github.com>
Co-authored-by: lym <letusgo126@126.com>
Co-authored-by: Kei-Chi Tse <109070650+KeiChiTse@users.noreply.github.com>
MeowZheng added a commit to open-mmlab/mmsegmentation that referenced this pull request Feb 1, 2023
## Motivation

The DETR-related modules have been refactored in
open-mmlab/mmdetection#8763, which causes breakings of MaskFormer and
Mask2Former in both MMDetection (has been fixed in
open-mmlab/mmdetection#9515) and MMSegmentation. This pr fix the bugs in
MMSegmentation.

### TO-DO List

- [x] update configs
- [x] check and modify data flow
- [x] fix unit test
- [x] aligning inference
- [x] write a ckpt converter
- [x] write ckpt update script
- [x] update model zoo
- [x] update model link in readme
- [x] update
[faq.md](https://github.com/open-mmlab/mmsegmentation/blob/dev-1.x/docs/en/notes/faq.md#installation)

## Tips of Fixing other implementations based on MaskXFormer of mmseg

1. The Transformer modules should be built directly. The original
building with register manner has been refactored.
2. The config requires to be modified. Delete `type` and modify several
keys, according to the modifications in this pr.
3. The `batch_first` is set `True` uniformly in the new implementations.
Hence the data flow requires to be transposed and config of
`batch_first` needs to be modified.
4. The checkpoint trained on the old implementation should be converted
to be used in the new one.

### Convert script

```Python
import argparse
from copy import deepcopy
from collections import OrderedDict

import torch

from mmengine.config import Config
from mmseg.models import build_segmentor
from mmseg.utils import register_all_modules
register_all_modules(init_default_scope=True)


def parse_args():
    parser = argparse.ArgumentParser(
        description='MMSeg convert MaskXFormer model, by Li-Qingyun')
    parser.add_argument('Mask_what_former', type=int,
                        help='Mask what former, can be a `1` or `2`',
                        choices=[1, 2])
    parser.add_argument('CFG_FILE', help='config file path')
    parser.add_argument('OLD_CKPT_FILEPATH', help='old ckpt file path')
    parser.add_argument('NEW_CKPT_FILEPATH', help='new ckpt file path')
    args = parser.parse_args()
    return args


args = parse_args()

def get_new_name(old_name: str):
    new_name = old_name

    if 'encoder.layers' in new_name:
        new_name = new_name.replace('attentions.0', 'self_attn')

    new_name = new_name.replace('ffns.0', 'ffn')

    if 'decoder.layers' in new_name:

        if args.Mask_what_former == 2:
            # for Mask2Former
            new_name = new_name.replace('attentions.0', 'cross_attn')
            new_name = new_name.replace('attentions.1', 'self_attn')
        else:
            # for Mask2Former
            new_name = new_name.replace('attentions.0', 'self_attn')
            new_name = new_name.replace('attentions.1', 'cross_attn')

    return new_name
    
def cvt_sd(old_sd: OrderedDict):
    new_sd = OrderedDict()
    for name, param in old_sd.items():
        new_name = get_new_name(name)
        assert new_name not in new_sd
        new_sd[new_name] = param
    assert len(new_sd) == len(old_sd)
    return new_sd
    
if __name__ == '__main__':
    cfg = Config.fromfile(args.CFG_FILE)
    model_cfg = cfg.model

    segmentor = build_segmentor(model_cfg)

    refer_sd = segmentor.state_dict()
    old_ckpt = torch.load(args.OLD_CKPT_FILEPATH)
    old_sd = old_ckpt['state_dict']

    new_sd = cvt_sd(old_sd)
    print(segmentor.load_state_dict(new_sd))

    new_ckpt = deepcopy(old_ckpt)
    new_ckpt['state_dict'] = new_sd
    torch.save(new_ckpt, args.NEW_CKPT_FILEPATH)
    print(f'{args.NEW_CKPT_FILEPATH} has been saved!')
```

Usage:
```bash
# for example
python ckpt4pr2532.py 1 configs/maskformer/maskformer_r50-d32_8xb2-160k_ade20k-512x512.py original_ckpts/maskformer_r50-d32_8xb2-160k_ade20k-512x512_20221030_182724-cbd39cc1.pth cvt_outputs/maskformer_r50-d32_8xb2-160k_ade20k-512x512_20221030_182724.pth
python ckpt4pr2532.py 2 configs/mask2former/mask2former_r50_8xb2-160k_ade20k-512x512.py original_ckpts/mask2former_r50_8xb2-160k_ade20k-512x512_20221204_000055-4c62652d.pth cvt_outputs/mask2former_r50_8xb2-160k_ade20k-512x512_20221204_000055.pth
```

---------

Co-authored-by: MeowZheng <meowzheng@outlook.com>
yumion pushed a commit to yumion/mmdetection that referenced this pull request Jan 31, 2024
* [Fix] Fix UT to be compatible with pytorch 1.6 (open-mmlab#8707)

* Update

* Update

* force reinstall pycocotools

* Fix build_cuda

* docker install git

* Update

* comment other job to speedup process

* update

* uncomment

* Update

* Update

* Add comments for --force-reinstall

* [Refactor] Refactor anchor head and base head with boxlist (open-mmlab#8625)

* Refactor anchor head

* Update

* Update

* Update

* Add a series of boxes tools

* Fix box type to support n x box_dim boxes

* revert box type changes

* Add docstring

* refactor retina_head

* Update

* Update

* Fix comments

* modify docstring of coder and ioucalculator

* Replace with_boxlist with use_box_type

* fix: fix config of detr-r18

* fix: modified import of MSDeformAttn in PixelDecoder of Mask2Former

* feat: add TransformerDetector as the base detector of DETR-like detectors

* refactor: refactor modules and configs of DETR

* refactor: refactor DETR-related modules in transformer.py

* refactor: refactor DETR-related modules in transformer.py

* fix: add type comments in detr.py

* correct trainloop in detr_r50 config

* fix: modify the parent class of DETRHead to BaseModule

* refactor: refactor modules and configs of Deformable DETR

* fix: modify the usage of num_query

* fix: modify the usage of num_query in configs

* refactor: replace input_proj of detr with ChannelMapper neck

* refactor: delete multi_apply in DETRHead.forward()

* Update detr_r18_8xb2-500e_coco.py

using channel mapper for r18

* change the name of detection_transfomer.py to base_detr.py

* refactor: modify construct binary masks section of forward_pretransformer

* refactor: utilize abstractmethod

* update ABCmeta to make sure reload class TransformerDetector

* some annotation

* some annotation

* some annotation

* refactor: delete _init_transformer in detectors

* refactor: modify args of deformable detr

* refactor: modify about super().__init__()

* Update detr_head.py

Remove the multi feat lvl in function 'predict_by_feat'

* Update detr.py

update init_weights

* some annotation for head

* to make sure the head args the same as detector

* to make sure the head args the same as detector

* some bug

* fix: fix bugs of num_pred in DeformableDETRHead

* add kwargs to transformer

* support MLP and sineembed position

* detele positional encodeing

* delete useless postnorm

* Revert "add kwargs to transformer"

This reverts commit a265c1a.

* Update detr_head.py

Update type and shape of args

* Update detr_head.py

fix args docstring in predict_by_feat

* Update base_detr.py

Update docstring for forward_pretransformer

* Update deformable_detr.py

Fix docstring

* to support conditional detr with reload forward_transformer

* fix: update config files of Two-stage and Box-refine

* replace all bs with batch_size in detr-related files

* update deformable.py and transformer.py

* update docstring in base_detr

* update docstring in base_detr, detr

* doc refine

* Revert "doc refine"

This reverts commit b69da4f.

* doc refine

* doc refine

* updabase_detr, detr, and le layers/transformdoc

* fix doc in base_detr

* add origin repo link

* add origin repo link

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* refine doc

* doc: add doc of the first edition of Deformable DETR

* batch_size to bs

* refine doc

* refine doc

* feat: add config comments of specific module

* refactor: refactor base DETR class TransformerDetector

* fix: fix wrong return typehint of forward_encoder in TransformerDetector

* refactor: refactor DETR

* refactor: refactor Deformable DETR

* refactor: refactor forward_encoder and pre_decoder

* fix: fix bugs of new edition

* refactor: small modifications

* fix: move get_reference_points to deformable_encoder

* refactor: merge init_&inter_reference to references in Deformable DETR

* modify docstring of get_valid_ratio in Deformable DETR

* add some docstring

* doc: add docstring of deformable_detr.py

* doc: add docstring of deformable_detr_head.py

* doc: modify docstring of deformable detr

* doc: add docstring of deformable_detr_head.py

* doc: modify docstring of deformable detr

* doc: add docstring of base_detr.py

* doc: refine docstring of base_detr.py

* doc: refine docstring of base_detr.py

* a little change of MLP

* a little change of MLP

* a little change of MLP

* a little change of MLP

* refine config

* refine config

* refine config

* refine doc string for detr

* little refine doc string for detr.py

* tiny modification

* doc: refine docstring of detr.py

* tiny modifications to resolve the conversations

* DETRHead.predict() draft

* tiny modifications to resolve conversations

* refactor: modify arg names and forward strategies of bbox_head

* tiny modifications to resolve the conversations

* support MLP

* fix docsting of function pre_decoder

* fix docsting of function pre_decoder

* fix docstring

* modifications for resolving conversations

* refactor: eradicate key_padding_mask args

* refactor: eradicate key_padding_mask args

* fix: fix bug of deformable detr and resolve some conversations

* refactor: rename base class with DetectionTransformer and other modifications

* fix: fix config of detr

* fix the bug of init

* fix: fix init_weight of DETR and Deformable DETR

* resolve conflict

* fix auto-merge bug

* fix pre-commit bug

* refactor: move the position of encoder and decoder

* delete Transformer in ci test

* delete Transformer in ci test

Co-authored-by: jbwang1997 <jbwang1997@gmail.com>
Co-authored-by: KeiChiTse <xqz20@mails.tsinghua.edu.cn>
Co-authored-by: LYMDLUT <70597027+LYMDLUT@users.noreply.github.com>
Co-authored-by: lym <letusgo126@126.com>
Co-authored-by: Kei-Chi Tse <109070650+KeiChiTse@users.noreply.github.com>
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