caddn.py 3.9 KB

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