pipeline.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.object_detection.model_list import MODELS
  16. from ...modules import create_model, PaddleInferenceOption
  17. from ...modules.object_detection import transforms as T
  18. class DetPipeline(BasePipeline):
  19. """Det Pipeline"""
  20. entities = "object_detection"
  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. 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. assert (
  40. self.model_name in MODELS
  41. ), f"The model name({self.model_name}) error. Only support: {MODELS}."
  42. def load_model(self):
  43. """load model predictor"""
  44. self.check_model_name()
  45. kernel_option = (
  46. self.get_kernel_option()
  47. if self.kernel_option is None
  48. else self.kernel_option
  49. )
  50. self.model = create_model(
  51. model_name=self.model_name,
  52. model_dir=self.model_dir,
  53. output=self.output,
  54. kernel_option=kernel_option,
  55. )
  56. def predict(self, input):
  57. """predict"""
  58. return self.model.predict(input)
  59. def get_kernel_option(self):
  60. """get kernel option"""
  61. kernel_option = PaddleInferenceOption()
  62. kernel_option.set_device(self.device)
  63. def update_model(self, model_name_list, model_dir_list):
  64. """update model
  65. Args:
  66. model_name_list (list): list of model name.
  67. model_dir_list (list): list of model directory.
  68. """
  69. assert len(model_name_list) == 1
  70. self.model_name = model_name_list[0]
  71. if model_dir_list:
  72. assert len(model_dir_list) == 1
  73. self.model_dir = model_dir_list[0]
  74. def get_input_keys(self):
  75. """get dict keys of input argument input"""
  76. return self.model.get_input_keys()