__init__.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.config import parse_config
  17. from .base import BasePipeline
  18. from .single_model_pipeline import (
  19. _SingleModelPipeline,
  20. ImageClassification,
  21. ObjectDetection,
  22. InstanceSegmentation,
  23. SemanticSegmentation,
  24. TSFc,
  25. TSAd,
  26. TSCls,
  27. MultiLableImageClas,
  28. SmallObjDet,
  29. AnomolyDetection,
  30. )
  31. from .ocr import OCRPipeline
  32. from .table_recognition import TableRecPipeline
  33. def create_pipeline(
  34. pipeline: str,
  35. device=None,
  36. pp_option=None,
  37. use_hpip: bool = False,
  38. hpi_params: Optional[Dict[str, Any]] = None,
  39. *args,
  40. **kwargs,
  41. ) -> BasePipeline:
  42. """build model evaluater
  43. Args:
  44. pipeline (str): the pipeline name, that is name of pipeline class
  45. Returns:
  46. BasePipeline: the pipeline, which is subclass of BasePipeline.
  47. """
  48. if not Path(pipeline).exists():
  49. # XXX: using dict class to handle all pipeline configs
  50. build_in_pipeline = (
  51. Path(__file__).parent.parent.parent / "pipelines" / f"{pipeline}.yaml"
  52. )
  53. if not Path(build_in_pipeline).exists():
  54. raise Exception(f"The pipeline don't exist! ({pipeline})")
  55. pipeline = build_in_pipeline
  56. config = parse_config(pipeline)
  57. pipeline_name = config["Global"]["pipeline_name"]
  58. pipeline_setting = config["Pipeline"]
  59. predictor_kwargs = {"use_hpip": use_hpip}
  60. if "use_hpip" in pipeline_setting:
  61. predictor_kwargs["use_hpip"] = use_hpip
  62. if hpi_params is not None:
  63. predictor_kwargs["hpi_params"] = hpi_params
  64. elif "hpi_params" in pipeline_setting:
  65. predictor_kwargs["hpi_params"] = pipeline_setting.pop("hpi_params")
  66. if device is not None:
  67. predictor_kwargs["device"] = device
  68. elif "device" in pipeline_setting:
  69. predictor_kwargs["device"] = pipeline_setting.pop("device")
  70. if pp_option is not None:
  71. predictor_kwargs["pp_option"] = pp_option
  72. elif "pp_option" in pipeline_setting:
  73. predictor_kwargs["pp_option"] = pipeline_setting.pop("pp_option")
  74. pipeline_setting.update(kwargs)
  75. pipeline = BasePipeline.get(pipeline_name)(
  76. predictor_kwargs=predictor_kwargs, *args, **pipeline_setting
  77. )
  78. return pipeline