def __process_rois(self, regions, class_scores):
""" get relevant regions, with non-maximum suppression and clipping """
region_shape = tf.shape(regions)
width = region_shape[2] * self.feat_stride
height = region_shape[1] * self.feat_stride
anchors = self.get_tiled_anchors_for_shape(width, height)
region_boxes = self.adjust_bbox(tf.reshape(regions,
[-1, 4]), anchors)
class_scores = tf.reshape(class_scores, [-1, 2])
if self.is_training:
# ignore boxes that cross the image boundary
self.outside_anchors = tf.logical_and(tf.logical_and(anchors[:, 0] - anchors[:, 2] / 2.0 >= -self.feat_stride, anchors[:, 1] - anchors[:, 3] / 2.0 >= -self.feat_stride), tf.logical_and(
anchors[:, 0] + anchors[:, 2] / 2.0 < tf.cast(height, tf.float32) + self.feat_stride, anchors[:, 1] + anchors[:, 3] / 2.0 < tf.cast(width, tf.float32) + self.feat_stride))
region_boxes = tf.boolean_mask(region_boxes, self.outside_anchors)
class_scores = tf.boolean_mask(class_scores, self.outside_anchors)
shape = tf.shape(region_boxes)
mask = tf.logical_or(region_boxes[:, 2] >
self.min_box_size,
region_boxes[:,
3] > self.min_box_size)
region_box = tf.boolean_mask(region_boxes, mask)
class_score = tf.boolean_mask(class_scores, mask)
region_box = self.clip_bboxes(region_box, width, height)
class_score = tf.nn.softmax(class_score)
class_score = tf.slice(class_score, [0, 1], [-1, 1])
class_score = tf.reshape(class_score, [-1])
_, idx = tf.nn.top_k(class_score, k=tf.minimum(self.num_proposals, tf.shape(class_score)[0]))
class_score = tf.gather(class_score, idx)
region_box = tf.gather(region_box, idx)
with tf.variable_scope('non_maximum_supression'):
bboxes2 = self.bboxes_to_yxyx(
region_box, tf.cast(height, tf.float32))
idx = tf.image.non_max_suppression(bboxes2, class_score,
self.num_proposals_after_nms,
0.7)
region_box = tf.gather(region_box, idx)
return region_box
评论列表
文章目录