convertor.py 3.3 KB

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