pipeline.py 2.3 KB

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