centerpoint.py 3.4 KB

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