pipeline.py 2.8 KB

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