glip_to_mmdet.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import subprocess
  4. from collections import OrderedDict
  5. import torch
  6. from mmengine.runner import CheckpointLoader
  7. convert_dict_fpn = {
  8. 'module.backbone.fpn.fpn_inner2': 'neck.lateral_convs.0.conv',
  9. 'module.backbone.fpn.fpn_inner3': 'neck.lateral_convs.1.conv',
  10. 'module.backbone.fpn.fpn_inner4': 'neck.lateral_convs.2.conv',
  11. 'module.backbone.fpn.fpn_layer2': 'neck.fpn_convs.0.conv',
  12. 'module.backbone.fpn.fpn_layer3': 'neck.fpn_convs.1.conv',
  13. 'module.backbone.fpn.fpn_layer4': 'neck.fpn_convs.2.conv',
  14. 'module.backbone.fpn.top_blocks.p6': 'neck.fpn_convs.3.conv',
  15. 'module.backbone.fpn.top_blocks.p7': 'neck.fpn_convs.4.conv',
  16. }
  17. def correct_unfold_reduction_order(x):
  18. out_channel, in_channel = x.shape
  19. x = x.reshape(out_channel, 4, in_channel // 4)
  20. x = x[:, [0, 2, 1, 3], :].transpose(1, 2).reshape(out_channel, in_channel)
  21. return x
  22. def correct_unfold_norm_order(x):
  23. in_channel = x.shape[0]
  24. x = x.reshape(4, in_channel // 4)
  25. x = x[[0, 2, 1, 3], :].transpose(0, 1).reshape(in_channel)
  26. return x
  27. def convert(ckpt):
  28. new_ckpt = OrderedDict()
  29. for k, v in list(ckpt.items()):
  30. if 'anchor_generator' in k or 'resizer' in k or 'cls_logits' in k:
  31. continue
  32. new_v = v
  33. if 'module.backbone.body' in k:
  34. new_k = k.replace('module.backbone.body', 'backbone')
  35. if 'patch_embed.proj' in new_k:
  36. new_k = new_k.replace('patch_embed.proj',
  37. 'patch_embed.projection')
  38. elif 'pos_drop' in new_k:
  39. new_k = new_k.replace('pos_drop', 'drop_after_pos')
  40. if 'layers' in new_k:
  41. new_k = new_k.replace('layers', 'stages')
  42. if 'mlp.fc1' in new_k:
  43. new_k = new_k.replace('mlp.fc1', 'ffn.layers.0.0')
  44. elif 'mlp.fc2' in new_k:
  45. new_k = new_k.replace('mlp.fc2', 'ffn.layers.1')
  46. elif 'attn' in new_k:
  47. new_k = new_k.replace('attn', 'attn.w_msa')
  48. if 'downsample' in k:
  49. if 'reduction.' in k:
  50. new_v = correct_unfold_reduction_order(v)
  51. elif 'norm.' in k:
  52. new_v = correct_unfold_norm_order(v)
  53. elif 'module.backbone.fpn' in k:
  54. old_k = k.replace('.weight', '')
  55. old_k = old_k.replace('.bias', '')
  56. new_k = k.replace(old_k, convert_dict_fpn[old_k])
  57. elif 'module.language_backbone' in k:
  58. new_k = k.replace('module.language_backbone',
  59. 'language_model.language_backbone')
  60. if 'pooler' in k:
  61. continue
  62. elif 'module.rpn' in k:
  63. if 'module.rpn.head.scales' in k:
  64. new_k = k.replace('module.rpn.head.scales',
  65. 'bbox_head.head.scales')
  66. else:
  67. new_k = k.replace('module.rpn', 'bbox_head')
  68. if 'anchor_generator' in k and 'resizer' in k:
  69. continue
  70. else:
  71. print('skip:', k)
  72. continue
  73. if 'DyConv' in new_k:
  74. new_k = new_k.replace('DyConv', 'dyconvs')
  75. if 'AttnConv' in new_k:
  76. new_k = new_k.replace('AttnConv', 'attnconv')
  77. new_ckpt[new_k] = new_v
  78. return new_ckpt
  79. def main():
  80. parser = argparse.ArgumentParser(
  81. description='Convert keys in pretrained eva '
  82. 'models to mmpretrain style.')
  83. parser.add_argument(
  84. 'src', default='glip_a_tiny_o365.pth', help='src model path or url')
  85. # The dst path must be a full path of the new checkpoint.
  86. parser.add_argument(
  87. '--dst', default='glip_tiny_a_mmdet.pth', help='save path')
  88. args = parser.parse_args()
  89. checkpoint = CheckpointLoader.load_checkpoint(args.src, map_location='cpu')
  90. if 'model' in checkpoint:
  91. state_dict = checkpoint['model']
  92. else:
  93. state_dict = checkpoint
  94. weight = convert(state_dict)
  95. torch.save(weight, args.dst)
  96. sha = subprocess.check_output(['sha256sum', args.dst]).decode()
  97. final_file = args.dst.replace('.pth', '') + '-{}.pth'.format(sha[:8])
  98. subprocess.Popen(['mv', args.dst, final_file])
  99. print(f'Done!!, save to {final_file}')
  100. if __name__ == '__main__':
  101. main()