centerpoint.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 CenterpointPreprocessor:
  18. def __init__(self, config_file):
  19. """Create a preprocessor for Centerpoint"""
  20. self._preprocessor = C.vision.perception.CenterpointPreprocessor(config_file)
  21. def run(self, point_dirs, num_point_dim, with_timelag):
  22. """Preprocess input images for Centerpoint
  23. :param: input_ims: (list of numpy.ndarray)The input image
  24. :return: list of FDTensor
  25. """
  26. return self._preprocessor.run(point_dirs, num_point_dim, with_timelag)
  27. class Centerpoint(UltraInferModel):
  28. def __init__(
  29. self,
  30. model_file,
  31. params_file,
  32. config_file,
  33. runtime_option=None,
  34. model_format=ModelFormat.PADDLE,
  35. ):
  36. """Load a Centerpoint model exported by Centerpoint.
  37. :param model_file: (str)Path of model file, e.g ./Centerpoint.pdmodel
  38. :param params_file: (str)Path of parameters file, e.g ./Centerpoint.pdiparams
  39. :param config_file: (str)Path of config file, e.g ./infer_cfg.yaml
  40. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  41. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  42. """
  43. super(Centerpoint, self).__init__(runtime_option)
  44. self._model = C.vision.perception.Centerpoint(
  45. model_file, params_file, config_file, self._runtime_option, model_format
  46. )
  47. assert self.initialized, "Centerpoint initialize failed."
  48. def predict(self, point_dir):
  49. """Detect an input image
  50. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  51. :param conf_threshold: confidence threshold for postprocessing, default is 0.25
  52. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  53. :return: PerceptionResult
  54. """
  55. return self._model.predict(point_dir)
  56. def batch_predict(self, points_dir):
  57. """Classify a batch of input image
  58. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  59. :return list of PerceptionResult
  60. """
  61. return self._model.batch_predict(points_dir)
  62. @property
  63. def preprocessor(self):
  64. """Get CenterpointPreprocessor object of the loaded model
  65. :return CenterpointPreprocessor
  66. """
  67. return self._model.preprocessor
  68. @property
  69. def postprocessor(self):
  70. """Get CenterpointPostprocessor object of the loaded model
  71. :return CenterpointPostprocessor
  72. """
  73. return self._model.postprocessor