Skip to content

Commit 11ea6cf

Browse files
authored
Merge pull request open-mmlab#558 from hellock/tools
Add a tool to process models to be published
2 parents 6fe5ccd + ae4648f commit 11ea6cf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tools/publish_model.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
import subprocess
3+
import torch
4+
5+
6+
def parse_args():
7+
parser = argparse.ArgumentParser(
8+
description='Process a checkpoint to be published')
9+
parser.add_argument('in_file', help='input checkpoint filename')
10+
parser.add_argument('out_file', help='output checkpoint filename')
11+
args = parser.parse_args()
12+
return args
13+
14+
15+
def process_checkpoint(in_file, out_file):
16+
checkpoint = torch.load(in_file, map_location='cpu')
17+
# remove optimizer for smaller file size
18+
if 'optimizer' in checkpoint:
19+
del checkpoint['optimizer']
20+
# if it is necessary to remove some sensitive data in checkpoint['meta'],
21+
# add the code here.
22+
torch.save(checkpoint, out_file)
23+
sha = subprocess.check_output(['sha256sum', out_file]).decode()
24+
final_file = out_file.rstrip('.pth') + '-{}.pth'.format(sha[:8])
25+
subprocess.Popen(['mv', out_file, final_file])
26+
27+
28+
def main():
29+
args = parse_args()
30+
process_checkpoint(args.in_file, args.out_file)
31+
32+
33+
if __name__ == '__main__':
34+
main()

0 commit comments

Comments
 (0)