def batch_iou_fast(anchors, bboxes):
""" Compute iou of two batch of boxes. Box format '[y_min, x_min, y_max, x_max]'.
Args:
anchors: know shape
bboxes: dynamic shape
Return:
ious: 2-D with shape '[num_bboxes, num_anchors]'
indices: [num_bboxes, 1]
"""
num_anchors = anchors.get_shape().as_list()[0]
tensor_num_bboxes = tf.shape(bboxes)[0]
indices = tf.reshape(tf.range(tensor_num_bboxes), shape=[-1, 1])
indices = tf.reshape(tf.stack([indices]*num_anchors, axis=1), shape=[-1, 1])
bboxes_m = tf.gather_nd(bboxes, indices)
anchors_m = tf.tile(anchors, [tensor_num_bboxes, 1])
lr = tf.maximum(
tf.minimum(bboxes_m[:, 3], anchors_m[:, 3]) -
tf.maximum(bboxes_m[:, 1], anchors_m[:, 1]),
0
)
tb = tf.maximum(
tf.minimum(bboxes_m[:, 2], anchors_m[:, 2]) -
tf.maximum(bboxes_m[:, 0], anchors_m[:, 0]),
0
)
intersection = tf.multiply(tb, lr)
union = tf.subtract(
tf.multiply((bboxes_m[:, 3] - bboxes_m[:, 1]), (bboxes_m[:, 2] - bboxes_m[:, 0])) +
tf.multiply((anchors_m[:, 3] - anchors_m[:, 1]), (anchors_m[:, 2] - anchors_m[:, 0])),
intersection
)
ious = tf.div(intersection, union)
ious = tf.reshape(ious, shape=[tensor_num_bboxes, num_anchors])
indices = tf.arg_max(ious, dimension=1)
return ious, indices
评论列表
文章目录