rkyolov5.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 RKYOLOPreprocessor:
  19. def __init__(self):
  20. """Create a preprocessor for RKYOLOV5"""
  21. self._preprocessor = C.vision.detection.RKYOLOPreprocessor()
  22. def run(self, input_ims):
  23. """Preprocess input images for RKYOLOV5
  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 RKYOLOPostprocessor:
  71. def __init__(self):
  72. """Create a postprocessor for RKYOLOV5"""
  73. self._postprocessor = C.vision.detection.RKYOLOPostprocessor()
  74. def run(self, runtime_results):
  75. """Postprocess the runtime results for RKYOLOV5
  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)
  81. def set_anchor(self, anchor):
  82. self._postprocessor.set_anchor(anchor)
  83. @property
  84. def conf_threshold(self):
  85. """
  86. confidence threshold for postprocessing, default is 0.25
  87. """
  88. return self._postprocessor.conf_threshold
  89. @property
  90. def nms_threshold(self):
  91. """
  92. nms threshold for postprocessing, default is 0.5
  93. """
  94. return self._postprocessor.nms_threshold
  95. @property
  96. def class_num(self):
  97. """
  98. class_num for postprocessing, default is 80
  99. """
  100. return self._postprocessor.class_num
  101. @conf_threshold.setter
  102. def conf_threshold(self, conf_threshold):
  103. assert isinstance(
  104. conf_threshold, float
  105. ), "The value to set `conf_threshold` must be type of float."
  106. self._postprocessor.conf_threshold = conf_threshold
  107. @nms_threshold.setter
  108. def nms_threshold(self, nms_threshold):
  109. assert isinstance(
  110. nms_threshold, float
  111. ), "The value to set `nms_threshold` must be type of float."
  112. self._postprocessor.nms_threshold = nms_threshold
  113. @class_num.setter
  114. def class_num(self, class_num):
  115. """
  116. class_num for postprocessing, default is 80
  117. """
  118. assert isinstance(
  119. class_num, int
  120. ), "The value to set `nms_threshold` must be type of float."
  121. self._postprocessor.class_num = class_num
  122. class RKYOLOV5(UltraInferModel):
  123. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  124. """Load a RKYOLOV5 model exported by RKYOLOV5.
  125. :param model_file: (str)Path of model file, e.g ./yolov5.rknn
  126. :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
  127. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  128. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  129. """
  130. # 调用基函数进行backend_option的初始化
  131. # 初始化后的option保存在self._runtime_option
  132. super(RKYOLOV5, self).__init__(runtime_option)
  133. self._model = C.vision.detection.RKYOLOV5(
  134. model_file, self._runtime_option, model_format
  135. )
  136. # 通过self.initialized判断整个模型的初始化是否成功
  137. assert self.initialized, "RKYOLOV5 initialize failed."
  138. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  139. """Detect an input image
  140. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  141. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  142. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  143. :return: DetectionResult
  144. """
  145. self.postprocessor.conf_threshold = conf_threshold
  146. self.postprocessor.nms_threshold = nms_iou_threshold
  147. return self._model.predict(input_image)
  148. def batch_predict(self, images):
  149. """Classify a batch of input image
  150. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  151. :return list of DetectionResult
  152. """
  153. return self._model.batch_predict(images)
  154. @property
  155. def preprocessor(self):
  156. """Get RKYOLOV5Preprocessor object of the loaded model
  157. :return RKYOLOV5Preprocessor
  158. """
  159. return self._model.preprocessor
  160. @property
  161. def postprocessor(self):
  162. """Get RKYOLOV5Postprocessor object of the loaded model
  163. :return RKYOLOV5Postprocessor
  164. """
  165. return self._model.postprocessor
  166. class RKYOLOX(UltraInferModel):
  167. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  168. """Load a RKYOLOX model exported by RKYOLOX.
  169. :param model_file: (str)Path of model file, e.g ./yolox.rknn
  170. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  171. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  172. """
  173. # 调用基函数进行backend_option的初始化
  174. # 初始化后的option保存在self._runtime_option
  175. super(RKYOLOX, self).__init__(runtime_option)
  176. self._model = C.vision.detection.RKYOLOX(
  177. model_file, self._runtime_option, model_format
  178. )
  179. # 通过self.initialized判断整个模型的初始化是否成功
  180. assert self.initialized, "RKYOLOV5 initialize failed."
  181. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  182. """Detect an input image
  183. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  184. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  185. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  186. :return: DetectionResult
  187. """
  188. self.postprocessor.conf_threshold = conf_threshold
  189. self.postprocessor.nms_threshold = nms_iou_threshold
  190. return self._model.predict(input_image)
  191. def batch_predict(self, images):
  192. """Classify a batch of input image
  193. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  194. :return list of DetectionResult
  195. """
  196. return self._model.batch_predict(images)
  197. @property
  198. def preprocessor(self):
  199. """Get RKYOLOV5Preprocessor object of the loaded model
  200. :return RKYOLOV5Preprocessor
  201. """
  202. return self._model.preprocessor
  203. @property
  204. def postprocessor(self):
  205. """Get RKYOLOV5Postprocessor object of the loaded model
  206. :return RKYOLOV5Postprocessor
  207. """
  208. return self._model.postprocessor
  209. class RKYOLOV7(UltraInferModel):
  210. def __init__(self, model_file, runtime_option=None, model_format=ModelFormat.RKNN):
  211. """Load a RKYOLOX model exported by RKYOLOV7.
  212. :param model_file: (str)Path of model file, e.g ./yolov7.rknn
  213. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  214. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  215. """
  216. # 调用基函数进行backend_option的初始化
  217. # 初始化后的option保存在self._runtime_option
  218. super(RKYOLOV7, self).__init__(runtime_option)
  219. self._model = C.vision.detection.RKYOLOV7(
  220. model_file, self._runtime_option, model_format
  221. )
  222. # 通过self.initialized判断整个模型的初始化是否成功
  223. assert self.initialized, "RKYOLOV5 initialize failed."
  224. def predict(self, input_image, conf_threshold=0.25, nms_iou_threshold=0.5):
  225. """Detect an input image
  226. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  227. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  228. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  229. :return: DetectionResult
  230. """
  231. self.postprocessor.conf_threshold = conf_threshold
  232. self.postprocessor.nms_threshold = nms_iou_threshold
  233. return self._model.predict(input_image)
  234. def batch_predict(self, images):
  235. """Classify a batch of input image
  236. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  237. :return list of DetectionResult
  238. """
  239. return self._model.batch_predict(images)
  240. @property
  241. def preprocessor(self):
  242. """Get RKYOLOV5Preprocessor object of the loaded model
  243. :return RKYOLOV5Preprocessor
  244. """
  245. return self._model.preprocessor
  246. @property
  247. def postprocessor(self):
  248. """Get RKYOLOV5Postprocessor object of the loaded model
  249. :return RKYOLOV5Postprocessor
  250. """
  251. return self._model.postprocessor