pipeline.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 typing import Any, Dict, Optional, Union
  15. from ....utils.deps import pipeline_requires_extra
  16. from ...common.batch_sampler import ImageBatchSampler
  17. from ...common.reader import ReadImage
  18. from ...utils.benchmark import benchmark
  19. from ...utils.hpi import HPIConfig
  20. from ...utils.pp_option import PaddlePredictorOption
  21. from ..base import BasePipeline
  22. from ..components import CropByBoxes, FaissBuilder, FaissIndexer
  23. from .result import ShiTuResult
  24. @benchmark.time_methods
  25. @pipeline_requires_extra("cv")
  26. class ShiTuV2Pipeline(BasePipeline):
  27. """ShiTuV2 Pipeline"""
  28. entities = "PP-ShiTuV2"
  29. def __init__(
  30. self,
  31. config: Dict,
  32. device: str = None,
  33. pp_option: PaddlePredictorOption = None,
  34. use_hpip: bool = False,
  35. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  36. ):
  37. super().__init__(
  38. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_config=hpi_config
  39. )
  40. self._topk, self._rec_threshold, self._hamming_radius, self._det_threshold = (
  41. config.get("rec_topk", 5),
  42. config.get("rec_threshold", 0.5),
  43. config.get("hamming_radius", None),
  44. config.get("det_threshold", 0.5),
  45. )
  46. index = config.get("index", None)
  47. self.img_reader = ReadImage(format="BGR")
  48. self.det_model = self.create_model(config["SubModules"]["Detection"])
  49. self.rec_model = self.create_model(config["SubModules"]["Recognition"])
  50. self.crop_by_boxes = CropByBoxes()
  51. self.indexer = FaissIndexer(index=index) if index else None
  52. self.batch_sampler = ImageBatchSampler(
  53. batch_size=self.det_model.batch_sampler.batch_size
  54. )
  55. def predict(self, input, index=None, **kwargs):
  56. indexer = FaissIndexer(index) if index is not None else self.indexer
  57. assert indexer
  58. kwargs = {k: v for k, v in kwargs.items() if v is not None}
  59. topk = kwargs.get("rec_topk", self._topk)
  60. rec_threshold = kwargs.get("rec_threshold", self._rec_threshold)
  61. hamming_radius = kwargs.get("hamming_radius", self._hamming_radius)
  62. det_threshold = kwargs.get("det_threshold", self._det_threshold)
  63. for img_id, batch_data in enumerate(self.batch_sampler(input)):
  64. raw_imgs = self.img_reader(batch_data.instances)
  65. all_det_res = list(self.det_model(raw_imgs, threshold=det_threshold))
  66. for input_data, raw_img, det_res in zip(
  67. batch_data.instances, raw_imgs, all_det_res
  68. ):
  69. rec_res = self.get_rec_result(
  70. raw_img, det_res, indexer, rec_threshold, hamming_radius, topk
  71. )
  72. yield self.get_final_result(input_data, raw_img, det_res, rec_res)
  73. def get_rec_result(
  74. self, raw_img, det_res, indexer, rec_threshold, hamming_radius, topk
  75. ):
  76. if len(det_res["boxes"]) == 0:
  77. w, h = raw_img.shape[:2]
  78. det_res["boxes"].append(
  79. {
  80. "cls_id": 0,
  81. "label": "full_img",
  82. "score": 0,
  83. "coordinate": [0, 0, h, w],
  84. }
  85. )
  86. subs_of_img = list(self.crop_by_boxes(raw_img, det_res["boxes"]))
  87. img_list = [img["img"] for img in subs_of_img]
  88. all_rec_res = list(self.rec_model(img_list))
  89. all_rec_res = indexer(
  90. [rec_res["feature"] for rec_res in all_rec_res],
  91. score_thres=rec_threshold,
  92. hamming_radius=hamming_radius,
  93. topk=topk,
  94. )
  95. output = {"label": [], "score": []}
  96. for res in all_rec_res:
  97. output["label"].append(res["label"])
  98. output["score"].append(res["score"])
  99. return output
  100. def get_final_result(self, input_data, raw_img, det_res, rec_res):
  101. single_img_res = {"input_path": input_data, "input_img": raw_img, "boxes": []}
  102. for i, obj in enumerate(det_res["boxes"]):
  103. rec_scores = rec_res["score"][i]
  104. rec_scores = rec_scores if rec_scores is not None else [None]
  105. labels = rec_res["label"][i]
  106. labels = labels if labels is not None else [None]
  107. single_img_res["boxes"].append(
  108. {
  109. "labels": labels,
  110. "rec_scores": rec_scores,
  111. "det_score": obj["score"],
  112. "coordinate": obj["coordinate"],
  113. }
  114. )
  115. return ShiTuResult(single_img_res)
  116. def build_index(
  117. self,
  118. gallery_imgs,
  119. gallery_label,
  120. metric_type="IP",
  121. index_type="HNSW32",
  122. **kwargs
  123. ):
  124. return FaissBuilder.build(
  125. gallery_imgs,
  126. gallery_label,
  127. self.rec_model.predict,
  128. metric_type=metric_type,
  129. index_type=index_type,
  130. )
  131. def remove_index(self, remove_ids, index):
  132. return FaissBuilder.remove(remove_ids, index)
  133. def append_index(
  134. self,
  135. gallery_imgs,
  136. gallery_label,
  137. index,
  138. ):
  139. return FaissBuilder.append(
  140. gallery_imgs,
  141. gallery_label,
  142. self.rec_model.predict,
  143. index,
  144. )