You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.3 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import mmcv
  3. import numpy as np
  4. import pycocotools.mask as mask_util
  5. def split_combined_polys(polys, poly_lens, polys_per_mask):
  6. """Split the combined 1-D polys into masks.
  7. A mask is represented as a list of polys, and a poly is represented as
  8. a 1-D array. In dataset, all masks are concatenated into a single 1-D
  9. tensor. Here we need to split the tensor into original representations.
  10. Args:
  11. polys (list): a list (length = image num) of 1-D tensors
  12. poly_lens (list): a list (length = image num) of poly length
  13. polys_per_mask (list): a list (length = image num) of poly number
  14. of each mask
  15. Returns:
  16. list: a list (length = image num) of list (length = mask num) of \
  17. list (length = poly num) of numpy array.
  18. """
  19. mask_polys_list = []
  20. for img_id in range(len(polys)):
  21. polys_single = polys[img_id]
  22. polys_lens_single = poly_lens[img_id].tolist()
  23. polys_per_mask_single = polys_per_mask[img_id].tolist()
  24. split_polys = mmcv.slice_list(polys_single, polys_lens_single)
  25. mask_polys = mmcv.slice_list(split_polys, polys_per_mask_single)
  26. mask_polys_list.append(mask_polys)
  27. return mask_polys_list
  28. # TODO: move this function to more proper place
  29. def encode_mask_results(mask_results):
  30. """Encode bitmap mask to RLE code.
  31. Args:
  32. mask_results (list | tuple[list]): bitmap mask results.
  33. In mask scoring rcnn, mask_results is a tuple of (segm_results,
  34. segm_cls_score).
  35. Returns:
  36. list | tuple: RLE encoded mask.
  37. """
  38. if isinstance(mask_results, tuple): # mask scoring
  39. cls_segms, cls_mask_scores = mask_results
  40. else:
  41. cls_segms = mask_results
  42. num_classes = len(cls_segms)
  43. encoded_mask_results = [[] for _ in range(num_classes)]
  44. for i in range(len(cls_segms)):
  45. for cls_segm in cls_segms[i]:
  46. encoded_mask_results[i].append(
  47. mask_util.encode(
  48. np.array(
  49. cls_segm[:, :, np.newaxis], order='F',
  50. dtype='uint8'))[0]) # encoded with RLE
  51. if isinstance(mask_results, tuple):
  52. return encoded_mask_results, cls_mask_scores
  53. else:
  54. return encoded_mask_results

No Description

Contributors (1)