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.

126 lines
4.2 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. """Square box coder.
  16. Square box coder follows the coding schema described below:
  17. l = sqrt(h * w)
  18. la = sqrt(ha * wa)
  19. ty = (y - ya) / la
  20. tx = (x - xa) / la
  21. tl = log(l / la)
  22. where x, y, w, h denote the box's center coordinates, width, and height,
  23. respectively. Similarly, xa, ya, wa, ha denote the anchor's center
  24. coordinates, width and height. tx, ty, tl denote the anchor-encoded
  25. center, and length, respectively. Because the encoded box is a square, only
  26. one length is encoded.
  27. This has shown to provide performance improvements over the Faster RCNN box
  28. coder when the objects being detected tend to be square (e.g. faces) and when
  29. the input images are not distorted via resizing.
  30. """
  31. import tensorflow as tf
  32. from object_detection.core import box_coder
  33. from object_detection.core import box_list
  34. EPSILON = 1e-8
  35. class SquareBoxCoder(box_coder.BoxCoder):
  36. """Encodes a 3-scalar representation of a square box."""
  37. def __init__(self, scale_factors=None):
  38. """Constructor for SquareBoxCoder.
  39. Args:
  40. scale_factors: List of 3 positive scalars to scale ty, tx, and tl.
  41. If set to None, does not perform scaling. For faster RCNN,
  42. the open-source implementation recommends using [10.0, 10.0, 5.0].
  43. Raises:
  44. ValueError: If scale_factors is not length 3 or contains values less than
  45. or equal to 0.
  46. """
  47. if scale_factors:
  48. if len(scale_factors) != 3:
  49. raise ValueError('The argument scale_factors must be a list of length '
  50. '3.')
  51. if any(scalar <= 0 for scalar in scale_factors):
  52. raise ValueError('The values in scale_factors must all be greater '
  53. 'than 0.')
  54. self._scale_factors = scale_factors
  55. @property
  56. def code_size(self):
  57. return 3
  58. def _encode(self, boxes, anchors):
  59. """Encodes a box collection with respect to an anchor collection.
  60. Args:
  61. boxes: BoxList holding N boxes to be encoded.
  62. anchors: BoxList of anchors.
  63. Returns:
  64. a tensor representing N anchor-encoded boxes of the format
  65. [ty, tx, tl].
  66. """
  67. # Convert anchors to the center coordinate representation.
  68. ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes()
  69. la = tf.sqrt(ha * wa)
  70. ycenter, xcenter, h, w = boxes.get_center_coordinates_and_sizes()
  71. l = tf.sqrt(h * w)
  72. # Avoid NaN in division and log below.
  73. la += EPSILON
  74. l += EPSILON
  75. tx = (xcenter - xcenter_a) / la
  76. ty = (ycenter - ycenter_a) / la
  77. tl = tf.log(l / la)
  78. # Scales location targets for joint training.
  79. if self._scale_factors:
  80. ty *= self._scale_factors[0]
  81. tx *= self._scale_factors[1]
  82. tl *= self._scale_factors[2]
  83. return tf.transpose(tf.stack([ty, tx, tl]))
  84. def _decode(self, rel_codes, anchors):
  85. """Decodes relative codes to boxes.
  86. Args:
  87. rel_codes: a tensor representing N anchor-encoded boxes.
  88. anchors: BoxList of anchors.
  89. Returns:
  90. boxes: BoxList holding N bounding boxes.
  91. """
  92. ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes()
  93. la = tf.sqrt(ha * wa)
  94. ty, tx, tl = tf.unstack(tf.transpose(rel_codes))
  95. if self._scale_factors:
  96. ty /= self._scale_factors[0]
  97. tx /= self._scale_factors[1]
  98. tl /= self._scale_factors[2]
  99. l = tf.exp(tl) * la
  100. ycenter = ty * la + ycenter_a
  101. xcenter = tx * la + xcenter_a
  102. ymin = ycenter - l / 2.
  103. xmin = xcenter - l / 2.
  104. ymax = ycenter + l / 2.
  105. xmax = xcenter + l / 2.
  106. return box_list.BoxList(tf.transpose(tf.stack([ymin, xmin, ymax, xmax])))