rkyolov5.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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. from ..... import UltraInferModel, ModelFormat
  16. from ..... import c_lib_wrap as C
  17. class RKYOLOPreprocessor:
  18. def __init__(self):
  19. """Create a preprocessor for RKYOLOV5"""
  20. self._preprocessor = C.vision.detection.RKYOLOPreprocessor()
  21. def run(self, input_ims):
  22. """Preprocess input images for RKYOLOV5
  23. :param: input_ims: (list of numpy.ndarray)The input image
  24. :return: list of FDTensor
  25. """
  26. return self._preprocessor.run(input_ims)
  27. @property
  28. def size(self):
  29. """
  30. Argument for image preprocessing step, the preprocess image size, tuple of (width, height), default size = [640, 640]
  31. """
  32. return self._preprocessor.size
  33. @property
  34. def padding_value(self):
  35. """
  36. padding value for preprocessing, default [114.0, 114.0, 114.0]
  37. """
  38. # padding value, size should be the same as channels
  39. return self._preprocessor.padding_value
  40. @property
  41. def is_scale_up(self):
  42. """
  43. is_scale_up for preprocessing, the input image only can be zoom out, the maximum resize scale cannot exceed 1.0, default true
  44. """
  45. return self._preprocessor.is_scale_up
  46. @size.setter
  47. def size(self, wh):
  48. assert isinstance(
  49. wh, (list, tuple)
  50. ), "The value to set `size` must be type of tuple or list."
  51. assert (
  52. len(wh) == 2
  53. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  54. len(wh)
  55. )
  56. self._preprocessor.size = wh
  57. @padding_value.setter
  58. def padding_value(self, value):
  59. assert isinstance(
  60. value, list
  61. ), "The value to set `padding_value` must be type of list."
  62. self._preprocessor.padding_value = value
  63. @is_scale_up.setter
  64. def is_scale_up(self, value):
  65. assert isinstance(
  66. value, bool
  67. ), "The value to set `is_scale_up` must be type of bool."
  68. self._preprocessor.is_scale_up = value
  69. class RKYOLOPostprocessor:
  70. def __init__(self):
  71. """Create a postprocessor for RKYOLOV5"""
  72. self._postprocessor = C.vision.detection.RKYOLOPostprocessor()
  73. def run(self, runtime_results):
  74. """Postprocess the runtime results for RKYOLOV5
  75. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  76. :param: ims_info: (list of dict)Record input_shape and output_shape
  77. :return: list of DetectionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  78. """
  79. return self._postprocessor.run(runtime_results)
  80. def set_anchor(self, anchor):
  81. self._postprocessor.set_anchor(anchor)
  82. @property
  83. def conf_threshold(self):
  84. """
  85. confidence threshold for postprocessing, default is 0.25
  86. """
  87. return self._postprocessor.conf_threshold
  88. @property
  89. def nms_threshold(self):
  90. """
  91. nms threshold for postprocessing, default is 0.5
  92. """
  93. return self._postprocessor.nms_threshold
  94. @property
  95. def class_num(self):
  96. """
  97. class_num for postprocessing, default is 80
  98. """
  99. return self._postprocessor.class_num
  100. @conf_threshold.setter
  101. def conf_threshold(self, conf_threshold):
  102. assert isinstance(
  103. conf_threshold, float
  104. ), "The value to set `conf_threshold` must be type of float."
  105. self._postprocessor.conf_threshold = conf_threshold
  106. @nms_threshold.setter
  107. def nms_threshold(self, nms_threshold):
  108. assert isinstance(
  109. nms_threshold, float
  110. ), "The value to set `nms_threshold` must be type of float."
  111. self._postprocessor.nms_threshold = nms_threshold
  112. @class_num.setter
  113. def class_num(self, class_num):
  114. """
  115. class_num for postprocessing, default is 80
  116. """
  117. assert isinstance(
  118. class_num, int
  119. ), "The value to set `nms_threshold` must be type of float."
  120. self._postprocessor.class_num = class_num
  121. class RKYOLOV5(UltraInferModel):
  122. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  123. """Load a RKYOLOV5 model exported by RKYOLOV5.
  124. :param model_file: (str)Path of model file, e.g ./yolov5.rknn
  125. :param params_file: (str)Path of parameters file, e.g , if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  126. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  127. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  128. """
  129. # 调用基函数进行backend_option的初始化
  130. # 初始化后的option保存在self._runtime_option
  131. super(RKYOLOV5, self).__init__(runtime_option)
  132. self._model = C.vision.detection.RKYOLOV5(
  133. model_file, self._runtime_option, model_format
  134. )
  135. # 通过self.initialized判断整个模型的初始化是否成功
  136. assert self.initialized, "RKYOLOV5 initialize failed."
  137. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  138. """Detect an input image
  139. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  140. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  141. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  142. :return: DetectionResult
  143. """
  144. self.postprocessor.conf_threshold = conf_threshold
  145. self.postprocessor.nms_threshold = nms_iou_threshold
  146. return self._model.predict(input_image)
  147. def batch_predict(self, images):
  148. """Classify a batch of input image
  149. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  150. :return list of DetectionResult
  151. """
  152. return self._model.batch_predict(images)
  153. @property
  154. def preprocessor(self):
  155. """Get RKYOLOV5Preprocessor object of the loaded model
  156. :return RKYOLOV5Preprocessor
  157. """
  158. return self._model.preprocessor
  159. @property
  160. def postprocessor(self):
  161. """Get RKYOLOV5Postprocessor object of the loaded model
  162. :return RKYOLOV5Postprocessor
  163. """
  164. return self._model.postprocessor
  165. class RKYOLOX(UltraInferModel):
  166. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  167. """Load a RKYOLOX model exported by RKYOLOX.
  168. :param model_file: (str)Path of model file, e.g ./yolox.rknn
  169. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  170. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  171. """
  172. # 调用基函数进行backend_option的初始化
  173. # 初始化后的option保存在self._runtime_option
  174. super(RKYOLOX, self).__init__(runtime_option)
  175. self._model = C.vision.detection.RKYOLOX(
  176. model_file, self._runtime_option, model_format
  177. )
  178. # 通过self.initialized判断整个模型的初始化是否成功
  179. assert self.initialized, "RKYOLOV5 initialize failed."
  180. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  181. """Detect an input image
  182. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  183. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  184. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  185. :return: DetectionResult
  186. """
  187. self.postprocessor.conf_threshold = conf_threshold
  188. self.postprocessor.nms_threshold = nms_iou_threshold
  189. return self._model.predict(input_image)
  190. def batch_predict(self, images):
  191. """Classify a batch of input image
  192. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  193. :return list of DetectionResult
  194. """
  195. return self._model.batch_predict(images)
  196. @property
  197. def preprocessor(self):
  198. """Get RKYOLOV5Preprocessor object of the loaded model
  199. :return RKYOLOV5Preprocessor
  200. """
  201. return self._model.preprocessor
  202. @property
  203. def postprocessor(self):
  204. """Get RKYOLOV5Postprocessor object of the loaded model
  205. :return RKYOLOV5Postprocessor
  206. """
  207. return self._model.postprocessor
  208. class RKYOLOV7(UltraInferModel):
  209. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  210. """Load a RKYOLOX model exported by RKYOLOV7.
  211. :param model_file: (str)Path of model file, e.g ./yolov7.rknn
  212. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  213. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  214. """
  215. # 调用基函数进行backend_option的初始化
  216. # 初始化后的option保存在self._runtime_option
  217. super(RKYOLOV7, self).__init__(runtime_option)
  218. self._model = C.vision.detection.RKYOLOV7(
  219. model_file, self._runtime_option, model_format
  220. )
  221. # 通过self.initialized判断整个模型的初始化是否成功
  222. assert self.initialized, "RKYOLOV5 initialize failed."
  223. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  224. """Detect an input image
  225. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  226. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  227. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  228. :return: DetectionResult
  229. """
  230. self.postprocessor.conf_threshold = conf_threshold
  231. self.postprocessor.nms_threshold = nms_iou_threshold
  232. return self._model.predict(input_image)
  233. def batch_predict(self, images):
  234. """Classify a batch of input image
  235. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  236. :return list of DetectionResult
  237. """
  238. return self._model.batch_predict(images)
  239. @property
  240. def preprocessor(self):
  241. """Get RKYOLOV5Preprocessor object of the loaded model
  242. :return RKYOLOV5Preprocessor
  243. """
  244. return self._model.preprocessor
  245. @property
  246. def postprocessor(self):
  247. """Get RKYOLOV5Postprocessor object of the loaded model
  248. :return RKYOLOV5Postprocessor
  249. """
  250. return self._model.postprocessor