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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.FaceAttribute.resnet18_softmax import get_resnet18
  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 main(args):
  27. network = get_resnet18(args)
  28. ckpt_path = args.model_path
  29. if os.path.isfile(ckpt_path):
  30. param_dict = load_checkpoint(ckpt_path)
  31. param_dict_new = {}
  32. for key, values in param_dict.items():
  33. if key.startswith('moments.'):
  34. continue
  35. elif key.startswith('network.'):
  36. param_dict_new[key[8:]] = values
  37. else:
  38. param_dict_new[key] = values
  39. load_param_into_net(network, param_dict_new)
  40. print('-----------------------load model success-----------------------')
  41. else:
  42. print('-----------------------load model failed -----------------------')
  43. input_data = np.random.uniform(low=0, high=1.0, size=(args.batch_size, 3, 112, 112)).astype(np.float32)
  44. tensor_input_data = Tensor(input_data)
  45. export(network, tensor_input_data, file_name=ckpt_path.replace('.ckpt', '_' + str(args.batch_size) + 'b.air'),
  46. file_format='AIR')
  47. print('-----------------------export model success-----------------------')
  48. def parse_args():
  49. """parse_args"""
  50. parser = argparse.ArgumentParser(description='Convert ckpt to air')
  51. parser.add_argument('--model_path', type=str, default='', help='pretrained model to load')
  52. parser.add_argument('--batch_size', type=int, default=8, help='batch size')
  53. args_opt = parser.parse_args()
  54. return args_opt
  55. if __name__ == "__main__":
  56. args_1 = parse_args()
  57. args_1.dst_h = config.dst_h
  58. args_1.dst_w = config.dst_w
  59. args_1.attri_num = config.attri_num
  60. args_1.classes = config.classes
  61. args_1.flat_dim = config.flat_dim
  62. args_1.fc_dim = config.fc_dim
  63. main(args_1)