command.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. import paddlex.utils.logging as logging
  18. def arg_parser():
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument(
  21. "--model_dir",
  22. "-m",
  23. type=_text_type,
  24. default=None,
  25. help="define model directory path")
  26. parser.add_argument(
  27. "--save_dir",
  28. "-s",
  29. type=_text_type,
  30. default=None,
  31. help="path to save inference model")
  32. parser.add_argument(
  33. "--version",
  34. "-v",
  35. action="store_true",
  36. default=False,
  37. help="get version of PaddleX")
  38. parser.add_argument(
  39. "--export_inference",
  40. "-e",
  41. action="store_true",
  42. default=False,
  43. help="export inference model for C++/Python deployment")
  44. parser.add_argument(
  45. "--export_onnx",
  46. "-eo",
  47. action="store_true",
  48. default=False,
  49. help="export onnx model for deployment")
  50. parser.add_argument(
  51. "--data_conversion",
  52. "-dc",
  53. action="store_true",
  54. default=False,
  55. help="convert the dataset to the standard format")
  56. parser.add_argument(
  57. "--source",
  58. "-se",
  59. type=_text_type,
  60. default=None,
  61. help="define dataset format before the conversion")
  62. parser.add_argument(
  63. "--to",
  64. "-to",
  65. type=_text_type,
  66. default=None,
  67. help="define dataset format after the conversion")
  68. parser.add_argument(
  69. "--pics",
  70. "-p",
  71. type=_text_type,
  72. default=None,
  73. help="define pictures directory path")
  74. parser.add_argument(
  75. "--annotations",
  76. "-a",
  77. type=_text_type,
  78. default=None,
  79. help="define annotations directory path")
  80. parser.add_argument(
  81. "--fixed_input_shape",
  82. "-fs",
  83. default=None,
  84. help="export inference model with fixed input shape:[w,h]")
  85. return parser
  86. def main():
  87. import os
  88. os.environ['CUDA_VISIBLE_DEVICES'] = ""
  89. import paddlex as pdx
  90. if len(sys.argv) < 2:
  91. print("Use command 'paddlex -h` to print the help information\n")
  92. return
  93. parser = arg_parser()
  94. args = parser.parse_args()
  95. if args.version:
  96. print("PaddleX-{}".format(pdx.__version__))
  97. print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
  98. print("Email: paddlex@baidu.com")
  99. return
  100. if args.export_inference:
  101. assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
  102. assert args.save_dir is not None, "--save_dir should be defined to save inference model"
  103. fixed_input_shape = None
  104. if args.fixed_input_shape is not None:
  105. fixed_input_shape = eval(args.fixed_input_shape)
  106. assert len(
  107. fixed_input_shape
  108. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  109. else:
  110. fixed_input_shape = None
  111. model = pdx.load_model(args.model_dir, fixed_input_shape)
  112. model.export_inference_model(args.save_dir)
  113. if args.export_onnx:
  114. assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
  115. assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
  116. model = pdx.load_model(args.model_dir)
  117. if model.status == "Normal" or model.status == "Prune":
  118. logging.error(
  119. "Only support inference model, try to export model first as below,",
  120. exit=False)
  121. logging.error(
  122. "paddlex --export_inference --model_dir model_path --save_dir infer_model"
  123. )
  124. pdx.convertor.export_onnx_model(model, args.save_dir)
  125. if args.data_conversion:
  126. assert args.source is not None, "--source should be defined while converting dataset"
  127. assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
  128. assert args.pics is not None, "--pics should be defined to confirm the pictures path"
  129. assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
  130. assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
  131. if args.source == 'labelme' and args.to == 'ImageNet':
  132. logging.error(
  133. "The labelme dataset can not convert to the ImageNet dataset.",
  134. exit=False)
  135. if args.source == 'jingling' and args.to == 'PascalVOC':
  136. logging.error(
  137. "The jingling dataset can not convert to the PascalVOC dataset.",
  138. exit=False)
  139. pdx.tools.convert.dataset_conversion(args.source, args.to,
  140. args.pics, args.annotations, args.save_dir )
  141. if __name__ == "__main__":
  142. main()