__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, Mapping, Optional
  16. from ...utils.config import parse_config
  17. from ..utils.get_pipeline_path import get_pipeline_path
  18. from .base import BasePipeline
  19. from .single_model_pipeline import (
  20. _SingleModelPipeline,
  21. ImageClassification,
  22. ObjectDetection,
  23. InstanceSegmentation,
  24. SemanticSegmentation,
  25. TSFc,
  26. TSAd,
  27. TSCls,
  28. MultiLableImageClas,
  29. SmallObjDet,
  30. AnomalyDetection,
  31. )
  32. from .ocr import OCRPipeline
  33. from .formula_recognition import FormulaRecognitionPipeline
  34. from .table_recognition import TableRecPipeline
  35. from .face_recognition import FaceRecPipeline
  36. from .seal_recognition import SealOCRPipeline
  37. from .ppchatocrv3 import PPChatOCRPipeline
  38. from .layout_parsing import LayoutParsingPipeline
  39. from .pp_shitu_v2 import ShiTuV2Pipeline
  40. from .attribute_recognition import (
  41. PedestrianAttributeRecPipeline,
  42. VehicleAttributeRecPipeline,
  43. )
  44. def load_pipeline_config(pipeline: str) -> Dict[str, Any]:
  45. if not Path(pipeline).exists():
  46. pipeline_path = get_pipeline_path(pipeline)
  47. if pipeline_path is None:
  48. raise Exception(
  49. f"The pipeline ({pipeline}) does not exist! Please use a pipeline name or a config file path!"
  50. )
  51. else:
  52. pipeline_path = pipeline
  53. config = parse_config(pipeline_path)
  54. return config
  55. def create_pipeline_from_config(
  56. config: Mapping[str, Any],
  57. device=None,
  58. pp_option=None,
  59. use_hpip: bool = False,
  60. hpi_params: Optional[Dict[str, Any]] = None,
  61. *args,
  62. **kwargs,
  63. ) -> BasePipeline:
  64. pipeline_name = config["Global"]["pipeline_name"]
  65. pipeline_setting = config["Pipeline"]
  66. predictor_kwargs = {"use_hpip": use_hpip}
  67. if "use_hpip" in pipeline_setting:
  68. predictor_kwargs["use_hpip"] = use_hpip
  69. pipeline_setting.pop("use_hpip", None)
  70. if hpi_params is not None:
  71. predictor_kwargs["hpi_params"] = hpi_params
  72. pipeline_setting.pop("hpi_params", None)
  73. elif "hpi_params" in pipeline_setting:
  74. predictor_kwargs["hpi_params"] = pipeline_setting.pop("hpi_params")
  75. if pp_option is not None:
  76. predictor_kwargs["pp_option"] = pp_option
  77. pipeline_setting.pop("pp_option", None)
  78. elif "pp_option" in pipeline_setting:
  79. predictor_kwargs["pp_option"] = pipeline_setting.pop("pp_option")
  80. if device:
  81. pipeline_setting.pop("device", None)
  82. else:
  83. device = pipeline_setting.pop("device", None)
  84. pipeline_setting.update(kwargs)
  85. pipeline = BasePipeline.get(pipeline_name)(
  86. device=device, predictor_kwargs=predictor_kwargs, *args, **pipeline_setting
  87. )
  88. return pipeline
  89. def create_pipeline(
  90. pipeline: str,
  91. device=None,
  92. pp_option=None,
  93. use_hpip: bool = False,
  94. hpi_params: Optional[Dict[str, Any]] = None,
  95. *args,
  96. **kwargs,
  97. ) -> BasePipeline:
  98. """build model evaluater
  99. Args:
  100. pipeline (str): the pipeline name, that is name of pipeline class
  101. Returns:
  102. BasePipeline: the pipeline, which is subclass of BasePipeline.
  103. """
  104. config = load_pipeline_config(pipeline)
  105. return create_pipeline_from_config(
  106. config,
  107. device=device,
  108. pp_option=pp_option,
  109. use_hpip=use_hpip,
  110. hpi_params=hpi_params,
  111. *args,
  112. **kwargs,
  113. )