modnet.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 __future__ import absolute_import
  15. import logging
  16. from .... import UltraInferModel, ModelFormat
  17. from .... import c_lib_wrap as C
  18. class MODNet(UltraInferModel):
  19. def __init__(
  20. self,
  21. model_file,
  22. params_file="",
  23. runtime_option=None,
  24. model_format=ModelFormat.ONNX,
  25. ):
  26. """Load a MODNet model exported by MODNet.
  27. :param model_file: (str)Path of model file, e.g ./modnet.onnx
  28. :param params_file: (str)Path of parameters file, e.g yolox/model.pdiparams, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  29. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  30. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  31. """
  32. # 调用基函数进行backend_option的初始化
  33. # 初始化后的option保存在self._runtime_option
  34. super(MODNet, self).__init__(runtime_option)
  35. self._model = C.vision.matting.MODNet(
  36. model_file, params_file, self._runtime_option, model_format
  37. )
  38. # 通过self.initialized判断整个模型的初始化是否成功
  39. assert self.initialized, "MODNet initialize failed."
  40. def predict(self, input_image):
  41. """Predict the matting result for an input image
  42. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  43. :return: MattingResult
  44. """
  45. return self._model.predict(input_image)
  46. # 一些跟模型有关的属性封装
  47. # 多数是预处理相关,可通过修改如model.size = [256, 256]改变预处理时resize的大小(前提是模型支持)
  48. @property
  49. def size(self):
  50. """
  51. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [256,256]
  52. """
  53. return self._model.size
  54. @property
  55. def alpha(self):
  56. """
  57. Argument for image preprocessing step, alpha value for normalization, default alpha = {1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f}
  58. """
  59. return self._model.alpha
  60. @property
  61. def beta(self):
  62. """
  63. Argument for image preprocessing step, beta value for normalization, default beta = {-1.f, -1.f, -1.f}
  64. """
  65. return self._model.beta
  66. @property
  67. def swap_rb(self):
  68. """
  69. Argument for image preprocessing step, whether to swap the B and R channel, such as BGR->RGB, default True.
  70. """
  71. return self._model.swap_rb
  72. @size.setter
  73. def size(self, wh):
  74. assert isinstance(
  75. wh, (list, tuple)
  76. ), "The value to set `size` must be type of tuple or list."
  77. assert (
  78. len(wh) == 2
  79. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  80. len(wh)
  81. )
  82. self._model.size = wh
  83. @alpha.setter
  84. def alpha(self, value):
  85. assert isinstance(
  86. value, (list, tuple)
  87. ), "The value to set `alpha` must be type of tuple or list."
  88. assert (
  89. len(value) == 3
  90. ), "The value to set `alpha` must contatins 3 elements for each channels, but now it contains {} elements.".format(
  91. len(value)
  92. )
  93. self._model.alpha = value
  94. @beta.setter
  95. def beta(self, value):
  96. assert isinstance(
  97. value, (list, tuple)
  98. ), "The value to set `beta` must be type of tuple or list."
  99. assert (
  100. len(value) == 3
  101. ), "The value to set `beta` must contatins 3 elements for each channels, but now it contains {} elements.".format(
  102. len(value)
  103. )
  104. self._model.beta = value
  105. @swap_rb.setter
  106. def swap_rb(self, value):
  107. assert isinstance(
  108. value, bool
  109. ), "The value to set `swap_rb` must be type of bool."
  110. self._model.swap_rb = value