command.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. from six import text_type as _text_type
  15. import argparse
  16. import sys
  17. def arg_parser():
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument(
  20. "--model_dir",
  21. "-m",
  22. type=_text_type,
  23. default=None,
  24. help="define model directory path")
  25. parser.add_argument(
  26. "--save_dir",
  27. "-s",
  28. type=_text_type,
  29. default=None,
  30. help="path to save inference model")
  31. parser.add_argument(
  32. "--version",
  33. "-v",
  34. action="store_true",
  35. default=False,
  36. help="get version of PaddleX")
  37. parser.add_argument(
  38. "--export_inference",
  39. "-e",
  40. action="store_true",
  41. default=False,
  42. help="export inference model for C++/Python deployment")
  43. parser.add_argument(
  44. "--export_onnx",
  45. "-eo",
  46. action="store_true",
  47. default=False,
  48. help="export onnx model for deployment")
  49. parser.add_argument(
  50. "--fixed_input_shape",
  51. "-fs",
  52. default=None,
  53. help="export inference model with fixed input shape:[w,h]")
  54. return parser
  55. def main():
  56. import os
  57. os.environ['CUDA_VISIBLE_DEVICES'] = ""
  58. import paddlex as pdx
  59. if len(sys.argv) < 2:
  60. print("Use command 'paddlex -h` to print the help information\n")
  61. return
  62. parser = arg_parser()
  63. args = parser.parse_args()
  64. if args.version:
  65. print("PaddleX-{}".format(pdx.__version__))
  66. print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
  67. print("Email: paddlex@baidu.com")
  68. return
  69. if args.export_inference:
  70. assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
  71. assert args.save_dir is not None, "--save_dir should be defined to save inference model"
  72. fixed_input_shape = None
  73. if args.fixed_input_shape is not None:
  74. fixed_input_shape = eval(args.fixed_input_shape)
  75. assert len(
  76. fixed_input_shape
  77. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  78. else:
  79. fixed_input_shape = None
  80. model = pdx.load_model(args.model_dir, fixed_input_shape)
  81. model.export_inference_model(args.save_dir)
  82. if args.export_onnx:
  83. assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
  84. assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
  85. assert args.fixed_input_shape is not None, "--fixed_input_shape should be defined [w,h] to create onnx model, such as [224,224]"
  86. fixed_input_shape = []
  87. if args.fixed_input_shape is not None:
  88. fixed_input_shape = eval(args.fixed_input_shape)
  89. assert len(
  90. fixed_input_shape
  91. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  92. model = pdx.load_model(args.model_dir, fixed_input_shape)
  93. pdx.convertor.export_onnx_model(model, args.save_dir)
  94. if __name__ == "__main__":
  95. main()