paddlex_cli.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # copyright (c) 2024 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. import os
  15. import argparse
  16. import textwrap
  17. from types import SimpleNamespace
  18. from .pipelines import build_pipeline, BasePipeline
  19. from .repo_manager import setup, get_all_supported_repo_names
  20. from .utils import logging
  21. def args_cfg():
  22. """parse cli arguments
  23. """
  24. def parse_str(s):
  25. """convert str type value
  26. to None type if it is "None",
  27. to bool type if it means True or False.
  28. """
  29. if s in ("None"):
  30. return None
  31. elif s in ("TRUE", "True", "true", "T", "t"):
  32. return True
  33. elif s in ("FALSE", "False", "false", "F", "f"):
  34. return False
  35. return s
  36. parser = argparse.ArgumentParser()
  37. ################# install pdx #################
  38. parser.add_argument(
  39. '--install', action='store_true', default=False, help="")
  40. parser.add_argument('devkits', nargs='*', default=[])
  41. parser.add_argument('--no_deps', action='store_true')
  42. parser.add_argument('--platform', type=str, default='github.com')
  43. parser.add_argument(
  44. '-y',
  45. '--yes',
  46. dest='update_repos',
  47. action='store_true',
  48. help="Whether to update_repos all packages.")
  49. parser.add_argument(
  50. '--use_local_repos',
  51. action='store_true',
  52. default=False,
  53. help="Use local repos when existing.")
  54. ################# pipeline predict #################
  55. parser.add_argument('--predict', action='store_true', default=True, help="")
  56. parser.add_argument('--pipeline', type=str, help="")
  57. parser.add_argument('--model', nargs='+', help="")
  58. parser.add_argument('--model_dir', nargs='+', type=parse_str, help="")
  59. parser.add_argument('--input', type=str, help="")
  60. parser.add_argument('--output', type=str, default="./", help="")
  61. parser.add_argument('--device', type=str, default='gpu:0', help="")
  62. return parser.parse_args()
  63. def install(args):
  64. """install paddlex
  65. """
  66. # Enable debug info
  67. os.environ['PADDLE_PDX_DEBUG'] = 'True'
  68. # Disable eager initialization
  69. os.environ['PADDLE_PDX_EAGER_INIT'] = 'False'
  70. repo_names = args.devkits
  71. if len(repo_names) == 0:
  72. repo_names = get_all_supported_repo_names()
  73. setup(
  74. repo_names=repo_names,
  75. no_deps=args.no_deps,
  76. platform=args.platform,
  77. update_repos=args.update_repos,
  78. use_local_repos=args.use_local_repos)
  79. return
  80. def pipeline_predict(pipeline, model_name_list, model_dir_list, input_path,
  81. output, device):
  82. """pipeline predict
  83. """
  84. pipeline = build_pipeline(pipeline, model_name_list, model_dir_list, output,
  85. device)
  86. pipeline.predict({"input_path": input_path})
  87. # for CLI
  88. def main():
  89. """API for commad line
  90. """
  91. args = args_cfg()
  92. if args.install:
  93. install(args)
  94. else:
  95. return pipeline_predict(args.pipeline, args.model, args.model_dir,
  96. args.input, args.output, args.device)