pipeline.py 2.3 KB

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