command.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 os.path as osp
  18. import paddlex.utils.logging as logging
  19. def arg_parser():
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument(
  22. "--model_dir",
  23. "-m",
  24. type=_text_type,
  25. default=None,
  26. help="define model directory path")
  27. parser.add_argument(
  28. "--save_dir",
  29. "-s",
  30. type=_text_type,
  31. default=None,
  32. help="path to save inference model")
  33. parser.add_argument(
  34. "--version",
  35. "-v",
  36. action="store_true",
  37. default=False,
  38. help="get version of PaddleX")
  39. parser.add_argument(
  40. "--export_inference",
  41. "-e",
  42. action="store_true",
  43. default=False,
  44. help="export inference model for C++/Python deployment")
  45. parser.add_argument(
  46. "--export_onnx",
  47. "-eo",
  48. action="store_true",
  49. default=False,
  50. help="export onnx model for deployment")
  51. parser.add_argument(
  52. "--data_conversion",
  53. "-dc",
  54. action="store_true",
  55. default=False,
  56. help="convert the dataset to the standard format")
  57. parser.add_argument(
  58. "--source",
  59. "-se",
  60. type=_text_type,
  61. default=None,
  62. help="define dataset format before the conversion")
  63. parser.add_argument(
  64. "--to",
  65. "-to",
  66. type=_text_type,
  67. default=None,
  68. help="define dataset format after the conversion")
  69. parser.add_argument(
  70. "--pics",
  71. "-p",
  72. type=_text_type,
  73. default=None,
  74. help="define pictures directory path")
  75. parser.add_argument(
  76. "--annotations",
  77. "-a",
  78. type=_text_type,
  79. default=None,
  80. help="define annotations directory path")
  81. parser.add_argument(
  82. "--fixed_input_shape",
  83. "-fs",
  84. default=None,
  85. help="export inference model with fixed input shape:[w,h]")
  86. parser.add_argument(
  87. "--split_dataset",
  88. "-sd",
  89. action="store_true",
  90. default=False,
  91. help="split dataset with the split value")
  92. parser.add_argument(
  93. "--form", "-f", default=None, help="define dataset format")
  94. parser.add_argument(
  95. "--dataset_dir",
  96. "-dd",
  97. type=_text_type,
  98. default=None,
  99. help="define the path of dataset to be splited")
  100. parser.add_argument(
  101. "--val_value",
  102. "-vv",
  103. default=None,
  104. help="define the value of validation dataset")
  105. parser.add_argument(
  106. "--test_value",
  107. "-tv",
  108. default=None,
  109. help="define the value of test dataset")
  110. return parser
  111. def main():
  112. import os
  113. os.environ['CUDA_VISIBLE_DEVICES'] = ""
  114. import paddlex as pdx
  115. if len(sys.argv) < 2:
  116. print("Use command 'paddlex -h` to print the help information\n")
  117. return
  118. parser = arg_parser()
  119. args = parser.parse_args()
  120. if args.version:
  121. print("PaddleX-{}".format(pdx.__version__))
  122. print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
  123. print("Email: paddlex@baidu.com")
  124. return
  125. if args.export_inference:
  126. assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
  127. assert args.save_dir is not None, "--save_dir should be defined to save inference model"
  128. fixed_input_shape = None
  129. if args.fixed_input_shape is not None:
  130. fixed_input_shape = eval(args.fixed_input_shape)
  131. assert len(
  132. fixed_input_shape
  133. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  134. else:
  135. fixed_input_shape = None
  136. model = pdx.load_model(args.model_dir, fixed_input_shape)
  137. model.export_inference_model(args.save_dir)
  138. if args.export_onnx:
  139. assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
  140. assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
  141. model = pdx.load_model(args.model_dir)
  142. if model.status == "Normal" or model.status == "Prune":
  143. logging.error(
  144. "Only support inference model, try to export model first as below,",
  145. exit=False)
  146. logging.error(
  147. "paddlex --export_inference --model_dir model_path --save_dir infer_model"
  148. )
  149. pdx.convertor.export_onnx_model(model, args.save_dir)
  150. if args.data_conversion:
  151. assert args.source is not None, "--source should be defined while converting dataset"
  152. assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
  153. assert args.pics is not None, "--pics should be defined to confirm the pictures path"
  154. assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
  155. assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
  156. if args.source == 'labelme' and args.to == 'ImageNet':
  157. logging.error(
  158. "The labelme dataset can not convert to the ImageNet dataset.",
  159. exit=False)
  160. if args.source == 'jingling' and args.to == 'PascalVOC':
  161. logging.error(
  162. "The jingling dataset can not convert to the PascalVOC dataset.",
  163. exit=False)
  164. pdx.tools.convert.dataset_conversion(args.source, args.to, args.pics,
  165. args.annotations, args.save_dir)
  166. if args.split_dataset:
  167. assert args.dataset_dir is not None, "--dataset_dir should be defined while spliting dataset"
  168. assert args.form is not None, "--form should be defined while spliting dataset"
  169. assert args.val_value is not None, "--val_value should be defined while spliting dataset"
  170. dataset_dir = args.dataset_dir
  171. dataset_form = args.form.lower()
  172. val_value = float(args.val_value)
  173. test_value = float(args.test_value
  174. if args.test_value is not None else 0)
  175. save_dir = dataset_dir
  176. if not dataset_form in ["coco", "imagenet", "voc", "seg"]:
  177. logging.error(
  178. "The dataset form is not correct defined.(support COCO/ImageNet/VOC/Seg)"
  179. )
  180. if not osp.exists(dataset_dir):
  181. logging.error("The path of dataset to be splited doesn't exist.")
  182. if val_value <= 0 or val_value >= 1 or test_value < 0 or test_value >= 1 or val_value + test_value >= 1:
  183. logging.error("The value of split is not correct.")
  184. pdx.tools.split.dataset_split(dataset_dir, dataset_form, val_value,
  185. test_value, save_dir)
  186. if __name__ == "__main__":
  187. main()