utils.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import numpy as np
  3. from mmengine.testing import assert_allclose
  4. from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes
  5. from mmdet.structures.mask import BitmapMasks, PolygonMasks
  6. def create_random_bboxes(num_bboxes, img_w, img_h):
  7. bboxes_left_top = np.random.uniform(0, 0.5, size=(num_bboxes, 2))
  8. bboxes_right_bottom = np.random.uniform(0.5, 1, size=(num_bboxes, 2))
  9. bboxes = np.concatenate((bboxes_left_top, bboxes_right_bottom), 1)
  10. bboxes = (bboxes * np.array([img_w, img_h, img_w, img_h])).astype(
  11. np.float32)
  12. return bboxes
  13. def create_full_masks(gt_bboxes, img_w, img_h):
  14. xmin, ymin = gt_bboxes[:, 0:1], gt_bboxes[:, 1:2]
  15. xmax, ymax = gt_bboxes[:, 2:3], gt_bboxes[:, 3:4]
  16. gt_masks = np.zeros((len(gt_bboxes), img_h, img_w), dtype=np.uint8)
  17. for i in range(len(gt_bboxes)):
  18. gt_masks[i, int(ymin[i]):int(ymax[i]), int(xmin[i]):int(xmax[i])] = 1
  19. gt_masks = BitmapMasks(gt_masks, img_h, img_w)
  20. return gt_masks
  21. def construct_toy_data(poly2mask, use_box_type=False):
  22. img = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
  23. dtype=np.uint8)
  24. img = np.stack([img, img, img], axis=-1)
  25. results = dict()
  26. results['img'] = img
  27. results['img_shape'] = img.shape[:2]
  28. if use_box_type:
  29. results['gt_bboxes'] = HorizontalBoxes(
  30. np.array([[1, 0, 2, 2]], dtype=np.float32))
  31. else:
  32. results['gt_bboxes'] = np.array([[1, 0, 2, 2]], dtype=np.float32)
  33. results['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
  34. if poly2mask:
  35. gt_masks = np.array([[0, 1, 0, 0], [0, 1, 1, 0], [0, 1, 0, 0]],
  36. dtype=np.uint8)[None, :, :]
  37. results['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  38. else:
  39. raw_masks = [[np.array([1, 2, 1, 0, 2, 1], dtype=np.float32)]]
  40. results['gt_masks'] = PolygonMasks(raw_masks, 3, 4)
  41. results['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  42. results['gt_seg_map'] = np.array(
  43. [[255, 13, 255, 255], [255, 13, 13, 255], [255, 13, 255, 255]],
  44. dtype=np.uint8)
  45. return results
  46. def check_result_same(results, pipeline_results, check_keys):
  47. """Check whether the ``pipeline_results`` is the same with the predefined
  48. ``results``.
  49. Args:
  50. results (dict): Predefined results which should be the standard
  51. output of the transform pipeline.
  52. pipeline_results (dict): Results processed by the transform
  53. pipeline.
  54. check_keys (tuple): Keys that need to be checked between
  55. results and pipeline_results.
  56. """
  57. for key in check_keys:
  58. if results.get(key, None) is None:
  59. continue
  60. if isinstance(results[key], (BitmapMasks, PolygonMasks)):
  61. assert_allclose(pipeline_results[key].to_ndarray(),
  62. results[key].to_ndarray())
  63. elif isinstance(results[key], BaseBoxes):
  64. assert_allclose(pipeline_results[key].tensor, results[key].tensor)
  65. else:
  66. assert_allclose(pipeline_results[key], results[key])