__init__.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 ..utils.official_models import official_models
  16. from .base import BasePredictor, BasicPredictor
  17. from .image_classification import ClasPredictor
  18. from .text_detection import TextDetPredictor
  19. from .text_recognition import TextRecPredictor
  20. from .table_recognition import TablePredictor
  21. from .object_detection import DetPredictor
  22. from .instance_segmentation import InstanceSegPredictor
  23. from .semantic_segmentation import SegPredictor
  24. from .general_recognition import ShiTuRecPredictor
  25. from .ts_fc import TSFcPredictor
  26. from .ts_ad import TSAdPredictor
  27. from .ts_cls import TSClsPredictor
  28. from .image_unwarping import WarpPredictor
  29. def _create_hp_predictor(
  30. model_name, model_dir, device, config, hpi_params, *args, **kwargs
  31. ):
  32. try:
  33. from paddlex_hpi.predictors import HPPredictor
  34. except ModuleNotFoundError as e:
  35. raise RuntimeError(
  36. "The PaddleX HPI plugin is not properly installed, and the high-performance model inference features are not available."
  37. )
  38. if hpi_params is None:
  39. raise ValueError("No HPI params given")
  40. if "serial_number" not in hpi_params:
  41. raise ValueError("The serial number is required but was not provided.")
  42. serial_number = hpi_params["serial_number"]
  43. update_license = hpi_params.get("update_license", False)
  44. return HPPredictor.get(model_name)(
  45. model_dir=model_dir,
  46. config=config,
  47. device=device,
  48. serial_number=serial_number,
  49. update_license=update_license,
  50. *args,
  51. **kwargs,
  52. )
  53. def create_model(
  54. model: str, device: str = None, *args, use_hpip=False, hpi_params=None, **kwargs
  55. ) -> BasePredictor:
  56. model_dir = check_model(model)
  57. config = BasePredictor.load_config(model_dir)
  58. model_name = config["Global"]["model_name"]
  59. if use_hpip:
  60. return _create_hp_predictor(
  61. model_name=model_name,
  62. model_dir=model_dir,
  63. device=device,
  64. config=config,
  65. hpi_params=hpi_params,
  66. *args,
  67. **kwargs,
  68. )
  69. else:
  70. return BasicPredictor.get(model_name)(
  71. model_dir=model_dir,
  72. config=config,
  73. device=device,
  74. *args,
  75. **kwargs,
  76. )
  77. def check_model(model):
  78. if Path(model).exists():
  79. return Path(model)
  80. elif model in official_models:
  81. return official_models[model]
  82. else:
  83. raise Exception(
  84. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  85. )