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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 air, onnx, mindir models#################
  17. python export.py
  18. """
  19. import argparse
  20. import numpy as np
  21. import mindspore as ms
  22. from mindspore import context, Tensor, load_checkpoint, load_param_into_net, export
  23. from src.config import alexnet_cifar10_cfg, alexnet_imagenet_cfg
  24. from src.alexnet import AlexNet
  25. parser = argparse.ArgumentParser(description='Classification')
  26. parser.add_argument("--device_id", type=int, default=0, help="Device id")
  27. parser.add_argument("--batch_size", type=int, default=1, help="batch size")
  28. parser.add_argument('--dataset_name', type=str, default='cifar10', choices=['imagenet', 'cifar10'],
  29. help='please choose dataset: imagenet or cifar10.')
  30. parser.add_argument('--device_target', type=str, default="Ascend",
  31. choices=['Ascend', 'GPU', 'CPU'],
  32. help='device where the code will be implemented (default: Ascend)')
  33. parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.")
  34. parser.add_argument("--file_name", type=str, default="alexnet", help="output file name.")
  35. parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format")
  36. args_opt = parser.parse_args()
  37. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target)
  38. if args_opt.device_target == "Ascend":
  39. context.set_context(device_id=args_opt.device_id)
  40. if __name__ == '__main__':
  41. if args_opt.dataset_name == 'cifar10':
  42. cfg = alexnet_cifar10_cfg
  43. elif args_opt.dataset_name == 'imagenet':
  44. cfg = alexnet_imagenet_cfg
  45. else:
  46. raise ValueError("dataset is not support.")
  47. net = AlexNet(num_classes=cfg.num_classes)
  48. param_dict = load_checkpoint(args_opt.ckpt_file)
  49. load_param_into_net(net, param_dict)
  50. input_arr = Tensor(np.zeros([args_opt.batch_size, 3, cfg.image_height, cfg.image_width]), ms.float32)
  51. export(net, input_arr, file_name=args_opt.file_name, file_format=args_opt.file_format)