caddn.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 CaddnPreprocessor:
  19. def __init__(self, config_file):
  20. """Create a preprocessor for Caddn"""
  21. self._preprocessor = C.vision.perception.CaddnPreprocessor(config_file)
  22. def run(self, input_ims, cam_data, lidar_data):
  23. """Preprocess input images for Caddn
  24. :param: input_ims: (list of numpy.ndarray)The input image
  25. :return: list of FDTensor
  26. """
  27. return self._preprocessor.run(input_ims, cam_data, lidar_data)
  28. class CaddnPostprocessor:
  29. def __init__(self):
  30. """Create a postprocessor for Caddn"""
  31. self._postprocessor = C.vision.perception.CaddnPostprocessor()
  32. def run(self, runtime_results):
  33. """Postprocess the runtime results for Caddn
  34. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  35. :return: list of PerceptionResult(If the runtime_results is predict by batched samples, the length of this list equals to the batch size)
  36. """
  37. return self._postprocessor.run(runtime_results)
  38. class Caddn(UltraInferModel):
  39. def __init__(
  40. self,
  41. model_file,
  42. params_file,
  43. config_file,
  44. runtime_option=None,
  45. model_format=ModelFormat.PADDLE,
  46. ):
  47. """Load a Caddn model exported by Caddn.
  48. :param model_file: (str)Path of model file, e.g ./Caddn.pdmodel
  49. :param params_file: (str)Path of parameters file, e.g ./Caddn.pdiparams
  50. :param config_file: (str)Path of config file, e.g ./infer_cfg.yaml
  51. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  52. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  53. """
  54. super(Caddn, self).__init__(runtime_option)
  55. self._model = C.vision.perception.Caddn(
  56. model_file, params_file, config_file, self._runtime_option, model_format
  57. )
  58. assert self.initialized, "Caddn initialize failed."
  59. def predict(self, input_image, cam_data, lidar_data):
  60. """Detect an input image
  61. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  62. :param: cam_data: (list)The input camera data
  63. :param: lidar_data: (list)The input lidar data
  64. :return: PerceptionResult
  65. """
  66. return self._model.predict(input_image, cam_data, lidar_data)
  67. def batch_predict(self, images, cam_data, lidar_data):
  68. """Classify a batch of input image
  69. :param im: (list of numpy.ndarray) The input image list, each element is a 3-D array with layout HWC, BGR format
  70. :param: cam_data: (list)The input camera data
  71. :param: lidar_data: (list)The input lidar data
  72. :return list of PerceptionResult
  73. """
  74. return self._model.batch_predict(images, cam_data, lidar_data)
  75. @property
  76. def preprocessor(self):
  77. """Get CaddnPreprocessor object of the loaded model
  78. :return CaddnPreprocessor
  79. """
  80. return self._model.preprocessor
  81. @property
  82. def postprocessor(self):
  83. """Get CaddnPostprocessor object of the loaded model
  84. :return CaddnPostprocessor
  85. """
  86. return self._model.postprocessor