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.

export.py 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. ##############export checkpoint file into mindir model#################
  17. python export.py
  18. """
  19. import argparse
  20. import os
  21. import numpy as np
  22. from mindspore import Tensor, context
  23. from mindspore import export, load_checkpoint, load_param_into_net
  24. from src.config import lstm_cfg, lstm_cfg_ascend
  25. from src.lstm import SentimentNet
  26. if __name__ == '__main__':
  27. parser = argparse.ArgumentParser(description='MindSpore LSTM Exporter')
  28. parser.add_argument('--preprocess_path', type=str, default='./preprocess',
  29. help='path where the pre-process data is stored.')
  30. parser.add_argument('--ckpt_file', type=str, required=True, help='lstm ckpt file.')
  31. parser.add_argument("--device_id", type=int, default=0, help="Device id")
  32. parser.add_argument("--file_name", type=str, default="lstm", help="output file name.")
  33. parser.add_argument('--file_format', type=str, choices=["AIR", "MINDIR"], default='AIR', help='file format')
  34. parser.add_argument('--device_target', type=str, default="Ascend", choices=['GPU', 'CPU', 'Ascend'],
  35. help='the target device to run, support "GPU", "CPU". Default: "Ascend".')
  36. args = parser.parse_args()
  37. context.set_context(
  38. mode=context.GRAPH_MODE,
  39. save_graphs=False,
  40. device_target=args.device_target,
  41. device_id=args.device_id)
  42. if args.device_target == 'Ascend':
  43. cfg = lstm_cfg_ascend
  44. else:
  45. cfg = lstm_cfg
  46. embedding_table = np.loadtxt(os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
  47. if args.device_target == 'Ascend':
  48. pad_num = int(np.ceil(cfg.embed_size / 16) * 16 - cfg.embed_size)
  49. if pad_num > 0:
  50. embedding_table = np.pad(embedding_table, [(0, 0), (0, pad_num)], 'constant')
  51. cfg.embed_size = int(np.ceil(cfg.embed_size / 16) * 16)
  52. network = SentimentNet(vocab_size=embedding_table.shape[0],
  53. embed_size=cfg.embed_size,
  54. num_hiddens=cfg.num_hiddens,
  55. num_layers=cfg.num_layers,
  56. bidirectional=cfg.bidirectional,
  57. num_classes=cfg.num_classes,
  58. weight=Tensor(embedding_table),
  59. batch_size=cfg.batch_size)
  60. param_dict = load_checkpoint(args.ckpt_file)
  61. load_param_into_net(network, param_dict)
  62. input_arr = Tensor(np.random.uniform(0.0, 1e5, size=[cfg.batch_size, 500]).astype(np.int32))
  63. export(network, input_arr, file_name=args.file_name, file_format=args.file_format)