command.py 8.2 KB

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