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

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 and onnx 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. if __name__ == '__main__':
  26. parser = argparse.ArgumentParser(description='Classification')
  27. parser.add_argument('--dataset_name', type=str, default='cifar10', choices=['imagenet', 'cifar10'],
  28. help='please choose dataset: imagenet or cifar10.')
  29. parser.add_argument('--device_target', type=str, default="Ascend",
  30. choices=['Ascend', 'GPU'],
  31. help='device where the code will be implemented (default: Ascend)')
  32. parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if is test, must provide\
  33. path where the trained ckpt file')
  34. args_opt = parser.parse_args()
  35. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target)
  36. if args_opt.dataset_name == 'cifar10':
  37. cfg = alexnet_cifar10_cfg
  38. elif args_opt.dataset_name == 'imagenet':
  39. cfg = alexnet_imagenet_cfg
  40. else:
  41. raise ValueError("dataset is not support.")
  42. net = AlexNet(num_classes=cfg.num_classes)
  43. param_dict = load_checkpoint(args_opt.ckpt_path)
  44. load_param_into_net(net, param_dict)
  45. input_arr = Tensor(np.random.uniform(0.0, 1.0, size=[1, 3, cfg.image_height, cfg.image_width]), ms.float32)
  46. export(net, input_arr, file_name=cfg.air_name, file_format="AIR")