pipeline.py 2.4 KB

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