predictor.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import os
  15. import numpy as np
  16. from ....utils import logging
  17. from ...base import BasePredictor
  18. from ...base.predictor.transforms import image_common
  19. from . import transforms as T
  20. from .keys import DetKeys as K
  21. from .utils import InnerConfig
  22. from ..model_list import MODELS
  23. class DetPredictor(BasePredictor):
  24. """Detection Predictor"""
  25. entities = MODELS
  26. def load_other_src(self):
  27. """load the inner config file"""
  28. infer_cfg_file_path = os.path.join(self.model_dir, "inference.yml")
  29. if not os.path.exists(infer_cfg_file_path):
  30. raise FileNotFoundError(f"Cannot find config file: {infer_cfg_file_path}")
  31. return InnerConfig(infer_cfg_file_path)
  32. @classmethod
  33. def get_input_keys(cls):
  34. """get input keys"""
  35. return [[K.IMAGE], [K.IM_PATH]]
  36. @classmethod
  37. def get_output_keys(cls):
  38. """get output keys"""
  39. return [K.BOXES]
  40. def _run(self, batch_input):
  41. """run"""
  42. input_dict = {}
  43. input_dict["image"] = np.stack(
  44. [data[K.IMAGE] for data in batch_input], axis=0
  45. ).astype(dtype=np.float32, copy=False)
  46. input_dict["scale_factor"] = np.stack(
  47. [data[K.SCALE_FACTOR][::-1] for data in batch_input], axis=0
  48. ).astype(dtype=np.float32, copy=False)
  49. input_dict["im_shape"] = np.stack(
  50. [data[K.IM_SIZE][::-1] for data in batch_input], axis=0
  51. ).astype(dtype=np.float32, copy=False)
  52. input_ = [input_dict[i] for i in self._predictor.get_input_names()]
  53. batch_np_boxes, batch_np_boxes_num = self._predictor.predict(input_)
  54. pred = batch_input
  55. box_idx_start = 0
  56. for idx in range(len(batch_input)):
  57. np_boxes_num = batch_np_boxes_num[idx]
  58. box_idx_end = box_idx_start + np_boxes_num
  59. np_boxes = batch_np_boxes[box_idx_start:box_idx_end]
  60. box_idx_start = box_idx_end
  61. batch_input[idx][K.BOXES] = np_boxes
  62. return pred
  63. def _get_pre_transforms_from_config(self):
  64. """get preprocess transforms"""
  65. logging.info(
  66. f"Transformation operators for data preprocessing will be inferred from config file."
  67. )
  68. pre_transforms = self.other_src.pre_transforms
  69. pre_transforms.insert(0, image_common.ReadImage(format="RGB"))
  70. return pre_transforms
  71. def _get_post_transforms_from_config(self):
  72. """get postprocess transforms"""
  73. post_transforms = []
  74. if not self.disable_print:
  75. post_transforms.append(T.PrintResult())
  76. if not self.disable_save:
  77. post_transforms.append(
  78. T.SaveDetResults(save_dir=self.output, labels=self.other_src.labels)
  79. )
  80. return post_transforms