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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #print(item.name, item.data_type, item.dims, len(item.raw_data))
  37. if not item.name in param_dict:
  38. print(f"Warning: Can not find '{item.name}' in checkpoint parameters dictionary")
  39. continue
  40. weight = param_dict[item.name].data.asnumpy()
  41. bin_data = weight.tobytes()
  42. if len(item.raw_data) != len(bin_data):
  43. print(f"Warning: Size of weight from checkpoint is different from original size, ignore it")
  44. continue
  45. item.raw_data = bin_data
  46. pb_msg = model.SerializeToString()
  47. with open(output_file, 'wb') as f:
  48. f.write(pb_msg)
  49. print(f'Graph name: {model.graph.name}')
  50. print(f'Initializer length: {len(initializer)}')
  51. print(f'Checkpoint dict length: {len(param_dict)}')
  52. print(f'The new weights have been written to file {output_file} successfully')
  53. def main():
  54. if len(sys.argv) < 3:
  55. print(f'Usage: {sys.argv[0]} onnx_file checkpoint_file [output_file]')
  56. sys.exit(1)
  57. onnx_file = sys.argv[1]
  58. ckpt_file = sys.argv[2]
  59. output_file = f'new_{onnx_file}' if len(sys.argv) == 3 else sys.argv[3]
  60. update_onnx_initializer(onnx_file, ckpt_file, output_file)
  61. if __name__ == '__main__':
  62. main()