pipeline.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 typing import Any, Dict, List, Optional, Union
  15. import numpy as np
  16. from ....utils.deps import pipeline_requires_extra
  17. from ...models.anomaly_detection.result import UadResult
  18. from ...utils.hpi import HPIConfig
  19. from ...utils.pp_option import PaddlePredictorOption
  20. from .._parallel import AutoParallelImageSimpleInferencePipeline
  21. from ..base import BasePipeline
  22. class _AnomalyDetectionPipeline(BasePipeline):
  23. """Image AnomalyDetectionPipeline Pipeline"""
  24. def __init__(
  25. self,
  26. config: Dict,
  27. device: str = None,
  28. pp_option: PaddlePredictorOption = None,
  29. use_hpip: bool = False,
  30. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  31. ) -> None:
  32. """Initializes the image anomaly detection pipeline.
  33. Args:
  34. config (Dict): Configuration dictionary containing various settings.
  35. device (str, optional): Device to run the predictions on. Defaults to None.
  36. pp_option (PaddlePredictorOption, optional): PaddlePredictor options. Defaults to None.
  37. use_hpip (bool, optional): Whether to use the high-performance
  38. inference plugin (HPIP) by default. Defaults to False.
  39. hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
  40. The default high-performance inference configuration dictionary.
  41. Defaults to None.
  42. """
  43. super().__init__(
  44. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_config=hpi_config
  45. )
  46. anomaly_detetion_model_config = config["SubModules"]["AnomalyDetection"]
  47. self.anomaly_detetion_model = self.create_model(anomaly_detetion_model_config)
  48. def predict(
  49. self, input: Union[str, List[str], np.ndarray, List[np.ndarray]], **kwargs
  50. ) -> UadResult:
  51. """Predicts anomaly detection results for the given input.
  52. Args:
  53. input (Union[str, list[str], np.ndarray, list[np.ndarray]]): The input image(s) or path(s) to the images.
  54. **kwargs: Additional keyword arguments that can be passed to the function.
  55. Returns:
  56. UadResult: The predicted anomaly results.
  57. """
  58. yield from self.anomaly_detetion_model(input)
  59. @pipeline_requires_extra("cv")
  60. class AnomalyDetectionPipeline(AutoParallelImageSimpleInferencePipeline):
  61. entities = "anomaly_detection"
  62. @property
  63. def _pipeline_cls(self):
  64. return _AnomalyDetectionPipeline
  65. def _get_batch_size(self, config):
  66. return config["SubModules"]["AnomalyDetection"].get("batch_size", 1)