def generate_anchors(boxes, height, width, conv_height, conv_width):
'''Generate anchors for given geometry
boxes: K x 2 tensor for anchor geometries, K different sizes
height: source image height
width: source image width
conv_height: convolution layer height
conv_width: convolution layer width
returns:
conv_height x conv_width x K x 4 tensor with boxes for all
positions. Last dimension 4 numbers are (y, x, h, w)
'''
k, _ = boxes.get_shape().as_list()
height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32)
grid = tf.transpose(tf.stack(tf.meshgrid(
tf.linspace(-0.5, height - 0.5, conv_height),
tf.linspace(-0.5, width - 0.5, conv_width)), axis=2), [1, 0, 2])
# convert boxes from K x 2 to 1 x 1 x K x 2
boxes = tf.expand_dims(tf.expand_dims(boxes, 0), 0)
# convert grid from H' x W' x 2 to H' x W' x 1 x 2
grid = tf.expand_dims(grid, 2)
# combine them into single H' x W' x K x 4 tensor
return tf.concat(
3,
[tf.tile(grid, [1, 1, k, 1]),
tf.tile(boxes, [conv_height, conv_width, 1, 1])]
)
评论列表
文章目录