utils.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import numpy as np
  3. import pycocotools.mask as mask_util
  4. import torch
  5. from mmengine.utils import slice_list
  6. def split_combined_polys(polys, poly_lens, polys_per_mask):
  7. """Split the combined 1-D polys into masks.
  8. A mask is represented as a list of polys, and a poly is represented as
  9. a 1-D array. In dataset, all masks are concatenated into a single 1-D
  10. tensor. Here we need to split the tensor into original representations.
  11. Args:
  12. polys (list): a list (length = image num) of 1-D tensors
  13. poly_lens (list): a list (length = image num) of poly length
  14. polys_per_mask (list): a list (length = image num) of poly number
  15. of each mask
  16. Returns:
  17. list: a list (length = image num) of list (length = mask num) of \
  18. list (length = poly num) of numpy array.
  19. """
  20. mask_polys_list = []
  21. for img_id in range(len(polys)):
  22. polys_single = polys[img_id]
  23. polys_lens_single = poly_lens[img_id].tolist()
  24. polys_per_mask_single = polys_per_mask[img_id].tolist()
  25. split_polys = slice_list(polys_single, polys_lens_single)
  26. mask_polys = slice_list(split_polys, polys_per_mask_single)
  27. mask_polys_list.append(mask_polys)
  28. return mask_polys_list
  29. # TODO: move this function to more proper place
  30. def encode_mask_results(mask_results):
  31. """Encode bitmap mask to RLE code.
  32. Args:
  33. mask_results (list): bitmap mask results.
  34. Returns:
  35. list | tuple: RLE encoded mask.
  36. """
  37. encoded_mask_results = []
  38. for mask in mask_results:
  39. encoded_mask_results.append(
  40. mask_util.encode(
  41. np.array(mask[:, :, np.newaxis], order='F',
  42. dtype='uint8'))[0]) # encoded with RLE
  43. return encoded_mask_results
  44. def mask2bbox(masks):
  45. """Obtain tight bounding boxes of binary masks.
  46. Args:
  47. masks (Tensor): Binary mask of shape (n, h, w).
  48. Returns:
  49. Tensor: Bboxe with shape (n, 4) of \
  50. positive region in binary mask.
  51. """
  52. N = masks.shape[0]
  53. bboxes = masks.new_zeros((N, 4), dtype=torch.float32)
  54. x_any = torch.any(masks, dim=1)
  55. y_any = torch.any(masks, dim=2)
  56. for i in range(N):
  57. x = torch.where(x_any[i, :])[0]
  58. y = torch.where(y_any[i, :])[0]
  59. if len(x) > 0 and len(y) > 0:
  60. bboxes[i, :] = bboxes.new_tensor(
  61. [x[0], y[0], x[-1] + 1, y[-1] + 1])
  62. return bboxes