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

fix beit model converter #1616

Merged
merged 2 commits into from
Jun 21, 2022
Merged

Conversation

ZhaoYi1222
Copy link

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

The script in tools/model_converters/beit2mmseg.py doesn't match the key of models between the official repo and MMSegmentation style (e.g. backbone.*).
The training log shows lots of keys in missing keys in source state_dict.
Trainging result after 1500 iteration:
| aAcc | mIoU | mAcc |
| 39.95 | 1.97 | 3.46 |

Modification

I modify tools/model_converters/beit2mmseg.py.
Now the warnings of missing keys related to 'backbone' disappear.
Trainging result after 1500 iteration:
| aAcc | mIoU | mAcc |
| 60.4 | 6.21 | 9.4 |

BC-breaking (Optional)

Use cases (Optional)

Checklist

@linfangjian01
Copy link
Contributor

In the mmseg implementation, the pre-trained model is loaded in mmseg/models/backbones/beit.py, so the 'backbone' prefix is not needed. Are you using our tools/model_converters/beit2mmseg.py script in your own codebase? Please see the other model_converters scripts and refer to the implementation in beit.py. Also, for

self.load_state_dict(state_dict, False)
, you can set it to True, so that you can check which key values are missing. The missing keys of the current model are
111. These pre-trained keys are not needed in the downstream task.

@MeowZheng
Copy link
Collaborator

@ ZhaoYi1222 What do you think about this?

In the mmseg implementation, the pre-trained model is loaded in mmseg/models/backbones/beit.py, so the 'backbone' prefix is not needed. Are you using our tools/model_converters/beit2mmseg.py script in your own codebase? Please see the other model_converters scripts and refer to the implementation in beit.py. Also, for

self.load_state_dict(state_dict, False)

, you can set it to True, so that you can check which key values are missing. The missing keys of the current model are
111. These pre-trained keys are not needed in the downstream task.

@ZhaoYi1222
Copy link
Author

Sorry for the delay. I try to reproduce the result shown in

configs/beit/README.md

So I download model from

https://conversationhub.blob.core.windows.net/beit-share-public/beit/beit_large_patch16_224_pt22k_ft22k.pth

Then I use

tools/model_converters/beit2mmseg.py

to convert the model.

When I train the model using --load-from, the log shows
unexpected key in source state_dict: cls_token, patch_embed.projection.weight, patch_embed.proj.weight, patch_embed.projection.bias, patch_embed.proj.bias, layers.0.gamma_1 and
missing keys in source state_dict: backbone.cls_token, backbone.patch_embed.projection.weight, backbone.patch_embed.projection.bias, backbone.layers.0.gamma_1

Maybe I make some mistakes in my training scripts, I think some guidance about training can be added to README.md?

Are you using our tools/model_converters/beit2mmseg.py script in your own codebase?

I run training based on mmsegmentation and try to follow the guidance.

Also, in

tools/model_converters/beit2mmseg.py

I think if k.startswith('patch_embed'), key would be add twice in line 17 and 29.

Thanks again for your generous help. @linfangjian01 @MeowZheng

@linfangjian01
Copy link
Contributor

Thx. I think 'patch_embed' can be modified with reference to https://github.com/ZhaoYi1222/mmsegmentation/blob/0ca30ef84579ce4c62aa3aac1ab3769672b389bc/tools/model_converters/beit2mmseg.py#L23-L24 and other parts can be kept. In addition the training can be referred to https://github.com/open-mmlab/mmsegmentation/blob/master/docs/en/train.md. The pretrain path can be modified in

pretrained='pretrain/beit_base_patch16_224_pt22k_ft22k.pth',
.

@linfangjian01
Copy link
Contributor

or use ln -s pretrain_local_path pretrain to create a soft connection in the same level directory as config, mmseg.

@linfangjian01
Copy link
Contributor

suggest change:
beit

I have modified the code according to your suggestion. Thanks.
@ZhaoYi1222
Copy link
Author

I have modified the code according to your suggestion. Thanks. @linfangjian01
I'll check my training scripts later to match parameters.

@linfangjian01
Copy link
Contributor

Thanks.

@MeowZheng MeowZheng merged commit 5fc7c5c into open-mmlab:master Jun 21, 2022
ZhimingNJ pushed a commit to AetrexTechnology/mmsegmentation that referenced this pull request Jun 29, 2022
* fix beit model converter

* Update beit2mmseg.py

I have modified the code according to your suggestion. Thanks.
huajiangjiangLi pushed a commit to pytorchuser/HDB-Seg that referenced this pull request Apr 12, 2023
* fix beit model converter

* Update beit2mmseg.py

I have modified the code according to your suggestion. Thanks.
wjkim81 pushed a commit to wjkim81/mmsegmentation that referenced this pull request Dec 3, 2023
…tasets.md` and `installation.md` (open-mmlab#1616)

* add some docs

* refine installation.md
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.

4 participants