__init__.py 3.9 KB

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