paddlex_cli.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 str2bool(v):
  25. """convert str to bool type
  26. """
  27. return v.lower() in ("true", "t", "1")
  28. def str2None(s):
  29. """convert to None type if it is "None"
  30. """
  31. return None if s.lower() == 'none' else s
  32. parser = argparse.ArgumentParser()
  33. ################# install pdx #################
  34. parser.add_argument(
  35. '--install', action='store_true', default=False, help="")
  36. parser.add_argument('devkits', nargs='*', default=[])
  37. parser.add_argument('--no_deps', action='store_true')
  38. parser.add_argument('--platform', type=str, default='github.com')
  39. parser.add_argument('--update_repos', action='store_true')
  40. parser.add_argument(
  41. '-y',
  42. '--yes',
  43. dest='reinstall',
  44. action='store_true',
  45. help="Whether to reinstall all packages.")
  46. ################# pipeline predict #################
  47. parser.add_argument('--predict', action='store_true', default=True, help="")
  48. parser.add_argument('--pipeline', type=str, help="")
  49. parser.add_argument('--model', nargs='+', help="")
  50. parser.add_argument('--model_dir', nargs='+', type=str2None, help="")
  51. parser.add_argument('--input', type=str, help="")
  52. parser.add_argument('--output', type=str, default="./", help="")
  53. parser.add_argument('--device', type=str, default='gpu:0', help="")
  54. return parser.parse_args()
  55. def install(args):
  56. """install paddlex
  57. """
  58. # Enable debug info
  59. os.environ['PADDLE_PDX_DEBUG'] = 'True'
  60. # Disable eager initialization
  61. os.environ['PADDLE_PDX_EAGER_INIT'] = 'False'
  62. repo_names = args.devkits
  63. if len(repo_names) == 0:
  64. repo_names = get_all_supported_repo_names()
  65. setup(
  66. repo_names=repo_names,
  67. reinstall=args.reinstall or None,
  68. no_deps=args.no_deps,
  69. platform=args.platform,
  70. update_repos=args.update_repos)
  71. return
  72. def pipeline_predict(pipeline, model_name_list, model_dir_list, input_path,
  73. output, device):
  74. """pipeline predict
  75. """
  76. pipeline = build_pipeline(pipeline, model_name_list, model_dir_list, output,
  77. device)
  78. pipeline.predict({"input_path": input_path})
  79. # for CLI
  80. def main():
  81. """API for commad line
  82. """
  83. args = args_cfg()
  84. if args.install:
  85. install(args)
  86. else:
  87. return pipeline_predict(args.pipeline, args.model, args.model_dir,
  88. args.input, args.output, args.device)