tinyvision / SOLIDER-REID

MIT License
55 stars 12 forks source link

init_weight got a table_key Error #4

Open ForeUP opened 1 year ago

ForeUP commented 1 year ago

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 commented 1 year ago

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. https://github.com/tinyvision/SOLIDER-REID/blob/c35c07671ceb16f72bb7c8fd94929ef436b9406b/model/backbones/swin_transformer.py#L1313 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 commented 1 year ago

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. https://github.com/tinyvision/SOLIDER-REID/blob/c35c07671ceb16f72bb7c8fd94929ef436b9406b/model/backbones/swin_transformer.py#L1313

    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 commented 1 year ago

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
      ]