__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from pathlib import Path
  15. from ..utils.official_models import official_models
  16. from .base import BasePredictor, BasicPredictor
  17. from .image_classification import ClasPredictor
  18. from .text_detection import TextDetPredictor
  19. from .text_recognition import TextRecPredictor
  20. from .table_recognition import TablePredictor
  21. from .object_detection import DetPredictor
  22. from .instance_segmentation import InstanceSegPredictor
  23. from .semantic_segmentation import SegPredictor
  24. from .general_recognition import ShiTuRecPredictor
  25. from .ts_fc import TSFcPredictor
  26. from .ts_ad import TSAdPredictor
  27. from .ts_cls import TSClsPredictor
  28. from .image_unwarping import WarpPredictor
  29. from .anomaly_detection import UadPredictor
  30. def _create_hp_predictor(
  31. model_name, model_dir, device, config, hpi_params, *args, **kwargs
  32. ):
  33. try:
  34. from paddlex_hpi.predictors import HPPredictor
  35. except ModuleNotFoundError as e:
  36. raise RuntimeError(
  37. "The PaddleX HPI plugin is not properly installed, and the high-performance model inference features are not available."
  38. )
  39. if hpi_params is None:
  40. raise ValueError("No HPI params given")
  41. if "serial_number" not in hpi_params:
  42. raise ValueError("The serial number is required but was not provided.")
  43. serial_number = hpi_params["serial_number"]
  44. update_license = hpi_params.get("update_license", False)
  45. return HPPredictor.get(model_name)(
  46. model_dir=model_dir,
  47. config=config,
  48. device=device,
  49. serial_number=serial_number,
  50. update_license=update_license,
  51. *args,
  52. **kwargs,
  53. )
  54. def create_predictor(
  55. model: str,
  56. device=None,
  57. pp_option=None,
  58. use_hpip=False,
  59. hpi_params=None,
  60. *args,
  61. **kwargs,
  62. ) -> BasePredictor:
  63. model_dir = check_model(model)
  64. config = BasePredictor.load_config(model_dir)
  65. model_name = config["Global"]["model_name"]
  66. if use_hpip:
  67. return _create_hp_predictor(
  68. model_name=model_name,
  69. model_dir=model_dir,
  70. config=config,
  71. hpi_params=hpi_params,
  72. device=device,
  73. *args,
  74. **kwargs,
  75. )
  76. else:
  77. return BasicPredictor.get(model_name)(
  78. model_dir=model_dir,
  79. config=config,
  80. device=device,
  81. pp_option=pp_option,
  82. *args,
  83. **kwargs,
  84. )
  85. def check_model(model):
  86. if Path(model).exists():
  87. return Path(model)
  88. elif model in official_models:
  89. return official_models[model]
  90. else:
  91. raise Exception(
  92. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  93. )