predictor.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 ..support_models import SUPPORT_MODELS
  18. class InstanceSegPredictor(DetPredictor):
  19. """ Instance Seg Predictor """
  20. support_models = SUPPORT_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).astype(
  26. dtype=np.float32, copy=False)
  27. input_dict["scale_factor"] = np.stack(
  28. [data[K.SCALE_FACTOR][::-1] for data in batch_input],
  29. axis=0).astype(
  30. dtype=np.float32, copy=False)
  31. input_dict["im_shape"] = np.stack(
  32. [data[K.IM_SIZE][::-1] for data in batch_input], axis=0).astype(
  33. dtype=np.float32, copy=False)
  34. input_ = [input_dict[i] for i in self._predictor.get_input_names()]
  35. batch_np_boxes, batch_np_boxes_num, batch_np_masks = self._predictor.predict(
  36. input_)
  37. pred = batch_input
  38. box_idx_start = 0
  39. for idx in range(len(batch_input)):
  40. np_boxes_num = batch_np_boxes_num[idx]
  41. box_idx_end = box_idx_start + np_boxes_num
  42. np_boxes = batch_np_boxes[box_idx_start:box_idx_end]
  43. np_masks = batch_np_masks[box_idx_start:box_idx_end]
  44. box_idx_start = box_idx_end
  45. batch_input[idx][K.BOXES] = np_boxes
  46. batch_input[idx][K.MASKS] = np_masks
  47. return pred
  48. @classmethod
  49. def get_output_keys(cls):
  50. """ get output keys """
  51. return [K.BOXES, K.MASKS]