pipeline.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 typing import Any, Dict, Optional, Union, List
  15. import numpy as np
  16. from ...common.reader import ReadImage
  17. from ...common.batch_sampler import ImageBatchSampler
  18. from ...utils.pp_option import PaddlePredictorOption
  19. from ..base import BasePipeline
  20. from ...models.image_multilabel_classification.result import MLClassResult
  21. class ImageMultiLabelClassificationPipeline(BasePipeline):
  22. """Image Multi Label Classification Pipeline"""
  23. entities = "image_multilabel_classification"
  24. def __init__(
  25. self,
  26. config: Dict,
  27. device: str = None,
  28. pp_option: PaddlePredictorOption = None,
  29. use_hpip: bool = False,
  30. ) -> None:
  31. """
  32. Initializes the class with given configurations and options.
  33. Args:
  34. config (Dict): Configuration dictionary containing model and other parameters.
  35. device (str): The device to run the prediction on. Default is None.
  36. pp_option (PaddlePredictorOption): Options for PaddlePaddle predictor. Default is None.
  37. use_hpip (bool): Whether to use high-performance inference (hpip) for prediction. Defaults to False.
  38. """
  39. super().__init__(device=device, pp_option=pp_option, use_hpip=use_hpip)
  40. self.threshold = config["SubModules"]["ImageMultiLabelClassification"].get(
  41. "threshold", None
  42. )
  43. image_multilabel_classification_model_config = config["SubModules"][
  44. "ImageMultiLabelClassification"
  45. ]
  46. self.image_multilabel_classification_model = self.create_model(
  47. image_multilabel_classification_model_config
  48. )
  49. batch_size = image_multilabel_classification_model_config["batch_size"]
  50. def predict(
  51. self,
  52. input: Union[str, List[str], np.ndarray, List[np.ndarray]],
  53. threshold: Union[float, dict, list, None] = None,
  54. **kwargs
  55. ) -> MLClassResult:
  56. """Predicts image classification results for the given input.
  57. Args:
  58. input (Union[str, list[str], np.ndarray, list[np.ndarray]]): The input image(s) or path(s) to the images.
  59. **kwargs: Additional keyword arguments that can be passed to the function.
  60. Returns:
  61. TopkResult: The predicted top k results.
  62. """
  63. yield from self.image_multilabel_classification_model(
  64. input=input,
  65. threshold=self.threshold if threshold is None else threshold,
  66. )