__init__.py 3.6 KB

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