pipeline.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_classification.result import TopkResult
  21. class ImageClassificationPipeline(BasePipeline):
  22. """Image Classification Pipeline"""
  23. entities = "image_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. image_classification_model_config = config["SubModules"]["ImageClassification"]
  41. model_kwargs = {}
  42. if (topk := image_classification_model_config.get("topk", None)) is not None:
  43. model_kwargs = {"topk": topk}
  44. self.image_classification_model = self.create_model(
  45. image_classification_model_config, **model_kwargs
  46. )
  47. self.topk = image_classification_model_config.get("topk", 5)
  48. def predict(
  49. self, input: Union[str, List[str], np.ndarray, List[np.ndarray]], **kwargs
  50. ) -> TopkResult:
  51. """Predicts image classification 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. TopkResult: The predicted top k results.
  57. """
  58. topk = kwargs.pop("topk", self.topk)
  59. yield from self.image_classification_model(input, topk=topk)