command.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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] or [n,3,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 = list(eval(args.fixed_input_shape))
  129. assert len(fixed_input_shape) in [
  130. 2, 4
  131. ], "fixed_input_shape must be a list/tuple with length 2 or 4, such as [224,224] or [1,3,224,244]"
  132. if len(fixed_input_shape) == 4:
  133. assert fixed_input_shape[
  134. 1] == 3, "input channel in fixed_input_shape must be 3, but recieved is {}".format(
  135. fixed_input_shape[1])
  136. assert fixed_input_shape[-2] > 0 and fixed_input_shape[
  137. -1] > 0, "input width and height must be a positive integer, but recievied is {}".format(
  138. fixed_input_shape[-2:])
  139. # input fixed_input_shape is [w,h]
  140. # export_inference_model needs [h,w]
  141. fixed_input_shape[-2:] = fixed_input_shape[-1:-3:-1]
  142. os.environ['PADDLEX_EXPORT_STAGE'] = 'True'
  143. os.environ['PADDLESEG_EXPORT_STAGE'] = 'True'
  144. model = pdx.load_model(args.model_dir)
  145. model._export_inference_model(args.save_dir, fixed_input_shape)
  146. if args.data_conversion:
  147. assert args.source is not None, "--source should be defined while converting dataset"
  148. assert args.to is not None, "--to should be defined to confirm the taregt dataset format"
  149. assert args.pics is not None, "--pics should be defined to confirm the pictures path"
  150. assert args.annotations is not None, "--annotations should be defined to confirm the annotations path"
  151. assert args.save_dir is not None, "--save_dir should be defined to store taregt dataset"
  152. if args.source not in ['labelme', 'jingling', 'easydata']:
  153. logging.error(
  154. "The source format {} is not one of labelme/jingling/easydata".
  155. format(args.source),
  156. exit=False)
  157. if args.to not in ['PascalVOC', 'MSCOCO', 'SEG', 'ImageNet']:
  158. logging.error(
  159. "The to format {} is not one of PascalVOC/MSCOCO/SEG/ImageNet".
  160. format(args.to),
  161. exit=False)
  162. if args.source == 'labelme' and args.to == 'ImageNet':
  163. logging.error(
  164. "The labelme dataset can not convert to the ImageNet dataset.",
  165. exit=False)
  166. if args.source == 'jingling' and args.to == 'PascalVOC':
  167. logging.error(
  168. "The jingling dataset can not convert to the PascalVOC dataset.",
  169. exit=False)
  170. if not osp.exists(args.save_dir):
  171. os.makedirs(args.save_dir)
  172. pdx.tools.convert.dataset_conversion(args.source, args.to, args.pics,
  173. args.annotations, args.save_dir)
  174. if args.split_dataset:
  175. assert args.dataset_dir is not None, "--dataset_dir should be defined while spliting dataset"
  176. assert args.format is not None, "--format should be defined while spliting dataset"
  177. assert args.val_value is not None, "--val_value should be defined while spliting dataset"
  178. dataset_dir = args.dataset_dir
  179. dataset_format = args.format.lower()
  180. val_value = float(args.val_value)
  181. test_value = float(args.test_value
  182. if args.test_value is not None else 0)
  183. save_dir = dataset_dir
  184. if not dataset_format in ["coco", "imagenet", "voc", "seg"]:
  185. logging.error(
  186. "The dataset format is not correct defined.(support COCO/ImageNet/VOC/Seg)"
  187. )
  188. if not osp.exists(dataset_dir):
  189. logging.error("The path of dataset to be splited doesn't exist.")
  190. if val_value <= 0 or val_value >= 1 or test_value < 0 or test_value >= 1 or val_value + test_value >= 1:
  191. logging.error("The value of split is not correct.")
  192. pdx.tools.split.dataset_split(dataset_dir, dataset_format, val_value,
  193. test_value, save_dir)
  194. if __name__ == "__main__":
  195. main()