predictor.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. from typing import Any, List, Optional, Sequence
  15. import numpy as np
  16. from ....modules.keypoint_detection.model_list import MODELS
  17. from ....utils import logging
  18. from ...common.batch_sampler import ImageBatchSampler
  19. from ..common import ToBatch
  20. from ..object_detection import DetPredictor
  21. from .processors import TopDownAffine, KptPostProcess
  22. from .result import KptResult
  23. class KptBatchSampler(ImageBatchSampler):
  24. def sample(self, inputs):
  25. if not isinstance(inputs, list):
  26. inputs = [inputs]
  27. batch = []
  28. for input in inputs:
  29. if isinstance(input, (np.ndarray, dict)):
  30. batch.append(input)
  31. if len(batch) == self.batch_size:
  32. yield batch
  33. batch = []
  34. elif isinstance(input, str):
  35. file_path = (
  36. self._download_from_url(input)
  37. if input.startswith("http")
  38. else input
  39. )
  40. file_list = self._get_files_list(file_path)
  41. for file_path in file_list:
  42. batch.append(file_path)
  43. if len(batch) == self.batch_size:
  44. yield batch
  45. batch = []
  46. else:
  47. logging.warning(
  48. f"Not supported input data type! Only `numpy.ndarray` and `str` are supported! So has been ignored: {input}."
  49. )
  50. if len(batch) > 0:
  51. yield batch
  52. class KptPredictor(DetPredictor):
  53. entities = MODELS
  54. flip_perm = [ # The left-right joints exchange order list
  55. [1, 2],
  56. [3, 4],
  57. [5, 6],
  58. [7, 8],
  59. [9, 10],
  60. [11, 12],
  61. [13, 14],
  62. [15, 16],
  63. ]
  64. def __init__(
  65. self,
  66. *args,
  67. flip: bool = False,
  68. use_udp: Optional[bool] = None,
  69. **kwargs,
  70. ):
  71. """Keypoint Predictor
  72. Args:
  73. flip (bool): Whether to do flipping test. Default value is ``False``.
  74. use_udp (Optional[bool]): Whether to use unbiased data processing. Default value is ``None``.
  75. """
  76. self.flip = flip
  77. self.use_udp = use_udp
  78. super().__init__(*args, **kwargs)
  79. for op in self.pre_ops:
  80. if isinstance(op, TopDownAffine):
  81. self.input_size = op.input_size
  82. break
  83. if any([name in self.model_name for name in ["PP-TinyPose"]]):
  84. self.shift_heatmap = True
  85. else:
  86. self.shift_heatmap = False
  87. def _build_batch_sampler(self):
  88. return KptBatchSampler()
  89. def _get_result_class(self):
  90. return KptResult
  91. def _format_output(self, pred: Sequence[Any]) -> List[dict]:
  92. """Transform batch outputs into a list of single image output."""
  93. return [
  94. {
  95. "heatmap": res[0],
  96. "masks": res[1],
  97. }
  98. for res in zip(*pred)
  99. ]
  100. def flip_back(self, output_flipped, matched_parts):
  101. assert (
  102. output_flipped.ndim == 4
  103. ), "output_flipped should be [batch_size, num_joints, height, width]"
  104. output_flipped = output_flipped[:, :, :, ::-1]
  105. for pair in matched_parts:
  106. tmp = output_flipped[:, pair[0], :, :].copy()
  107. output_flipped[:, pair[0], :, :] = output_flipped[:, pair[1], :, :]
  108. output_flipped[:, pair[1], :, :] = tmp
  109. return output_flipped
  110. def process(self, batch_data: List[dict]):
  111. """
  112. Process a batch of data through the preprocessing, inference, and postprocessing.
  113. Args:
  114. batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
  115. Returns:
  116. dict: A dictionary containing the input path, raw image, class IDs, scores, and label names
  117. for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
  118. """
  119. datas = batch_data
  120. # preprocess
  121. for pre_op in self.pre_ops[:-1]:
  122. datas = pre_op(datas)
  123. # use `ToBatch` format batch inputs
  124. batch_inputs = self.pre_ops[-1]([data["img"] for data in datas])
  125. # do infer
  126. batch_preds = self.infer(batch_inputs)
  127. if self.flip:
  128. # flip w
  129. batch_inputs[0] = np.flip(batch_inputs[0], axis=3)
  130. preds_flipped = self.infer(batch_inputs)
  131. output_flipped = self.flip_back(preds_flipped[0], self.flip_perm)
  132. if self.shift_heatmap:
  133. output_flipped[:, :, :, 1:] = output_flipped.copy()[:, :, :, 0:-1]
  134. batch_preds[0] = (batch_preds[0] + output_flipped) * 0.5
  135. # process a batch of predictions into a list of single image result
  136. preds_list = self._format_output(batch_preds)
  137. # postprocess
  138. keypoints = self.post_op(preds_list, datas)
  139. return {
  140. "input_path": [data.get("img_path", None) for data in datas],
  141. "input_img": [data["ori_img"] for data in datas],
  142. "kpts": keypoints,
  143. }
  144. @DetPredictor.register("TopDownEvalAffine")
  145. def build_topdown_affine(self, trainsize, use_udp=False):
  146. return TopDownAffine(
  147. input_size=trainsize,
  148. use_udp=use_udp if self.use_udp is None else self.use_udp,
  149. )
  150. def build_to_batch(self):
  151. return ToBatch()
  152. def build_postprocess(self):
  153. return KptPostProcess(use_dark=True)