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.

update_onnx_weight.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. # coding=UTF-8
  3. # Copyright 2020 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # ============================================================================
  17. """
  18. Function:
  19. Use checkpoint file and onnx file as inputs, create a new onnx with Initializer's value from checkpoint file
  20. Usage:
  21. python update_onnx_weight.py onnx_file checkpoint_file [output_file]
  22. """
  23. import sys
  24. from onnx import onnx_pb
  25. from mindspore.train.serialization import load_checkpoint
  26. def update_onnx_initializer(onnx_file, ckpt_file, output_file):
  27. "Update onnx initializer."
  28. with open(onnx_file, 'rb') as f:
  29. data = f.read()
  30. model = onnx_pb.ModelProto()
  31. model.ParseFromString(data)
  32. initializer = model.graph.initializer
  33. param_dict = load_checkpoint(ckpt_file)
  34. for i, _ in enumerate(initializer):
  35. item = initializer[i]
  36. if not item.name in param_dict:
  37. print(f"Warning: Can not find '{item.name}' in checkpoint parameters dictionary")
  38. continue
  39. weight = param_dict[item.name].data.asnumpy()
  40. bin_data = weight.tobytes()
  41. if len(item.raw_data) != len(bin_data):
  42. print(f"Warning: Size of weight from checkpoint is different from original size, ignore it")
  43. continue
  44. item.raw_data = bin_data
  45. pb_msg = model.SerializeToString()
  46. with open(output_file, 'wb') as f:
  47. f.write(pb_msg)
  48. print(f'Graph name: {model.graph.name}')
  49. print(f'Initializer length: {len(initializer)}')
  50. print(f'Checkpoint dict length: {len(param_dict)}')
  51. print(f'The new weights have been written to file {output_file} successfully')
  52. def main():
  53. if len(sys.argv) < 3:
  54. print(f'Usage: {sys.argv[0]} onnx_file checkpoint_file [output_file]')
  55. sys.exit(1)
  56. onnx_file = sys.argv[1]
  57. ckpt_file = sys.argv[2]
  58. output_file = f'new_{onnx_file}' if len(sys.argv) == 3 else sys.argv[3]
  59. update_onnx_initializer(onnx_file, ckpt_file, output_file)
  60. if __name__ == '__main__':
  61. main()