You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

publish_model.py 1.3 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import subprocess
  4. import torch
  5. def parse_args():
  6. parser = argparse.ArgumentParser(
  7. description='Process a checkpoint to be published')
  8. parser.add_argument('in_file', help='input checkpoint filename')
  9. parser.add_argument('out_file', help='output checkpoint filename')
  10. args = parser.parse_args()
  11. return args
  12. def process_checkpoint(in_file, out_file):
  13. checkpoint = torch.load(in_file, map_location='cpu')
  14. # remove optimizer for smaller file size
  15. if 'optimizer' in checkpoint:
  16. del checkpoint['optimizer']
  17. # if it is necessary to remove some sensitive data in checkpoint['meta'],
  18. # add the code here.
  19. if torch.__version__ >= '1.6':
  20. torch.save(checkpoint, out_file, _use_new_zipfile_serialization=False)
  21. else:
  22. torch.save(checkpoint, out_file)
  23. sha = subprocess.check_output(['sha256sum', out_file]).decode()
  24. if out_file.endswith('.pth'):
  25. out_file_name = out_file[:-4]
  26. else:
  27. out_file_name = out_file
  28. final_file = out_file_name + f'-{sha[:8]}.pth'
  29. subprocess.Popen(['mv', out_file, final_file])
  30. def main():
  31. args = parse_args()
  32. process_checkpoint(args.in_file, args.out_file)
  33. if __name__ == '__main__':
  34. main()

No Description

Contributors (1)