command.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright (c) 2021 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. "--data_conversion",
  48. "-dc",
  49. action="store_true",
  50. default=False,
  51. help="convert the dataset to the standard format")
  52. parser.add_argument(
  53. "--source",
  54. "-se",
  55. type=_text_type,
  56. default=None,
  57. help="define dataset format before the conversion")
  58. parser.add_argument(
  59. "--to",
  60. "-to",
  61. type=_text_type,
  62. default=None,
  63. help="define dataset format after the conversion")
  64. parser.add_argument(
  65. "--pics",
  66. "-p",
  67. type=_text_type,
  68. default=None,
  69. help="define pictures directory path")
  70. parser.add_argument(
  71. "--annotations",
  72. "-a",
  73. type=_text_type,
  74. default=None,
  75. help="define annotations directory path")
  76. parser.add_argument(
  77. "--fixed_input_shape",
  78. "-fs",
  79. default=None,
  80. help="export inference model with fixed input shape:[w,h]")
  81. parser.add_argument(
  82. "--split_dataset",
  83. "-sd",
  84. action="store_true",
  85. default=False,
  86. help="split dataset with the split value")
  87. parser.add_argument(
  88. "--format",
  89. "-f",
  90. default=None,
  91. help="define dataset format(ImageNet/COCO/VOC/Seg)")
  92. parser.add_argument(
  93. "--dataset_dir",
  94. "-dd",
  95. type=_text_type,
  96. default=None,
  97. help="define the path of dataset to be splited")
  98. parser.add_argument(
  99. "--val_value",
  100. "-vv",
  101. default=None,
  102. help="define the value of validation dataset(E.g 0.2)")
  103. parser.add_argument(
  104. "--test_value",
  105. "-tv",
  106. default=None,
  107. help="define the value of test dataset(E.g 0.1)")
  108. return parser
  109. def main():
  110. import os
  111. os.environ['CUDA_VISIBLE_DEVICES'] = ""
  112. import paddlex as pdx
  113. if len(sys.argv) < 2:
  114. print("Use command 'paddlex -h` to print the help information\n")
  115. return
  116. parser = arg_parser()
  117. args = parser.parse_args()
  118. if args.version:
  119. print("PaddleX-{}".format(pdx.__version__))
  120. print("Repo: https://github.com/PaddlePaddle/PaddleX.git")
  121. print("Email: paddlex@baidu.com")
  122. return
  123. if args.export_inference:
  124. assert args.model_dir is not None, "--model_dir should be defined while exporting inference model"
  125. assert args.save_dir is not None, "--save_dir should be defined to save inference model"
  126. fixed_input_shape = None
  127. if args.fixed_input_shape is not None:
  128. fixed_input_shape = eval(args.fixed_input_shape)
  129. assert len(
  130. fixed_input_shape
  131. ) == 2, "len of fixed input shape must == 2, such as [224,224]"
  132. # input fixed_input_shape is [w,h]
  133. # export_inference_model needs [h,w]
  134. fixed_input_shape = fixed_input_shape[-1::-1]
  135. else:
  136. fixed_input_shape = [-1, -1]
  137. os.environ['PADDLEX_EXPORT_STAGE'] = 'True'
  138. os.environ['PADDLESEG_EXPORT_STAGE'] = 'True'
  139. model = pdx.load_model(args.model_dir)
  140. model._export_inference_model(args.save_dir, fixed_input_shape)
  141. if args.data_conversion:
  142. assert args.source is not None, "--source should be defined while converting dataset"
  143. assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
  144. assert args.pics is not None, "--pics should be defined to confirm the pictures path"
  145. assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
  146. assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
  147. if args.source not in ['labelme', 'jingling', 'easydata']:
  148. logging.error(
  149. "The source format {} is not one of labelme/jingling/easydata".
  150. format(args.source),
  151. exit=False)
  152. if args.to not in ['PascalVOC', 'MSCOCO', 'SEG', 'ImageNet']:
  153. logging.error(
  154. "The to format {} is not one of PascalVOC/MSCOCO/SEG/ImageNet".
  155. format(args.to),
  156. exit=False)
  157. if args.source == 'labelme' and args.to == 'ImageNet':
  158. logging.error(
  159. "The labelme dataset can not convert to the ImageNet dataset.",
  160. exit=False)
  161. if args.source == 'jingling' and args.to == 'PascalVOC':
  162. logging.error(
  163. "The jingling dataset can not convert to the PascalVOC dataset.",
  164. exit=False)
  165. if not osp.exists(args.save_dir):
  166. os.makedirs(args.save_dir)
  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.format is not None, "--format 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_format = args.format.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_format in ["coco", "imagenet", "voc", "seg"]:
  180. logging.error(
  181. "The dataset format 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_format, val_value,
  188. test_value, save_dir)
  189. if __name__ == "__main__":
  190. main()