pipeline.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.semantic_segmentation import transforms as T
  17. class SegPipeline(BasePipeline):
  18. """Seg Pipeline
  19. """
  20. support_models = "semantic_segmentation"
  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. return kernel_option
  55. def update_model_name(self, model_name_list):
  56. """update model name and re
  57. Args:
  58. model_list (list): list of model name.
  59. """
  60. assert len(model_name_list) == 1
  61. self.model_name = model_name_list[0]
  62. def get_input_keys(self):
  63. """get dict keys of input argument input
  64. """
  65. return self.model.get_input_keys()