command.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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",
  94. "-f",
  95. default=None,
  96. help="define dataset format(ImageNet/COCO/VOC/Seg)")
  97. parser.add_argument(
  98. "--dataset_dir",
  99. "-dd",
  100. type=_text_type,
  101. default=None,
  102. help="define the path of dataset to be splited")
  103. parser.add_argument(
  104. "--val_value",
  105. "-vv",
  106. default=None,
  107. help="define the value of validation dataset(E.g 0.2)")
  108. parser.add_argument(
  109. "--test_value",
  110. "-tv",
  111. default=None,
  112. help="define the value of test dataset(E.g 0.1)")
  113. return parser
  114. def main():
  115. import os
  116. os.environ['CUDA_VISIBLE_DEVICES'] = ""
  117. import paddlex as pdx
  118. if len(sys.argv) < 2:
  119. print("Use command 'paddlex -h` to print the help information\n")
  120. return
  121. parser = arg_parser()
  122. args = parser.parse_args()
  123. if args.version:
  124. print("PaddleX-{}".format(pdx.__version__))
  125. print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
  126. print("Email: paddlex@baidu.com")
  127. return
  128. if args.export_inference:
  129. assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
  130. assert args.save_dir is not None, "--save_dir should be defined to save inference model"
  131. fixed_input_shape = None
  132. if args.fixed_input_shape is not None:
  133. fixed_input_shape = eval(args.fixed_input_shape)
  134. assert len(
  135. fixed_input_shape
  136. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  137. else:
  138. fixed_input_shape = None
  139. model = pdx.load_model(args.model_dir, fixed_input_shape)
  140. model.export_inference_model(args.save_dir)
  141. if args.export_onnx:
  142. assert args.model_dir is not None, "--model_dir should be defined while exporting onnx model"
  143. assert args.save_dir is not None, "--save_dir should be defined to create onnx model"
  144. model = pdx.load_model(args.model_dir)
  145. if model.status == "Normal" or model.status == "Prune":
  146. logging.error(
  147. "Only support inference model, try to export model first as below,",
  148. exit=False)
  149. logging.error(
  150. "paddlex --export_inference --model_dir model_path --save_dir infer_model"
  151. )
  152. pdx.convertor.export_onnx_model(model, args.save_dir)
  153. if args.data_conversion:
  154. assert args.source is not None, "--source should be defined while converting dataset"
  155. assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
  156. assert args.pics is not None, "--pics should be defined to confirm the pictures path"
  157. assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
  158. assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
  159. if args.source == 'labelme' and args.to == 'ImageNet':
  160. logging.error(
  161. "The labelme dataset can not convert to the ImageNet dataset.",
  162. exit=False)
  163. if args.source == 'jingling' and args.to == 'PascalVOC':
  164. logging.error(
  165. "The jingling dataset can not convert to the PascalVOC dataset.",
  166. exit=False)
  167. pdx.tools.convert.dataset_conversion(args.source, args.to, args.pics,
  168. args.annotations, args.save_dir)
  169. if args.split_dataset:
  170. assert args.dataset_dir is not None, "--dataset_dir should be defined while spliting dataset"
  171. assert args.form is not None, "--form should be defined while spliting dataset"
  172. assert args.val_value is not None, "--val_value should be defined while spliting dataset"
  173. dataset_dir = args.dataset_dir
  174. dataset_form = args.form.lower()
  175. val_value = float(args.val_value)
  176. test_value = float(args.test_value
  177. if args.test_value is not None else 0)
  178. save_dir = dataset_dir
  179. if not dataset_form in ["coco", "imagenet", "voc", "seg"]:
  180. logging.error(
  181. "The dataset form is not correct defined.(support COCO/ImageNet/VOC/Seg)"
  182. )
  183. if not osp.exists(dataset_dir):
  184. logging.error("The path of dataset to be splited doesn't exist.")
  185. if val_value <= 0 or val_value >= 1 or test_value < 0 or test_value >= 1 or val_value + test_value >= 1:
  186. logging.error("The value of split is not correct.")
  187. pdx.tools.split.dataset_split(dataset_dir, dataset_form, val_value,
  188. test_value, save_dir)
  189. if __name__ == "__main__":
  190. main()