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

119 lines
4.1 KiB

6 years ago
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  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. # ==============================================================================
  15. """Operations for [N, height, width] numpy arrays representing masks.
  16. Example mask operations that are supported:
  17. * Areas: compute mask areas
  18. * IOU: pairwise intersection-over-union scores
  19. """
  20. import numpy as np
  21. EPSILON = 1e-7
  22. def area(masks):
  23. """Computes area of masks.
  24. Args:
  25. masks: Numpy array with shape [N, height, width] holding N masks. Masks
  26. values are of type np.uint8 and values are in {0,1}.
  27. Returns:
  28. a numpy array with shape [N*1] representing mask areas.
  29. Raises:
  30. ValueError: If masks.dtype is not np.uint8
  31. """
  32. if masks.dtype != np.uint8:
  33. raise ValueError('Masks type should be np.uint8')
  34. return np.sum(masks, axis=(1, 2), dtype=np.float32)
  35. def intersection(masks1, masks2):
  36. """Compute pairwise intersection areas between masks.
  37. Args:
  38. masks1: a numpy array with shape [N, height, width] holding N masks. Masks
  39. values are of type np.uint8 and values are in {0,1}.
  40. masks2: a numpy array with shape [M, height, width] holding M masks. Masks
  41. values are of type np.uint8 and values are in {0,1}.
  42. Returns:
  43. a numpy array with shape [N*M] representing pairwise intersection area.
  44. Raises:
  45. ValueError: If masks1 and masks2 are not of type np.uint8.
  46. """
  47. if masks1.dtype != np.uint8 or masks2.dtype != np.uint8:
  48. raise ValueError('masks1 and masks2 should be of type np.uint8')
  49. n = masks1.shape[0]
  50. m = masks2.shape[0]
  51. answer = np.zeros([n, m], dtype=np.float32)
  52. for i in np.arange(n):
  53. for j in np.arange(m):
  54. answer[i, j] = np.sum(np.minimum(masks1[i], masks2[j]), dtype=np.float32)
  55. return answer
  56. def iou(masks1, masks2):
  57. """Computes pairwise intersection-over-union between mask collections.
  58. Args:
  59. masks1: a numpy array with shape [N, height, width] holding N masks. Masks
  60. values are of type np.uint8 and values are in {0,1}.
  61. masks2: a numpy array with shape [M, height, width] holding N masks. Masks
  62. values are of type np.uint8 and values are in {0,1}.
  63. Returns:
  64. a numpy array with shape [N, M] representing pairwise iou scores.
  65. Raises:
  66. ValueError: If masks1 and masks2 are not of type np.uint8.
  67. """
  68. if masks1.dtype != np.uint8 or masks2.dtype != np.uint8:
  69. raise ValueError('masks1 and masks2 should be of type np.uint8')
  70. intersect = intersection(masks1, masks2)
  71. area1 = area(masks1)
  72. area2 = area(masks2)
  73. union = np.expand_dims(area1, axis=1) + np.expand_dims(
  74. area2, axis=0) - intersect
  75. return intersect / np.maximum(union, EPSILON)
  76. def ioa(masks1, masks2):
  77. """Computes pairwise intersection-over-area between box collections.
  78. Intersection-over-area (ioa) between two masks, mask1 and mask2 is defined as
  79. their intersection area over mask2's area. Note that ioa is not symmetric,
  80. that is, IOA(mask1, mask2) != IOA(mask2, mask1).
  81. Args:
  82. masks1: a numpy array with shape [N, height, width] holding N masks. Masks
  83. values are of type np.uint8 and values are in {0,1}.
  84. masks2: a numpy array with shape [M, height, width] holding N masks. Masks
  85. values are of type np.uint8 and values are in {0,1}.
  86. Returns:
  87. a numpy array with shape [N, M] representing pairwise ioa scores.
  88. Raises:
  89. ValueError: If masks1 and masks2 are not of type np.uint8.
  90. """
  91. if masks1.dtype != np.uint8 or masks2.dtype != np.uint8:
  92. raise ValueError('masks1 and masks2 should be of type np.uint8')
  93. intersect = intersection(masks1, masks2)
  94. areas = np.expand_dims(area(masks2), axis=0)
  95. return intersect / (areas + EPSILON)