predictor.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 typing import Any, Union, Dict, List, Tuple, Iterator
  15. import shutil
  16. import tempfile
  17. from importlib import import_module
  18. import lazy_paddle
  19. from ....utils import logging
  20. from ....utils.func_register import FuncRegister
  21. module_3d_bev_detection = import_module(".3d_bev_detection", "paddlex.modules")
  22. module_3d_model_list = getattr(module_3d_bev_detection, "model_list")
  23. MODELS = getattr(module_3d_model_list, "MODELS")
  24. from ...common.batch_sampler import Det3DBatchSampler
  25. from ...common.reader import ReadNuscenesData
  26. from ..common import StaticInfer
  27. from ..base import BasePredictor
  28. from ..base.predictor.base_predictor import PredictionWrap
  29. from .processors import (
  30. LoadPointsFromFile,
  31. LoadPointsFromMultiSweeps,
  32. LoadMultiViewImageFromFiles,
  33. ResizeImage,
  34. NormalizeImage,
  35. PadImage,
  36. SampleFilterByKey,
  37. GetInferInput,
  38. )
  39. from .result import BEV3DDetResult
  40. class BEVDet3DPredictor(BasePredictor):
  41. """BEVDet3DPredictor that inherits from BasePredictor."""
  42. entities = MODELS
  43. _FUNC_MAP = {}
  44. register = FuncRegister(_FUNC_MAP)
  45. def __init__(self, *args: List, **kwargs: Dict) -> None:
  46. """Initializes BEVDet3DPredictor.
  47. Args:
  48. *args: Arbitrary positional arguments passed to the superclass.
  49. **kwargs: Arbitrary keyword arguments passed to the superclass.
  50. """
  51. self.temp_dir = tempfile.mkdtemp()
  52. logging.info(
  53. f"infer data will be stored in temporary directory {self.temp_dir}"
  54. )
  55. super().__init__(*args, **kwargs)
  56. self.pre_tfs, self.infer = self._build()
  57. def _build_batch_sampler(self) -> Det3DBatchSampler:
  58. """Builds and returns an Det3DBatchSampler instance.
  59. Returns:
  60. Det3DBatchSampler: An instance of Det3DBatchSampler.
  61. """
  62. return Det3DBatchSampler(temp_dir=self.temp_dir)
  63. def _get_result_class(self) -> type:
  64. """Returns the result class, BEV3DDetResult.
  65. Returns:
  66. type: The BEV3DDetResult class.
  67. """
  68. return BEV3DDetResult
  69. def _build(self) -> Tuple:
  70. """Build the preprocessors and inference engine based on the configuration.
  71. Returns:
  72. tuple: A tuple containing the preprocessors and inference engine.
  73. """
  74. if (
  75. lazy_paddle.is_compiled_with_cuda()
  76. and not lazy_paddle.is_compiled_with_rocm()
  77. ):
  78. from ....ops.voxelize import hard_voxelize
  79. from ....ops.iou3d_nms import nms_gpu
  80. else:
  81. logging.error("3D BEVFusion custom ops only support GPU platform!")
  82. pre_tfs = {"Read": ReadNuscenesData()}
  83. for cfg in self.config["PreProcess"]["transform_ops"]:
  84. tf_key = list(cfg.keys())[0]
  85. func = self._FUNC_MAP[tf_key]
  86. args = cfg.get(tf_key, {})
  87. name, op = func(self, **args) if args else func(self)
  88. if op:
  89. pre_tfs[name] = op
  90. pre_tfs["GetInferInput"] = GetInferInput()
  91. infer = self.create_static_infer()
  92. return pre_tfs, infer
  93. def _format_output(
  94. self, infer_input: List[Any], outs: List[Any], img_metas: Dict[str, Any]
  95. ) -> Dict[str, Any]:
  96. """format inference input and output into predict result
  97. Args:
  98. infer_input(List): Model infer inputs with list containing images, points and lidar2img matrix.
  99. outs(List): Model infer output containing bboxes, scores, labels result.
  100. img_metas(Dict): Image metas info of input sample.
  101. Returns:
  102. Dict: A Dict containing formatted inference output results.
  103. """
  104. input_lidar_path = img_metas["input_lidar_path"]
  105. input_img_paths = img_metas["input_img_paths"]
  106. sample_id = img_metas["sample_id"]
  107. results = {}
  108. out_bboxes_3d = []
  109. out_scores_3d = []
  110. out_labels_3d = []
  111. input_imgs = []
  112. input_points = []
  113. input_lidar2imgs = []
  114. input_ids = []
  115. input_lidar_path_list = []
  116. input_img_paths_list = []
  117. out_bboxes_3d.append(outs[0])
  118. out_labels_3d.append(outs[1])
  119. out_scores_3d.append(outs[2])
  120. input_imgs.append(infer_input[1])
  121. input_points.append(infer_input[0])
  122. input_lidar2imgs.append(infer_input[2])
  123. input_ids.append(sample_id)
  124. input_lidar_path_list.append(input_lidar_path)
  125. input_img_paths_list.append(input_img_paths)
  126. results["input_path"] = input_lidar_path_list
  127. results["input_img_paths"] = input_img_paths_list
  128. results["sample_id"] = input_ids
  129. results["boxes_3d"] = out_bboxes_3d
  130. results["labels_3d"] = out_labels_3d
  131. results["scores_3d"] = out_scores_3d
  132. return results
  133. def process(self, batch_data: List[str]) -> Dict[str, Any]:
  134. """
  135. Process a batch of data through the preprocessing and inference.
  136. Args:
  137. batch_data (List[str]): A batch of input data (e.g., sample anno file paths).
  138. Returns:
  139. dict: A dictionary containing the input path, input img, input points, input lidar2img, output bboxes, output labels, output scores and label names. Keys include 'input_path', 'input_img', 'input_points', 'input_lidar2img', 'boxes_3d', 'labels_3d' and 'scores_3d'.
  140. """
  141. sample = self.pre_tfs["Read"](batch_data=batch_data)
  142. sample = self.pre_tfs["LoadPointsFromFile"](results=sample[0])
  143. sample = self.pre_tfs["LoadPointsFromMultiSweeps"](results=sample)
  144. sample = self.pre_tfs["LoadMultiViewImageFromFiles"](sample=sample)
  145. sample = self.pre_tfs["ResizeImage"](results=sample)
  146. sample = self.pre_tfs["NormalizeImage"](results=sample)
  147. sample = self.pre_tfs["PadImage"](results=sample)
  148. sample = self.pre_tfs["SampleFilterByKey"](sample=sample)
  149. infer_input, img_metas = self.pre_tfs["GetInferInput"](sample=sample)
  150. infer_output = self.infer(x=infer_input)
  151. results = self._format_output(infer_input, infer_output, img_metas)
  152. return results
  153. @register("LoadPointsFromFile")
  154. def build_load_img_from_file(
  155. self, load_dim=6, use_dim=[0, 1, 2], shift_height=False, use_color=False
  156. ):
  157. return "LoadPointsFromFile", LoadPointsFromFile(
  158. load_dim=load_dim,
  159. use_dim=use_dim,
  160. shift_height=shift_height,
  161. use_color=use_color,
  162. )
  163. @register("LoadPointsFromMultiSweeps")
  164. def build_load_points_from_multi_sweeps(
  165. self,
  166. sweeps_num=10,
  167. load_dim=5,
  168. use_dim=[0, 1, 2, 4],
  169. pad_empty_sweeps=False,
  170. remove_close=False,
  171. test_mode=False,
  172. point_cloud_angle_range=None,
  173. ):
  174. return "LoadPointsFromMultiSweeps", LoadPointsFromMultiSweeps(
  175. sweeps_num=sweeps_num,
  176. load_dim=load_dim,
  177. use_dim=use_dim,
  178. pad_empty_sweeps=pad_empty_sweeps,
  179. remove_close=remove_close,
  180. test_mode=test_mode,
  181. point_cloud_angle_range=point_cloud_angle_range,
  182. )
  183. @register("LoadMultiViewImageFromFiles")
  184. def build_load_multi_view_image_from_files(
  185. self,
  186. to_float32=False,
  187. project_pts_to_img_depth=False,
  188. cam_depth_range=[4.0, 45.0, 1.0],
  189. constant_std=0.5,
  190. imread_flag=-1,
  191. ):
  192. return "LoadMultiViewImageFromFiles", LoadMultiViewImageFromFiles(
  193. to_float32=to_float32,
  194. project_pts_to_img_depth=project_pts_to_img_depth,
  195. cam_depth_range=cam_depth_range,
  196. constant_std=constant_std,
  197. imread_flag=imread_flag,
  198. )
  199. @register("ResizeImage")
  200. def build_resize_image(
  201. self,
  202. img_scale=None,
  203. multiscale_mode="range",
  204. ratio_range=None,
  205. keep_ratio=True,
  206. bbox_clip_border=True,
  207. backend="cv2",
  208. override=False,
  209. ):
  210. return "ResizeImage", ResizeImage(
  211. img_scale=img_scale,
  212. multiscale_mode=multiscale_mode,
  213. ratio_range=ratio_range,
  214. keep_ratio=keep_ratio,
  215. bbox_clip_border=bbox_clip_border,
  216. backend=backend,
  217. override=override,
  218. )
  219. @register("NormalizeImage")
  220. def build_normalize_image(self, mean, std, to_rgb=True):
  221. return "NormalizeImage", NormalizeImage(mean=mean, std=std, to_rgb=to_rgb)
  222. @register("PadImage")
  223. def build_pad_image(self, size=None, size_divisor=None, pad_val=0):
  224. return "PadImage", PadImage(
  225. size=size, size_divisor=size_divisor, pad_val=pad_val
  226. )
  227. @register("SampleFilterByKey")
  228. def build_sample_filter_by_key(
  229. self,
  230. keys,
  231. meta_keys=(
  232. "filename",
  233. "ori_shape",
  234. "img_shape",
  235. "lidar2img",
  236. "depth2img",
  237. "cam2img",
  238. "pad_shape",
  239. "scale_factor",
  240. "flip",
  241. "pcd_horizontal_flip",
  242. "pcd_vertical_flip",
  243. "box_type_3d",
  244. "img_norm_cfg",
  245. "pcd_trans",
  246. "sample_idx",
  247. "pcd_scale_factor",
  248. "pcd_rotation",
  249. "pts_filename",
  250. "transformation_3d_flow",
  251. ),
  252. ):
  253. return "SampleFilterByKey", SampleFilterByKey(keys=keys, meta_keys=meta_keys)
  254. @register("GetInferInput")
  255. def build_get_infer_input(self):
  256. return "GetInferInput", GetInferInput()
  257. def apply(self, input: Any, **kwargs) -> Iterator[Any]:
  258. """
  259. Do predicting with the input data and yields predictions.
  260. Args:
  261. input (Any): The input data to be predicted.
  262. Yields:
  263. Iterator[Any]: An iterator yielding prediction results.
  264. """
  265. try:
  266. for batch_data in self.batch_sampler(input):
  267. prediction = self.process(batch_data, **kwargs)
  268. prediction = PredictionWrap(prediction, len(batch_data))
  269. for idx in range(len(batch_data)):
  270. yield self.result_class(prediction.get_by_idx(idx))
  271. except Exception as e:
  272. raise e
  273. finally:
  274. shutil.rmtree(self.temp_dir)