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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.backbone.resnet import get_backbone
  23. devid = int(os.getenv('DEVICE_ID'))
  24. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False, device_id=devid)
  25. def main(args):
  26. network = get_backbone(args)
  27. ckpt_path = args.pretrained
  28. if os.path.isfile(ckpt_path):
  29. param_dict = load_checkpoint(ckpt_path)
  30. param_dict_new = {}
  31. for key, values in param_dict.items():
  32. if key.startswith('moments.'):
  33. continue
  34. elif key.startswith('network.'):
  35. param_dict_new[key[8:]] = values
  36. else:
  37. param_dict_new[key] = values
  38. load_param_into_net(network, param_dict_new)
  39. print('-----------------------load model success-----------------------')
  40. else:
  41. print('-----------------------load model failed -----------------------')
  42. network.add_flags_recursive(fp16=True)
  43. network.set_train(False)
  44. input_data = np.random.uniform(low=0, high=1.0, size=(args.batch_size, 3, 112, 112)).astype(np.float32)
  45. tensor_input_data = Tensor(input_data)
  46. file_path = ckpt_path.replace('.ckpt', '_' + str(args.batch_size) + 'b.air')
  47. export(network, tensor_input_data, file_name=file_path, file_format='AIR')
  48. print('-----------------------export model success, save file:{}-----------------------'.format(file_path))
  49. def parse_args():
  50. '''parse_args'''
  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=16, help='batch size')
  54. parser.add_argument('--pre_bn', type=int, default=0, help='1: bn-conv-bn-conv-bn, 0: conv-bn-conv-bn')
  55. parser.add_argument('--inference', type=int, default=1, help='use inference backbone')
  56. parser.add_argument('--use_se', type=int, default=0, help='use se block or not')
  57. parser.add_argument('--emb_size', type=int, default=256, help='embedding size of the network')
  58. parser.add_argument('--act_type', type=str, default='relu', help='activation layer type')
  59. parser.add_argument('--backbone', type=str, default='r100', help='backbone network')
  60. parser.add_argument('--head', type=str, default='0', help='head type, default is 0')
  61. parser.add_argument('--use_drop', type=int, default=0, help='whether use dropout in network')
  62. args = parser.parse_args()
  63. return args
  64. if __name__ == "__main__":
  65. arg = parse_args()
  66. main(arg)