__init__.py 3.7 KB

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