object_detection.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 numpy as np
  15. from ...utils.func_register import FuncRegister
  16. from ...modules.object_detection.model_list import MODELS
  17. from ..components import *
  18. from ..results import DetResult
  19. from ..utils.process_hook import batchable_method
  20. from .base import BasicPredictor
  21. class DetPredictor(BasicPredictor):
  22. entities = MODELS
  23. _FUNC_MAP = {}
  24. register = FuncRegister(_FUNC_MAP)
  25. def _build_components(self):
  26. ops = {}
  27. ops["ReadImage"] = ReadImage(
  28. batch_size=self.kwargs.get("batch_size", 1), format="RGB"
  29. )
  30. for cfg in self.config["Preprocess"]:
  31. tf_key = cfg["type"]
  32. func = self._FUNC_MAP.get(tf_key)
  33. cfg.pop("type")
  34. args = cfg
  35. op = func(self, **args) if args else func(self)
  36. ops[tf_key] = op
  37. predictor = ImageDetPredictor(
  38. model_dir=self.model_dir,
  39. model_prefix=self.MODEL_FILE_PREFIX,
  40. option=self.pp_option,
  41. )
  42. if self.model_name in [
  43. "RT-DETR-R18",
  44. "RT-DETR-R50",
  45. "RT-DETR-L",
  46. "RT-DETR-H",
  47. "RT-DETR-X",
  48. ]:
  49. predictor.set_inputs(
  50. {
  51. "img": "img",
  52. "scale_factors": "scale_factors",
  53. "img_size": "img_size",
  54. }
  55. )
  56. ops["predictor"] = predictor
  57. ops["postprocess"] = DetPostProcess(
  58. threshold=self.config["draw_threshold"], labels=self.config["label_list"]
  59. )
  60. return ops
  61. @register("Resize")
  62. def build_resize(self, target_size, keep_ratio=False, interp=2):
  63. assert target_size
  64. if isinstance(interp, int):
  65. interp = {
  66. 0: "NEAREST",
  67. 1: "LINEAR",
  68. 2: "CUBIC",
  69. 3: "AREA",
  70. 4: "LANCZOS4",
  71. }[interp]
  72. op = Resize(target_size=target_size, keep_ratio=keep_ratio, interp=interp)
  73. return op
  74. @register("NormalizeImage")
  75. def build_normalize(
  76. self,
  77. norm_type=None,
  78. mean=[0.485, 0.456, 0.406],
  79. std=[0.229, 0.224, 0.225],
  80. is_scale=None,
  81. ):
  82. if is_scale:
  83. scale = 1.0 / 255.0
  84. else:
  85. scale = 1
  86. if not norm_type or norm_type == "none":
  87. norm_type = "mean_std"
  88. if norm_type != "mean_std":
  89. mean = 0
  90. std = 1
  91. return Normalize(mean=mean, std=std)
  92. @register("Permute")
  93. def build_to_chw(self):
  94. return ToCHWImage()
  95. def _pack_res(self, single):
  96. keys = ["img_path", "boxes", "labels"]
  97. return DetResult({key: single[key] for key in keys})