__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 typing import Any, Dict, Optional
  16. from importlib import import_module
  17. from ...utils import errors
  18. from ..utils.official_models import official_models
  19. from .base import BasePredictor, BasicPredictor
  20. from .image_classification import ClasPredictor
  21. from .object_detection import DetPredictor
  22. from .keypoint_detection import KptPredictor
  23. from .text_detection import TextDetPredictor
  24. from .text_recognition import TextRecPredictor
  25. from .table_structure_recognition import TablePredictor
  26. from .formula_recognition import FormulaRecPredictor
  27. from .instance_segmentation import InstanceSegPredictor
  28. from .semantic_segmentation import SegPredictor
  29. from .image_feature import ImageFeaturePredictor
  30. from .ts_forecasting import TSFcPredictor
  31. from .ts_anomaly_detection import TSAdPredictor
  32. from .ts_classification import TSClsPredictor
  33. from .image_unwarping import WarpPredictor
  34. from .image_multilabel_classification import MLClasPredictor
  35. from .face_feature import FaceFeaturePredictor
  36. from .open_vocabulary_detection import OVDetPredictor
  37. from .open_vocabulary_segmentation import OVSegPredictor
  38. # from .table_recognition import TablePredictor
  39. # from .general_recognition import ShiTuRecPredictor
  40. from .anomaly_detection import UadPredictor
  41. # from .face_recognition import FaceRecPredictor
  42. from .multilingual_speech_recognition import WhisperPredictor
  43. from .video_classification import VideoClasPredictor
  44. from .video_detection import VideoDetPredictor
  45. module_3d_bev_detection = import_module(".3d_bev_detection", "paddlex.inference.models")
  46. BEVDet3DPredictor = getattr(module_3d_bev_detection, "BEVDet3DPredictor")
  47. def _create_hp_predictor(
  48. model_name, model_dir, device, config, hpi_params, *args, **kwargs
  49. ):
  50. try:
  51. from paddlex_hpi.models import HPPredictor
  52. except ModuleNotFoundError:
  53. raise RuntimeError(
  54. "The PaddleX HPI plugin is not properly installed, and the high-performance model inference features are not available."
  55. ) from None
  56. try:
  57. predictor = HPPredictor.get(model_name)(
  58. model_dir=model_dir,
  59. config=config,
  60. device=device,
  61. *args,
  62. hpi_params=hpi_params,
  63. **kwargs,
  64. )
  65. except errors.others.ClassNotFoundException:
  66. raise ValueError(
  67. f"{model_name} is not supported by the PaddleX HPI plugin."
  68. ) from None
  69. return predictor
  70. def create_predictor(
  71. model_name: str,
  72. model_dir: Optional[str] = None,
  73. device=None,
  74. pp_option=None,
  75. use_hpip: bool = False,
  76. hpi_params: Optional[Dict[str, Any]] = None,
  77. *args,
  78. **kwargs,
  79. ) -> BasePredictor:
  80. if model_dir is None:
  81. model_dir = check_model(model_name)
  82. else:
  83. assert Path(model_dir).exists(), f"{model_dir} is not exists!"
  84. model_dir = Path(model_dir)
  85. config = BasePredictor.load_config(model_dir)
  86. assert (
  87. model_name == config["Global"]["model_name"]
  88. ), f"Model name mismatch,please input the correct model dir."
  89. if use_hpip:
  90. return _create_hp_predictor(
  91. model_name=model_name,
  92. model_dir=model_dir,
  93. config=config,
  94. hpi_params=hpi_params,
  95. device=device,
  96. *args,
  97. **kwargs,
  98. )
  99. else:
  100. return BasicPredictor.get(model_name)(
  101. model_dir=model_dir,
  102. config=config,
  103. device=device,
  104. pp_option=pp_option,
  105. *args,
  106. **kwargs,
  107. )
  108. def check_model(model):
  109. if Path(model).exists():
  110. return Path(model)
  111. elif model in official_models:
  112. return official_models[model]
  113. else:
  114. raise Exception(
  115. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  116. )