command.py 7.6 KB

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