fastestdet.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 FastestDetPreprocessor:
  19. def __init__(self):
  20. """Create a preprocessor for FastestDet"""
  21. self._preprocessor = C.vision.detection.FastestDetPreprocessor()
  22. def run(self, input_ims):
  23. """Preprocess input images for FastestDet
  24. :param: input_ims: (list of numpy.ndarray)The input image
  25. :return: list of FDTensor
  26. """
  27. return self._preprocessor.run(input_ims)
  28. @property
  29. def size(self):
  30. """
  31. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [352, 352]
  32. """
  33. return self._preprocessor.size
  34. @size.setter
  35. def size(self, wh):
  36. assert isinstance(
  37. wh, (list, tuple)
  38. ), "The value to set `size` must be type of tuple or list."
  39. assert (
  40. len(wh) == 2
  41. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  42. len(wh)
  43. )
  44. self._preprocessor.size = wh
  45. class FastestDetPostprocessor:
  46. def __init__(self):
  47. """Create a postprocessor for FastestDet"""
  48. self._postprocessor = C.vision.detection.FastestDetPostprocessor()
  49. def run(self, runtime_results, ims_info):
  50. """Postprocess the runtime results for FastestDet
  51. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  52. :param: ims_info: (list of dict)Record input_shape and output_shape
  53. :return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  54. """
  55. return self._postprocessor.run(runtime_results, ims_info)
  56. @property
  57. def conf_threshold(self):
  58. """
  59. confidence threshold for postprocessing, default is 0.65
  60. """
  61. return self._postprocessor.conf_threshold
  62. @property
  63. def nms_threshold(self):
  64. """
  65. nms threshold for postprocessing, default is 0.45
  66. """
  67. return self._postprocessor.nms_threshold
  68. @conf_threshold.setter
  69. def conf_threshold(self, conf_threshold):
  70. assert isinstance(
  71. conf_threshold, float
  72. ), "The value to set `conf_threshold` must be type of float."
  73. self._postprocessor.conf_threshold = conf_threshold
  74. @nms_threshold.setter
  75. def nms_threshold(self, nms_threshold):
  76. assert isinstance(
  77. nms_threshold, float
  78. ), "The value to set `nms_threshold` must be type of float."
  79. self._postprocessor.nms_threshold = nms_threshold
  80. class FastestDet(UltraInferModel):
  81. def __init__(
  82. self,
  83. model_file,
  84. params_file="",
  85. runtime_option=None,
  86. model_format=ModelFormat.ONNX,
  87. ):
  88. """Load a FastestDet model exported by FastestDet.
  89. :param model_file: (str)Path of model file, e.g ./FastestDet.onnx
  90. :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
  91. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  92. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  93. """
  94. super(FastestDet, self).__init__(runtime_option)
  95. assert (
  96. model_format == ModelFormat.ONNX
  97. ), "FastestDet only support model format of ModelFormat.ONNX now."
  98. self._model = C.vision.detection.FastestDet(
  99. model_file, params_file, self._runtime_option, model_format
  100. )
  101. assert self.initialized, "FastestDet initialize failed."
  102. def predict(self, input_image):
  103. """Detect an input image
  104. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  105. :return: DetectionResult
  106. """
  107. assert input_image is not None, "Input image is None."
  108. return self._model.predict(input_image)
  109. def batch_predict(self, images):
  110. assert len(images) == 1, "FastestDet is only support 1 image in batch_predict"
  111. """Classify a batch of input image
  112. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  113. :return list of DetectionResult
  114. """
  115. return self._model.batch_predict(images)
  116. @property
  117. def preprocessor(self):
  118. """Get FastestDetPreprocessor object of the loaded model
  119. :return FastestDetPreprocessor
  120. """
  121. return self._model.preprocessor
  122. @property
  123. def postprocessor(self):
  124. """Get FastestDetPostprocessor object of the loaded model
  125. :return FastestDetPostprocessor
  126. """
  127. return self._model.postprocessor