smoke.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 SmokePreprocessor:
  18. def __init__(self, config_file):
  19. """Create a preprocessor for Smoke"""
  20. self._preprocessor = C.vision.perception.SmokePreprocessor(config_file)
  21. def run(self, input_ims):
  22. """Preprocess input images for Smoke
  23. :param: input_ims: (list of numpy.ndarray)The input image
  24. :return: list of FDTensor
  25. """
  26. return self._preprocessor.run(input_ims)
  27. class SmokePostprocessor:
  28. def __init__(self):
  29. """Create a postprocessor for Smoke"""
  30. self._postprocessor = C.vision.perception.SmokePostprocessor()
  31. def run(self, runtime_results):
  32. """Postprocess the runtime results for Smoke
  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 Smoke(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 SMoke model exported by Smoke.
  47. :param model_file: (str)Path of model file, e.g ./smoke.pdmodel
  48. :param params_file: (str)Path of parameters file, e.g ./smoke.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(Smoke, self).__init__(runtime_option)
  54. self._model = C.vision.perception.Smoke(
  55. model_file, params_file, config_file, self._runtime_option, model_format
  56. )
  57. assert self.initialized, "Smoke initialize failed."
  58. def predict(self, input_image):
  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 conf_threshold: confidence threshold for postprocessing, default is 0.25
  62. :param nms_iou_threshold: iou threshold for NMS, default is 0.5
  63. :return: PerceptionResult
  64. """
  65. return self._model.predict(input_image)
  66. def batch_predict(self, images):
  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. :return list of PerceptionResult
  70. """
  71. return self._model.batch_predict(images)
  72. @property
  73. def preprocessor(self):
  74. """Get SmokePreprocessor object of the loaded model
  75. :return SmokePreprocessor
  76. """
  77. return self._model.preprocessor
  78. @property
  79. def postprocessor(self):
  80. """Get SmokePostprocessor object of the loaded model
  81. :return SmokePostprocessor
  82. """
  83. return self._model.postprocessor