pipeline.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ..base import BasePipeline
  15. from ...modules.image_classification.model_list import MODELS
  16. from ...modules import create_model, PaddleInferenceOption
  17. from ...modules.image_classification import transforms as T
  18. class ClsPipeline(BasePipeline):
  19. """Cls Pipeline
  20. """
  21. entities = "image_classification"
  22. def __init__(self,
  23. model_name=None,
  24. model_dir=None,
  25. output=None,
  26. kernel_option=None,
  27. device="gpu",
  28. **kwargs):
  29. super().__init__()
  30. self.model_name = model_name
  31. self.model_dir = model_dir
  32. self.output = output
  33. self.device = device
  34. self.kernel_option = kernel_option
  35. if self.model_name is not None:
  36. self.load_model()
  37. def check_model_name(self):
  38. """ check that model name is valid
  39. """
  40. assert self.model_name in MODELS, f"The model name({self.model_name}) error. Only support: {MODELS}."
  41. def predict(self, input):
  42. """predict
  43. """
  44. return self.model.predict(input)
  45. def load_model(self):
  46. """load model predictor
  47. """
  48. self.check_model_name()
  49. kernel_option = self.get_kernel_option(
  50. ) if self.kernel_option is None else self.kernel_option
  51. self.model = create_model(
  52. model_name=self.model_name,
  53. model_dir=self.model_dir,
  54. output=self.output,
  55. kernel_option=kernel_option)
  56. def get_kernel_option(self):
  57. """get kernel option
  58. """
  59. kernel_option = PaddleInferenceOption()
  60. kernel_option.set_device(self.device)
  61. return kernel_option
  62. def update_model_name(self, model_name_list):
  63. """update model name and re
  64. Args:
  65. model_list (list): list of model name.
  66. """
  67. assert len(model_name_list) == 1
  68. self.model_name = model_name_list[0]
  69. def get_input_keys(self):
  70. """get dict keys of input argument input
  71. """
  72. return self.model.get_input_keys()