pipeline.py 2.4 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 InstanceSegPipeline(BasePipeline):
  18. """InstanceSeg Pipeline
  19. """
  20. support_models = "instance_segmentation"
  21. def __init__(self,
  22. model_name=None,
  23. model_dir=None,
  24. output_dir="./output",
  25. kernel_option=None,
  26. **kwargs):
  27. self.model_name = model_name
  28. self.model_dir = model_dir
  29. self.output_dir = output_dir
  30. self.post_transforms = self.get_post_transforms(model_dir)
  31. self.kernel_option = self.get_kernel_option(
  32. ) if kernel_option is None else kernel_option
  33. if self.model_name is not None:
  34. self.load_model()
  35. def load_model(self):
  36. """load model predictor
  37. """
  38. assert self.model_name is not None
  39. self.model = create_model(
  40. self.model_name,
  41. model_dir=self.model_dir,
  42. kernel_option=self.kernel_option,
  43. post_transforms=self.post_transforms)
  44. def predict(self, input_path):
  45. """predict
  46. """
  47. return self.model.predict({"input_path": input_path})
  48. def get_post_transforms(self, model_dir):
  49. """get post transform ops
  50. """
  51. return [T.SaveDetResults(self.output_dir), T.PrintResult()]
  52. def get_kernel_option(self):
  53. """get kernel option
  54. """
  55. kernel_option = PaddleInferenceOption()
  56. kernel_option.set_device("gpu")
  57. return kernel_option
  58. def update_model_name(self, model_name_list):
  59. """update model name and re
  60. Args:
  61. model_list (list): list of model name.
  62. """
  63. assert len(model_name_list) == 1
  64. self.model_name = model_name_list[0]