mmyolo/tools/model_converters/rtmdet_to_mmyolo.py
Yanyi Liu 4b996f10a5
Rotated object detection and RTMDet-R (#513)
* init

* add cfg

* update align

* update

* fix regularize box

* fix comment

* update config

* remove ckpt

* update

* make mmrotate optional

* fix doc

* add mmrotate req

* support large_demo with rbbox

* add ut

* update

* add doc v01

* update doc

* fix doc

* update

* update

* update readme

* update comments

* fix

* fix doc

* fix doc

* fix

* update

* update

* fix large

* update doc

* update readme

* fix config

* fix configs

* inprove

* update doc

* update assigner

* update ut

* remove rdsl assigner

* rename aug config

* speedup ut

* add comment

* fix data root

* remove doc

* remove empty folder

* add docs

* rename configs

* fix readme

* fix readme

* fix configs

* revert

* fix name

* fix table

* fix doc link

* fix doc link

* update

* update

* update

* Refactor dota splits

* add shapely

* fix typo

* fix ci

* change

* fix type

* uppdata link

* uppdata link

* add some comment

* update

---------

Co-authored-by: huanghaian <huanghaian@sensetime.com>
2023-03-02 10:27:46 +08:00

62 lines
2.1 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import argparse
from collections import OrderedDict
import torch
def convert(src, dst):
"""Convert keys in pretrained RTMDet models to MMYOLO style."""
blobs = torch.load(src)['state_dict']
state_dict = OrderedDict()
for key, weight in blobs.items():
if 'neck.reduce_layers.0' in key:
new_key = key.replace('.0', '.2')
state_dict[new_key] = weight
elif 'neck.reduce_layers.1' in key:
new_key = key.replace('reduce_layers.1', 'top_down_layers.0.1')
state_dict[new_key] = weight
elif 'neck.top_down_blocks.0' in key:
new_key = key.replace('down_blocks', 'down_layers.0')
state_dict[new_key] = weight
elif 'neck.top_down_blocks.1' in key:
new_key = key.replace('down_blocks', 'down_layers')
state_dict[new_key] = weight
elif 'downsamples' in key:
new_key = key.replace('downsamples', 'downsample_layers')
state_dict[new_key] = weight
elif 'bottom_up_blocks' in key:
new_key = key.replace('bottom_up_blocks', 'bottom_up_layers')
state_dict[new_key] = weight
elif 'out_convs' in key:
new_key = key.replace('out_convs', 'out_layers')
state_dict[new_key] = weight
elif 'bbox_head' in key:
new_key = key.replace('bbox_head', 'bbox_head.head_module')
state_dict[new_key] = weight
elif 'data_preprocessor' in key:
continue
else:
new_key = key
state_dict[new_key] = weight
print(f'Convert {key} to {new_key}')
# save checkpoint
checkpoint = dict()
checkpoint['state_dict'] = state_dict
checkpoint['meta'] = blobs.get('meta')
torch.save(checkpoint, dst)
def main():
parser = argparse.ArgumentParser(description='Convert model keys')
parser.add_argument('src', help='src rtm model path')
parser.add_argument('dst', help='save path')
args = parser.parse_args()
convert(args.src, args.dst)
if __name__ == '__main__':
main()