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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. """Export MobilenetV2 on ImageNet"""
  16. import argparse
  17. import numpy as np
  18. import mindspore
  19. from mindspore import Tensor
  20. from mindspore import context
  21. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  22. from mindspore.train.quant import quant
  23. from src.mobilenetV2 import mobilenetV2
  24. from src.config import config_ascend
  25. parser = argparse.ArgumentParser(description='Image classification')
  26. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  27. parser.add_argument('--device_target', type=str, default=None, help='Run device target')
  28. args_opt = parser.parse_args()
  29. if __name__ == '__main__':
  30. cfg = None
  31. if args_opt.device_target == "Ascend":
  32. cfg = config_ascend
  33. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False)
  34. else:
  35. raise ValueError("Unsupported device target: {}.".format(args_opt.device_target))
  36. # define fusion network
  37. network = mobilenetV2(num_classes=cfg.num_classes)
  38. # convert fusion network to quantization aware network
  39. network = quant.convert_quant_network(network, bn_fold=True, per_channel=[True, False], symmetric=[True, False])
  40. # load checkpoint
  41. param_dict = load_checkpoint(args_opt.checkpoint_path)
  42. load_param_into_net(network, param_dict)
  43. # export network
  44. print("============== Starting export ==============")
  45. inputs = Tensor(np.ones([1, 3, cfg.image_height, cfg.image_width]), mindspore.float32)
  46. quant.export(network, inputs, file_name="mobilenet_quant", file_format='GEIR')
  47. print("============== End export ==============")