__init__.py 3.5 KB

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