1 # -*- coding: utf-8 -*-
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
7 import tensorflow as tf
9 def ious_calu(boxes_1, boxes_2):
12 :param boxes_1: [N, 4] [xmin, ymin, xmax, ymax]
13 :param boxes_2: [M, 4] [xmin, ymin. xmax, ymax]
16 boxes_1 = tf.cast(boxes_1, tf.float32)
17 boxes_2 = tf.cast(boxes_2, tf.float32)
18 xmin_1, ymin_1, xmax_1, ymax_1 = tf.split(boxes_1, 4, axis=1) # xmin_1 shape is [N, 1]..
19 xmin_2, ymin_2, xmax_2, ymax_2 = tf.unstack(boxes_2, axis=1) # xmin_2 shape is [M, ]..
21 max_xmin = tf.maximum(xmin_1, xmin_2)
22 min_xmax = tf.minimum(xmax_1, xmax_2)
24 max_ymin = tf.maximum(ymin_1, ymin_2)
25 min_ymax = tf.minimum(ymax_1, ymax_2)
27 overlap_h = tf.maximum(0., min_ymax - max_ymin) # avoid h < 0
28 overlap_w = tf.maximum(0., min_xmax - max_xmin)
30 overlaps = overlap_h * overlap_w
32 area_1 = (xmax_1 - xmin_1) * (ymax_1 - ymin_1) # [N, 1]
33 area_2 = (xmax_2 - xmin_2) * (ymax_2 - ymin_2) # [M, ]
35 ious = overlaps / (area_1 + area_2 - overlaps)
40 def clip_boxes_to_img_boundaries(decode_boxes, img_shape):
44 :return: decode boxes, and already clip to boundaries
47 with tf.name_scope('clip_boxes_to_img_boundaries'):
49 # xmin, ymin, xmax, ymax = tf.unstack(decode_boxes, axis=1)
50 xmin = decode_boxes[:, 0]
51 ymin = decode_boxes[:, 1]
52 xmax = decode_boxes[:, 2]
53 ymax = decode_boxes[:, 3]
54 img_h, img_w = img_shape[1], img_shape[2]
56 img_h, img_w = tf.cast(img_h, tf.float32), tf.cast(img_w, tf.float32)
58 # xmin = tf.maximum(tf.minimum(xmin, img_w-1.), 0.)
59 # ymin = tf.maximum(tf.minimum(ymin, img_h-1.), 0.)
61 # xmax = tf.maximum(tf.minimum(xmax, img_w-1.), 0.)
62 # ymax = tf.maximum(tf.minimum(ymax, img_h-1.), 0.)
63 xmin = tf.maximum(tf.minimum(xmin, img_w), 0.)
64 ymin = tf.maximum(tf.minimum(ymin, img_h), 0.)
66 xmax = tf.maximum(tf.minimum(xmax, img_w), 0.)
67 ymax = tf.maximum(tf.minimum(ymax, img_h), 0.)
69 return tf.transpose(tf.stack([xmin, ymin, xmax, ymax]))
72 def filter_outside_boxes(boxes, img_h, img_w):
74 :param anchors:boxes with format [xmin, ymin, xmax, ymax]
75 :param img_h: height of image
76 :param img_w: width of image
77 :return: indices of anchors that inside the image boundary
80 with tf.name_scope('filter_outside_boxes'):
81 xmin, ymin, xmax, ymax = tf.unstack(boxes, axis=1)
83 xmin_index = tf.greater_equal(xmin, 0)
84 ymin_index = tf.greater_equal(ymin, 0)
85 xmax_index = tf.less_equal(xmax, tf.cast(img_w, tf.float32))
86 ymax_index = tf.less_equal(ymax, tf.cast(img_h, tf.float32))
88 indices = tf.transpose(tf.stack([xmin_index, ymin_index, xmax_index, ymax_index]))
89 indices = tf.cast(indices, dtype=tf.int32)
90 indices = tf.reduce_sum(indices, axis=1)
91 indices = tf.where(tf.equal(indices, 4))
92 # indices = tf.equal(indices, 4)
93 return tf.reshape(indices, [-1])
96 def padd_boxes_with_zeros(boxes, scores, max_num_of_boxes):
99 num of boxes less than max num of boxes, so it need to pad with zeros[0, 0, 0, 0]
102 :param max_num_of_boxes:
106 pad_num = tf.cast(max_num_of_boxes, tf.int32) - tf.shape(boxes)[0]
108 zero_boxes = tf.zeros(shape=[pad_num, 4], dtype=boxes.dtype)
109 zero_scores = tf.zeros(shape=[pad_num], dtype=scores.dtype)
111 final_boxes = tf.concat([boxes, zero_boxes], axis=0)
113 final_scores = tf.concat([scores, zero_scores], axis=0)
115 return final_boxes, final_scores