Exemplo n.º 1
0
    def rpn_proposals(self):
        with tf.variable_scope('rpn_proposals'):
            rpn_decode_boxes = encode_and_decode.decode_boxes(encode_boxes=self.rpn_encode_boxes,
                                                              reference_boxes=self.anchors,
                                                              scale_factors=self.scale_factors)

            if not self.is_training:  # when test, clip proposals to img boundaries
                img_shape = tf.shape(self.img_batch)
                rpn_decode_boxes = boxes_utils.clip_boxes_to_img_boundaries(rpn_decode_boxes, img_shape)

            rpn_softmax_scores = slim.softmax(self.rpn_scores)
            rpn_object_score = rpn_softmax_scores[:, 1]  # second column represent object

            if self.top_k_nms:
                rpn_object_score, top_k_indices = tf.nn.top_k(rpn_object_score, k=self.top_k_nms)
                rpn_decode_boxes = tf.gather(rpn_decode_boxes, top_k_indices)

            valid_indices = nms.non_maximal_suppression(boxes=rpn_decode_boxes,
                                                        scores=rpn_object_score,
                                                        max_output_size=self.max_proposals_num,
                                                        iou_threshold=self.rpn_nms_iou_threshold)

            valid_boxes = tf.gather(rpn_decode_boxes, valid_indices)
            valid_scores = tf.gather(rpn_object_score, valid_indices)
            rpn_proposals_boxes, rpn_proposals_scores = tf.cond(
                tf.less(tf.shape(valid_boxes)[0], self.max_proposals_num),
                lambda: boxes_utils.padd_boxes_with_zeros(valid_boxes, valid_scores,
                                                          self.max_proposals_num),
                lambda: (valid_boxes, valid_scores))

            return rpn_proposals_boxes, rpn_proposals_scores
Exemplo n.º 2
0
    def rpn_proposals(self):
        with tf.variable_scope('rpn_proposals'):
            rpn_decode_boxes = encode_and_decode.decode_boxes(
                encode_boxes=self.rpn_encode_boxes,
                reference_boxes=self.anchors,
                scale_factors=self.scale_factors)

            # if not self.is_training:  # when test, clip proposals to img boundaries
            #     img_shape = tf.shape(self.img_batch)
            #     rpn_decode_boxes = boxes_utils.clip_boxes_to_img_boundaries(rpn_decode_boxes, img_shape)

            rpn_softmax_scores = slim.softmax(self.rpn_scores)
            rpn_object_score = rpn_softmax_scores[:,
                                                  1]  # second column represent object

            if self.top_k_nms:
                rpn_object_score, top_k_indices = tf.nn.top_k(rpn_object_score,
                                                              k=self.top_k_nms)
                rpn_decode_boxes = tf.gather(rpn_decode_boxes, top_k_indices)

            if not cfgs.USE_HORIZONTAL_NMS:
                valid_indices = nms_rotate.nms_rotate(
                    decode_boxes=rpn_decode_boxes,
                    scores=rpn_object_score,
                    iou_threshold=self.rpn_nms_iou_threshold,
                    max_output_size=self.max_proposals_num,
                    use_angle_condition=self.use_angles_condition,
                    angle_threshold=self.anchor_angle_threshold,
                    use_gpu=cfgs.NMS_USE_GPU)

            ############################################################################################################
            else:
                rpn_decode_boxes_convert = tf.py_func(
                    coordinate_convert.forward_convert,
                    inp=[rpn_decode_boxes],
                    Tout=tf.float32)

                rpn_decode_boxes_convert = tf.reshape(
                    rpn_decode_boxes_convert,
                    [tf.shape(rpn_decode_boxes)[0], 8])
                x1, y1, x2, y2, x3, y3, x4, y4 = tf.unstack(
                    rpn_decode_boxes_convert, axis=1)
                x = tf.transpose(tf.stack([x1, x2, x3, x4]))
                y = tf.transpose(tf.stack([y1, y2, y3, y4]))
                min_x = tf.reduce_min(x, axis=1)
                max_x = tf.reduce_max(x, axis=1)
                min_y = tf.reduce_min(y, axis=1)
                max_y = tf.reduce_max(y, axis=1)
                rpn_decode_boxes_convert = tf.transpose(
                    tf.stack([min_x, min_y, max_x, max_y]))

                valid_indices = tf.image.non_max_suppression(
                    boxes=rpn_decode_boxes_convert,
                    scores=rpn_object_score,
                    max_output_size=self.max_proposals_num,
                    iou_threshold=self.rpn_nms_iou_threshold,
                    name='rpn_horizontal_nms')

            ############################################################################################################

            valid_boxes = tf.gather(rpn_decode_boxes, valid_indices)
            valid_scores = tf.gather(rpn_object_score, valid_indices)
            rpn_proposals_boxes, rpn_proposals_scores = tf.cond(
                tf.less(tf.shape(valid_boxes)[0], self.max_proposals_num),
                lambda: boxes_utils.padd_boxes_with_zeros(
                    valid_boxes, valid_scores, self.max_proposals_num), lambda:
                (valid_boxes, valid_scores))

            return rpn_proposals_boxes, rpn_proposals_scores