Skip to content

Commit

Permalink
update publish_model script
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangxu-0103 committed Dec 7, 2023
1 parent 5c0613b commit f07f13c
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions tools/model_converters/publish_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,59 @@
import subprocess

import torch
from mmengine.logging import print_log
from mmengine.utils import digit_version


def parse_args():
parser = argparse.ArgumentParser(
description='Process a checkpoint to be published')
parser.add_argument('in_file', help='input checkpoint filename')
parser.add_argument('out_file', help='output checkpoint filename')
parser.add_argument(
'--save-keys',
nargs='+',
type=str,
default=['meta', 'state_dict'],
help='keys to save in the published checkpoint')
args = parser.parse_args()
return args


def process_checkpoint(in_file, out_file):
def process_checkpoint(in_file, out_file, save_keys=['meta', 'state_dict']):
checkpoint = torch.load(in_file, map_location='cpu')
# remove optimizer for smaller file size
if 'optimizer' in checkpoint:
del checkpoint['optimizer']

# only keep `meta` and `state_dict` for smaller file size
ckpt_keys = list(checkpoint.keys())
for k in ckpt_keys:
if k not in save_keys:
print_log(
f'Key `{k}` will be removed because it is not in '
'save_keys. If you want to keep it, '
'please set --save-keys.',
logger='current')
checkpoint.pop(k, None)

# if it is necessary to remove some sensitive data in checkpoint['meta'],
# add the code here.
torch.save(checkpoint, out_file)
if digit_version(torch.__version__) >= digit_version('1.6'):
torch.save(checkpoint, out_file, _use_new_zipfile_serialization=False)
else:
torch.save(checkpoint, out_file)
sha = subprocess.check_output(['sha256sum', out_file]).decode()
final_file = out_file.rstrip('.pth') + '-{}.pth'.format(sha[:8])
if out_file.endswith('.pth'):
out_file_name = out_file[:-4]
else:
out_file_name = out_file
final_file = out_file_name + f'-{sha[:8]}.pth'
subprocess.Popen(['mv', out_file, final_file])
print_log(
f'The published model is saved at {final_file}.', logger='current')


def main():
args = parse_args()
process_checkpoint(args.in_file, args.out_file)
process_checkpoint(args.in_file, args.out_file, args.save_keys)


if __name__ == '__main__':
Expand Down

0 comments on commit f07f13c

Please sign in to comment.