File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments