Args: boxes: (..., 4), float32 anchors: (..., 4), float32 Returns: box_encoded: (..., 4), float32 with the same shape.
(boxes, anchors)
| 54 | |
| 55 | @under_name_scope() |
| 56 | def encode_bbox_target(boxes, anchors): |
| 57 | """ |
| 58 | Args: |
| 59 | boxes: (..., 4), float32 |
| 60 | anchors: (..., 4), float32 |
| 61 | |
| 62 | Returns: |
| 63 | box_encoded: (..., 4), float32 with the same shape. |
| 64 | """ |
| 65 | anchors_x1y1x2y2 = tf.reshape(anchors, (-1, 2, 2)) |
| 66 | anchors_x1y1, anchors_x2y2 = tf.split(anchors_x1y1x2y2, 2, axis=1) |
| 67 | waha = anchors_x2y2 - anchors_x1y1 |
| 68 | xaya = (anchors_x2y2 + anchors_x1y1) * 0.5 |
| 69 | |
| 70 | boxes_x1y1x2y2 = tf.reshape(boxes, (-1, 2, 2)) |
| 71 | boxes_x1y1, boxes_x2y2 = tf.split(boxes_x1y1x2y2, 2, axis=1) |
| 72 | wbhb = boxes_x2y2 - boxes_x1y1 |
| 73 | xbyb = (boxes_x2y2 + boxes_x1y1) * 0.5 |
| 74 | |
| 75 | # Note that here not all boxes are valid. Some may be zero |
| 76 | txty = (xbyb - xaya) / waha |
| 77 | twth = tf.math.log(wbhb / waha) # may contain -inf for invalid boxes |
| 78 | encoded = tf.concat([txty, twth], axis=1) # (-1x2x2) |
| 79 | return tf.reshape(encoded, tf.shape(boxes)) |
| 80 | |
| 81 | |
| 82 | @under_name_scope() |
no test coverage detected
searching dependent graphs…