pipeline.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. entities = "image_classification"
  21. def __init__(
  22. self,
  23. model_name=None,
  24. model_dir=None,
  25. output="./output",
  26. kernel_option=None,
  27. device="gpu",
  28. **kwargs,
  29. ):
  30. super().__init__(**kwargs)
  31. self.model_name = model_name
  32. self.model_dir = model_dir
  33. self.output = output
  34. self.device = device
  35. self.kernel_option = kernel_option
  36. if self.model_name is not None:
  37. self.load_model()
  38. def check_model_name(self):
  39. """check that model name is valid"""
  40. assert (
  41. self.model_name in MODELS
  42. ), f"The model name({self.model_name}) error. Only support: {MODELS}."
  43. def predict(self, input):
  44. """predict"""
  45. return self.model.predict(input)
  46. def load_model(self):
  47. """load model predictor"""
  48. self.check_model_name()
  49. kernel_option = (
  50. self.get_kernel_option()
  51. if self.kernel_option is None
  52. else self.kernel_option
  53. )
  54. self.model = create_model(
  55. model_name=self.model_name,
  56. model_dir=self.model_dir,
  57. output=self.output,
  58. kernel_option=kernel_option,
  59. disable_print=self.disable_print,
  60. disable_save=self.disable_save,
  61. )
  62. def get_kernel_option(self):
  63. """get kernel option"""
  64. kernel_option = PaddleInferenceOption()
  65. kernel_option.set_device(self.device)
  66. return kernel_option
  67. def update_model(self, model_name_list, model_dir_list):
  68. """update model
  69. Args:
  70. model_name_list (list): list of model name.
  71. model_dir_list (list): list of model directory.
  72. """
  73. assert len(model_name_list) == 1
  74. self.model_name = model_name_list[0]
  75. if model_dir_list:
  76. assert len(model_dir_list) == 1
  77. self.model_dir = model_dir_list[0]
  78. def get_input_keys(self):
  79. """get dict keys of input argument input"""
  80. return self.model.get_input_keys()