predictor.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 ...object_detection import DetPredictor
  16. from .keys import InstanceSegKeys as K
  17. from ..model_list import MODELS
  18. class InstanceSegPredictor(DetPredictor):
  19. """Instance Seg Predictor"""
  20. entities = MODELS
  21. def _run(self, batch_input):
  22. """run"""
  23. input_dict = {}
  24. input_dict["image"] = np.stack(
  25. [data[K.IMAGE] for data in batch_input], axis=0
  26. ).astype(dtype=np.float32, copy=False)
  27. input_dict["scale_factor"] = np.stack(
  28. [data[K.SCALE_FACTOR][::-1] for data in batch_input], axis=0
  29. ).astype(dtype=np.float32, copy=False)
  30. input_dict["im_shape"] = np.stack(
  31. [data[K.IM_SIZE][::-1] for data in batch_input], axis=0
  32. ).astype(dtype=np.float32, copy=False)
  33. input_ = [input_dict[i] for i in self._predictor.get_input_names()]
  34. pred = batch_input
  35. box_idx_start = 0
  36. if self.model_name == "SOLOv2":
  37. batch_np_boxes_num, batch_np_label, batch_np_score, batch_np_segm = (
  38. self._predictor.predict(input_)
  39. )
  40. for idx in range(len(batch_input)):
  41. np_boxes_num = batch_np_boxes_num
  42. box_idx_end = box_idx_start + np_boxes_num
  43. np_label = batch_np_label[box_idx_start:box_idx_end]
  44. np_score = batch_np_score[box_idx_start:box_idx_end]
  45. np_segm = batch_np_segm[box_idx_start:box_idx_end]
  46. box_idx_start = box_idx_end
  47. batch_input[idx][K.LABEL] = np_label
  48. batch_input[idx][K.SCORE] = np_score
  49. batch_input[idx][K.SEGM] = np_segm
  50. return pred
  51. else:
  52. batch_np_boxes, batch_np_boxes_num, batch_np_masks = (
  53. self._predictor.predict(input_)
  54. )
  55. for idx in range(len(batch_input)):
  56. np_boxes_num = batch_np_boxes_num[idx]
  57. box_idx_end = box_idx_start + np_boxes_num
  58. np_boxes = batch_np_boxes[box_idx_start:box_idx_end]
  59. np_masks = batch_np_masks[box_idx_start:box_idx_end]
  60. box_idx_start = box_idx_end
  61. batch_input[idx][K.BOXES] = np_boxes
  62. batch_input[idx][K.MASKS] = np_masks
  63. return pred
  64. @classmethod
  65. def get_output_keys(cls):
  66. """get output keys"""
  67. return [[K.LABEL, K.SCORE, K.SEGM], [K.BOXES, K.MASKS]]