converter.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. import os
  15. import os.path as osp
  16. from six import text_type as _text_type
  17. import argparse
  18. import sys
  19. import yaml
  20. import paddlex as pdx
  21. def arg_parser():
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument(
  24. "--model_dir",
  25. "-m",
  26. type=_text_type,
  27. default=None,
  28. help="define model directory path")
  29. parser.add_argument(
  30. "--save_dir",
  31. "-s",
  32. type=_text_type,
  33. default=None,
  34. help="path to save inference model")
  35. parser.add_argument(
  36. "--fixed_input_shape",
  37. "-fs",
  38. default=None,
  39. help="export openvino model with input shape:[w,h]")
  40. parser.add_argument(
  41. "--data_type",
  42. "-dp",
  43. default="FP32",
  44. help="option, FP32 or FP16, the data_type of openvino IR")
  45. return parser
  46. def export_openvino_model(model, args):
  47. if model.model_type == "detector" or model.__class__.__name__ == "FastSCNN":
  48. print(
  49. "Only image classifier models and semantic segmentation models(except FastSCNN) are supported to export to openvino"
  50. )
  51. try:
  52. import x2paddle
  53. if x2paddle.__version__ < '0.7.4':
  54. logging.error("You need to upgrade x2paddle >= 0.7.4")
  55. except:
  56. print(
  57. "You need to install x2paddle first, pip install x2paddle>=0.7.4")
  58. import x2paddle.convert as x2pc
  59. x2pc.paddle2onnx(args.model_dir, args.save_dir)
  60. import mo.main as mo
  61. from mo.utils.cli_parser import get_onnx_cli_parser
  62. onnx_parser = get_onnx_cli_parser()
  63. onnx_parser.add_argument("--model_dir", type=_text_type)
  64. onnx_parser.add_argument("--save_dir", type=_text_type)
  65. onnx_parser.add_argument("--fixed_input_shape")
  66. onnx_input = os.path.join(args.save_dir, 'paddle2onnx_model.onnx')
  67. onnx_parser.set_defaults(input_model=onnx_input)
  68. onnx_parser.set_defaults(output_dir=args.save_dir)
  69. shape_list = args.fixed_input_shape[1:-1].split(',')
  70. with open(osp.join(args.model_dir, "model.yml")) as f:
  71. info = yaml.load(f.read(), Loader=yaml.Loader)
  72. input_channel = 3
  73. if 'input_channel' in info['_init_params']:
  74. input_channel = info['_init_params']['input_channel']
  75. shape = '[1,{},' + shape_list[1] + ',' + shape_list[0] + ']'
  76. shape = shape.format(input_channel)
  77. if model.__class__.__name__ == "YOLOV3":
  78. shape = shape + ",[1,2]"
  79. inputs = "image,im_size"
  80. onnx_parser.set_defaults(input=inputs)
  81. onnx_parser.set_defaults(input_shape=shape)
  82. mo.main(onnx_parser, 'onnx')
  83. def main():
  84. parser = arg_parser()
  85. args = parser.parse_args()
  86. assert args.model_dir is not None, "--model_dir should be defined while exporting openvino model"
  87. assert args.save_dir is not None, "--save_dir should be defined to create openvino model"
  88. model = pdx.load_model(args.model_dir)
  89. if model.status == "Normal" or model.status == "Prune":
  90. print(
  91. "Only support inference model, try to export model first as below,",
  92. exit=False)
  93. export_openvino_model(model, args)
  94. if __name__ == "__main__":
  95. main()