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

init_weight got a table_key Error #4

Open
ForeUP opened this issue Apr 14, 2023 · 4 comments
Open

init_weight got a table_key Error #4

ForeUP opened this issue Apr 14, 2023 · 4 comments

Comments

@ForeUP
Copy link

ForeUP commented Apr 14, 2023

I try to run swin_small model, and I have convert_model to teacher model, met an error like fllow:

using img_triplet sampler
using Transformer_type: swin_tiny_patch4_window7_224 as a backbone
/home/yoyo/G/Ubuntu/PythonProject/Transformer/SOLIDER-REID/model/backbones/swin_transformer.py:1159: UserWarning: DeprecationWarning: pretrained is deprecated, please use "init_cfg" instead
  warnings.warn('DeprecationWarning: pretrained is deprecated, '
Traceback (most recent call last):
  File "/home/yoyo/G/Ubuntu/Applications/pycharm-2022.3/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/yoyo/G/Ubuntu/Applications/pycharm-2022.3/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/yoyo/G/Ubuntu/PythonProject/Transformer/SOLIDER-REID/train.py", line 72, in <module>
    model = make_model(cfg, num_class=num_classes, camera_num=camera_num, view_num = view_num, semantic_weight = cfg.MODEL.SEMANTIC_WEIGHT)
  File "/home/yoyo/G/Ubuntu/PythonProject/Transformer/SOLIDER-REID/model/make_model.py", line 448, in make_model
    model = build_transformer(num_class, camera_num, view_num, cfg, __factory_T_type, semantic_weight)
  File "/home/yoyo/G/Ubuntu/PythonProject/Transformer/SOLIDER-REID/model/make_model.py", line 195, in __init__
    self.base.init_weights(model_path)
  File "/home/yoyo/G/Ubuntu/PythonProject/Transformer/SOLIDER-REID/model/backbones/swin_transformer.py", line 1344, in init_weights
    table_current = self.state_dict()[table_key]
KeyError: 'stages.2.blocks.6.attn.w_msa.relative_position_bias_table'

It may means that could not fund a dict key in self.state_dict(), and I found the table_key is obtain from the pretained model.
Should I change the source of table key to obrain the table_key from the SwinTransformer model itself or its just my error not the code.

@cwhgn
Copy link
Collaborator

cwhgn commented Apr 14, 2023

hi, you can check two things first.

  1. use the pre-trained model from SOLIDER;
  2. check whether the convert_weights parameter has been set to True.
    if self.convert_weights:

    If not, it may be caused by the wrong setting of MODEL.PRETRAIN_CHOICE. You should set MODEL.PRETRAIN_CHOICE to 'self' as described in run.sh.

@ForeUP
Copy link
Author

ForeUP commented Apr 14, 2023

hi, you can check two things first.

  1. use the pre-trained model from SOLIDER;

  2. check whether the convert_weights parameter has been set to True.

    if self.convert_weights:

    If not, it may be caused by the wrong setting of MODEL.PRETRAIN_CHOICE. You should set MODEL.PRETRAIN_CHOICE to 'self' as described in run.sh.

I have tried to set convert_weights=True and convert_weights=True , and the errors are same as before.
And before that I have use convert_model.py to convert SOLIDER pretrained model.

@ForeUP
Copy link
Author

ForeUP commented Apr 14, 2023

I change the code as below, so the table_key will obtain from the model itself, and run successfully.
I'm not sure if this is the correct way to do it, but I have not encountered any errors in doing so.

      # interpolate position bias table if needed
      relative_position_bias_table_keys = [
          # k for k in state_dict.keys()
          k for k in self.state_dict().keys()
          if 'relative_position_bias_table' in k
      ]

@ForeUP ForeUP mentioned this issue May 4, 2023
@Liumeifeng283
Copy link

在问题7说提及这样修改会导致问题,那么我改如何修改初始化,只利用swin tranformer的原始数据而不利用solider

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

No branches or pull requests

3 participants