yolov7.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 YOLOv7Preprocessor:
  19. def __init__(self):
  20. """Create a preprocessor for YOLOv7"""
  21. self._preprocessor = C.vision.detection.YOLOv7Preprocessor()
  22. def run(self, input_ims):
  23. """Preprocess input images for YOLOv7
  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 = [640, 640]
  32. """
  33. return self._preprocessor.size
  34. @property
  35. def padding_value(self):
  36. """
  37. padding value for preprocessing, default [114.0, 114.0, 114.0]
  38. """
  39. # padding value, size should be the same as channels
  40. return self._preprocessor.padding_value
  41. @property
  42. def is_scale_up(self):
  43. """
  44. is_scale_up for preprocessing, the input image only can be zoom out, the maximum resize scale cannot exceed 1.0, default true
  45. """
  46. return self._preprocessor.is_scale_up
  47. @size.setter
  48. def size(self, wh):
  49. assert isinstance(
  50. wh, (list, tuple)
  51. ), "The value to set `size` must be type of tuple or list."
  52. assert (
  53. len(wh) == 2
  54. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  55. len(wh)
  56. )
  57. self._preprocessor.size = wh
  58. @padding_value.setter
  59. def padding_value(self, value):
  60. assert isinstance(
  61. value, list
  62. ), "The value to set `padding_value` must be type of list."
  63. self._preprocessor.padding_value = value
  64. @is_scale_up.setter
  65. def is_scale_up(self, value):
  66. assert isinstance(
  67. value, bool
  68. ), "The value to set `is_scale_up` must be type of bool."
  69. self._preprocessor.is_scale_up = value
  70. class YOLOv7Postprocessor:
  71. def __init__(self):
  72. """Create a postprocessor for YOLOv7"""
  73. self._postprocessor = C.vision.detection.YOLOv7Postprocessor()
  74. def run(self, runtime_results, ims_info):
  75. """Postprocess the runtime results for YOLOv7
  76. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  77. :param: ims_info: (list of dict)Record input_shape and output_shape
  78. :return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  79. """
  80. return self._postprocessor.run(runtime_results, ims_info)
  81. @property
  82. def conf_threshold(self):
  83. """
  84. confidence threshold for postprocessing, default is 0.25
  85. """
  86. return self._postprocessor.conf_threshold
  87. @property
  88. def nms_threshold(self):
  89. """
  90. nms threshold for postprocessing, default is 0.5
  91. """
  92. return self._postprocessor.nms_threshold
  93. @conf_threshold.setter
  94. def conf_threshold(self, conf_threshold):
  95. assert isinstance(
  96. conf_threshold, float
  97. ), "The value to set `conf_threshold` must be type of float."
  98. self._postprocessor.conf_threshold = conf_threshold
  99. @nms_threshold.setter
  100. def nms_threshold(self, nms_threshold):
  101. assert isinstance(
  102. nms_threshold, float
  103. ), "The value to set `nms_threshold` must be type of float."
  104. self._postprocessor.nms_threshold = nms_threshold
  105. class YOLOv7(UltraInferModel):
  106. def __init__(
  107. self,
  108. model_file,
  109. params_file="",
  110. runtime_option=None,
  111. model_format=ModelFormat.ONNX,
  112. ):
  113. """Load a YOLOv7 model exported by YOLOv7.
  114. :param model_file: (str)Path of model file, e.g ./yolov7.onnx
  115. :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
  116. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  117. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  118. """
  119. # 调用基函数进行backend_option的初始化
  120. # 初始化后的option保存在self._runtime_option
  121. super(YOLOv7, self).__init__(runtime_option)
  122. self._model = C.vision.detection.YOLOv7(
  123. model_file, params_file, self._runtime_option, model_format
  124. )
  125. # 通过self.initialized判断整个模型的初始化是否成功
  126. assert self.initialized, "YOLOv7 initialize failed."
  127. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  128. """Detect an input image
  129. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  130. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  131. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  132. :return: DetectionResult
  133. """
  134. self.postprocessor.conf_threshold = conf_threshold
  135. self.postprocessor.nms_threshold = nms_iou_threshold
  136. return self._model.predict(input_image)
  137. def batch_predict(self, images):
  138. """Classify a batch of input image
  139. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  140. :return list of DetectionResult
  141. """
  142. return self._model.batch_predict(images)
  143. @property
  144. def preprocessor(self):
  145. """Get YOLOv7Preprocessor object of the loaded model
  146. :return YOLOv7Preprocessor
  147. """
  148. return self._model.preprocessor
  149. @property
  150. def postprocessor(self):
  151. """Get YOLOv7Postprocessor object of the loaded model
  152. :return YOLOv7Postprocessor
  153. """
  154. return self._model.postprocessor