__init__.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 .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_cls import TSClsPredictor
  27. def _create_hp_predictor(
  28. model_name, model_dir, device, config, hpi_params, *args, **kwargs
  29. ):
  30. try:
  31. from paddlex_hpi.predictors import HPPredictor
  32. except ModuleNotFoundError as e:
  33. raise RuntimeError(
  34. "The PaddleX HPI plugin is not properly installed, and the high-performance model inference features are not available."
  35. )
  36. if hpi_params is None:
  37. raise ValueError("No HPI params given")
  38. if "serial_number" not in hpi_params:
  39. raise ValueError("The serial number is required but was not provided.")
  40. serial_number = hpi_params["serial_number"]
  41. update_license = hpi_params.get("update_license", False)
  42. return HPPredictor.get(model_name)(
  43. model_dir=model_dir,
  44. config=config,
  45. device=device,
  46. serial_number=serial_number,
  47. update_license=update_license,
  48. *args,
  49. **kwargs,
  50. )
  51. def create_predictor(
  52. model: str, device: str = None, *args, use_hpip=False, hpi_params=None, **kwargs
  53. ) -> BasePredictor:
  54. model_dir = check_model(model)
  55. config = BasePredictor.load_config(model_dir)
  56. model_name = config["Global"]["model_name"]
  57. if use_hpip:
  58. return _create_hp_predictor(
  59. model_name=model_name,
  60. model_dir=model_dir,
  61. device=device,
  62. config=config,
  63. hpi_params=hpi_params,
  64. *args,
  65. **kwargs,
  66. )
  67. else:
  68. return BasicPredictor.get(model_name)(
  69. model_dir=model_dir,
  70. config=config,
  71. device=device,
  72. *args,
  73. **kwargs,
  74. )
  75. def check_model(model):
  76. if Path(model).exists():
  77. return Path(model)
  78. elif model in official_models:
  79. return official_models[model]
  80. else:
  81. raise Exception(
  82. f"The model ({model}) is no exists! Please using directory of local model files or model name supported by PaddleX!"
  83. )