• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""
18Function:
19    Use checkpoint file and onnx file as inputs, create a new onnx with Initializer's value from checkpoint file
20Usage:
21    python update_onnx_weight.py onnx_file checkpoint_file [output_file]
22"""
23import sys
24from onnx import onnx_pb
25from mindspore.train.serialization import load_checkpoint
26
27
28def update_onnx_initializer(onnx_file, ckpt_file, output_file):
29    "Update onnx initializer."
30    with open(onnx_file, 'rb') as f:
31        data = f.read()
32    model = onnx_pb.ModelProto()
33    model.ParseFromString(data)
34    initializer = model.graph.initializer
35    param_dict = load_checkpoint(ckpt_file)
36
37    for i, _ in enumerate(initializer):
38        item = initializer[i]
39        if not item.name in param_dict:
40            print(f"Warning: Can not find '{item.name}' in checkpoint parameters dictionary")
41            continue
42        weight = param_dict[item.name].data.asnumpy()
43        bin_data = weight.tobytes()
44        if len(item.raw_data) != len(bin_data):
45            print(f"Warning: Size of weight from checkpoint is different from original size, ignore it")
46            continue
47        item.raw_data = bin_data
48
49    pb_msg = model.SerializeToString()
50    with open(output_file, 'wb') as f:
51        f.write(pb_msg)
52
53    print(f'Graph name: {model.graph.name}')
54    print(f'Initializer length: {len(initializer)}')
55    print(f'Checkpoint dict length: {len(param_dict)}')
56    print(f'The new weights have been written to file {output_file} successfully')
57
58
59def main():
60    if len(sys.argv) < 3:
61        print(f'Usage: {sys.argv[0]} onnx_file checkpoint_file [output_file]')
62        sys.exit(1)
63    onnx_file = sys.argv[1]
64    ckpt_file = sys.argv[2]
65    output_file = f'new_{onnx_file}' if len(sys.argv) == 3 else sys.argv[3]
66    update_onnx_initializer(onnx_file, ckpt_file, output_file)
67
68
69if __name__ == '__main__':
70    main()
71