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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """Convert ckpt to air."""
  16. import os
  17. import argparse
  18. import numpy as np
  19. from mindspore import context
  20. from mindspore import Tensor
  21. from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
  22. from src.FaceDetection.yolov3 import HwYolov3 as backbone_HwYolov3
  23. from src.config import config
  24. devid = int(os.getenv('DEVICE_ID'))
  25. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False, device_id=devid)
  26. def save_air(args):
  27. '''save air'''
  28. print('============= yolov3 start save air ==================')
  29. num_classes = config.num_classes
  30. anchors_mask = config.anchors_mask
  31. num_anchors_list = [len(x) for x in anchors_mask]
  32. network = backbone_HwYolov3(num_classes, num_anchors_list, args)
  33. if os.path.isfile(args.pretrained):
  34. param_dict = load_checkpoint(args.pretrained)
  35. param_dict_new = {}
  36. for key, values in param_dict.items():
  37. if key.startswith('moments.'):
  38. continue
  39. elif key.startswith('network.'):
  40. param_dict_new[key[8:]] = values
  41. else:
  42. param_dict_new[key] = values
  43. load_param_into_net(network, param_dict_new)
  44. print('load model {} success'.format(args.pretrained))
  45. input_data = np.random.uniform(low=0, high=1.0, size=(args.batch_size, 3, 448, 768)).astype(np.float32)
  46. tensor_input_data = Tensor(input_data)
  47. export(network, tensor_input_data,
  48. file_name=args.pretrained.replace('.ckpt', '_' + str(args.batch_size) + 'b.air'), file_format='AIR')
  49. print("export model success.")
  50. if __name__ == "__main__":
  51. parser = argparse.ArgumentParser(description='Convert ckpt to air')
  52. parser.add_argument('--pretrained', type=str, default='', help='pretrained model to load')
  53. parser.add_argument('--batch_size', type=int, default=8, help='batch size')
  54. arg = parser.parse_args()
  55. save_air(arg)