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 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2020 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
  23. from mindspore import export, load_checkpoint, load_param_into_net
  24. from src.config import lstm_cfg as cfg
  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. args = parser.parse_args()
  32. embedding_table = np.loadtxt(os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
  33. network = SentimentNet(vocab_size=embedding_table.shape[0],
  34. embed_size=cfg.embed_size,
  35. num_hiddens=cfg.num_hiddens,
  36. num_layers=cfg.num_layers,
  37. bidirectional=cfg.bidirectional,
  38. num_classes=cfg.num_classes,
  39. weight=Tensor(embedding_table),
  40. batch_size=cfg.batch_size)
  41. param_dict = load_checkpoint(args.ckpt_file)
  42. load_param_into_net(network, param_dict)
  43. input_arr = Tensor(np.random.uniform(0.0, 1e5, size=[64, 500]).astype(np.int32))
  44. export(network, input_arr, file_name="lstm", file_format="MINDIR")