pipeline.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.image_multilabel_classification.result import MLClassResult
  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 _ImageMultiLabelClassificationPipeline(BasePipeline):
  23. """Image Multi Label Classification 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. """
  33. Initializes the class with given configurations and options.
  34. Args:
  35. config (Dict): Configuration dictionary containing model and other parameters.
  36. device (str): The device to run the prediction on. Default is None.
  37. pp_option (PaddlePredictorOption): Options for PaddlePaddle predictor. Default is None.
  38. use_hpip (Optional[bool], optional): Whether to use the
  39. high-performance inference plugin (HPIP) by default. Defaults to None.
  40. hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
  41. The high-performance inference configuration dictionary.
  42. Defaults to None.
  43. """
  44. super().__init__(
  45. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_config=hpi_config
  46. )
  47. self.threshold = config["SubModules"]["ImageMultiLabelClassification"].get(
  48. "threshold", None
  49. )
  50. image_multilabel_classification_model_config = config["SubModules"][
  51. "ImageMultiLabelClassification"
  52. ]
  53. self.image_multilabel_classification_model = self.create_model(
  54. image_multilabel_classification_model_config
  55. )
  56. image_multilabel_classification_model_config["batch_size"]
  57. def predict(
  58. self,
  59. input: Union[str, List[str], np.ndarray, List[np.ndarray]],
  60. threshold: Union[float, dict, list, None] = None,
  61. **kwargs
  62. ) -> MLClassResult:
  63. """Predicts image classification results for the given input.
  64. Args:
  65. input (Union[str, list[str], np.ndarray, list[np.ndarray]]): The input image(s) or path(s) to the images.
  66. **kwargs: Additional keyword arguments that can be passed to the function.
  67. Returns:
  68. TopkResult: The predicted top k results.
  69. """
  70. yield from self.image_multilabel_classification_model(
  71. input=input,
  72. threshold=self.threshold if threshold is None else threshold,
  73. )
  74. @pipeline_requires_extra("cv")
  75. class ImageMultiLabelClassificationPipeline(AutoParallelImageSimpleInferencePipeline):
  76. entities = "image_multilabel_classification"
  77. @property
  78. def _pipeline_cls(self):
  79. return _ImageMultiLabelClassificationPipeline
  80. def _get_batch_size(self, config):
  81. return config["SubModules"]["ImageMultiLabelClassification"]["batch_size"]