Beispiel #1
0
        def filter_detections(boxes, scores, is_training):

            if is_training:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [-1, ])
            else:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [-1, ])

            if self.cfgs.NMS:
                filtered_boxes = tf.gather(boxes, indices)
                filtered_scores = tf.gather(scores, indices)

                filtered_boxes = tf.py_func(func=backward_convert,
                                            inp=[filtered_boxes, False],
                                            Tout=[tf.float32])
                filtered_boxes = tf.reshape(filtered_boxes, [-1, 5])

                nms_indices = nms_rotate.nms_rotate(decode_boxes=filtered_boxes,
                                                    scores=tf.reshape(filtered_scores, [-1, ]),
                                                    iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                                                    max_output_size=100 if is_training else 1000,
                                                    use_gpu=True)

                # filter indices based on NMS
                indices = tf.gather(indices, nms_indices)

            # add indices to list of all indices
            # return indices
            return indices
        def filter_detections(boxes, scores):
            """
            :param boxes: [-1, 4]
            :param scores: [-1, ]
            :param labels: [-1, ]
            :return:
            """
            if self.is_training:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [-1, ])
            else:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [-1, ])

            if self.cfgs.NMS:
                filtered_boxes = tf.gather(boxes, indices)
                filtered_scores = tf.gather(scores, indices)

                # perform NMS

                nms_indices = nms_rotate.nms_rotate(decode_boxes=filtered_boxes,
                                                    scores=filtered_scores,
                                                    iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                                                    max_output_size=100 if self.is_training else 1000,
                                                    use_gpu=True,
                                                    gpu_id=gpu_id)

                # filter indices based on NMS
                indices = tf.gather(indices, nms_indices)

            # add indices to list of all indices
            return indices
    def postprocess_detctions(self, refine_bbox_pred, refine_cls_prob, refine_angle_prob, refine_boxes, gpu_id):

        # return_boxes_pred = []
        return_boxes_pred_angle = []
        return_scores = []
        return_labels = []
        for j in range(0, self.cfgs.CLASS_NUM):
            scores = refine_cls_prob[:, j]
            if self.is_training:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [-1, ])
            else:
                indices = tf.reshape(tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [-1, ])

            refine_boxes_ = tf.gather(refine_boxes, indices)
            refine_bbox_pred_ = tf.gather(refine_bbox_pred, indices)
            scores = tf.gather(scores, indices)
            refine_angle_prob_ = tf.gather(refine_angle_prob, indices)

            angle_cls = tf.py_func(angle_label_decode,
                                   inp=[refine_angle_prob_, self.cfgs.ANGLE_RANGE, self.cfgs.OMEGA, self.cfgs.ANGLE_MODE],
                                   Tout=[tf.float32])
            angle_cls = tf.reshape(angle_cls, [-1, ]) * -1

            if self.cfgs.ANGLE_RANGE == 180:
                refine_boxes_ = tf.py_func(coordinate_present_convert,
                                           inp=[refine_boxes_, -1],
                                           Tout=[tf.float32])
                refine_boxes_ = tf.reshape(refine_boxes_, [-1, 5])

            refine_boxes_pred = bbox_transform.rbbox_transform_inv_dcl(boxes=refine_boxes_, deltas=refine_bbox_pred_)
            refine_boxes_pred = tf.reshape(refine_boxes_pred, [-1, 4])

            x, y, w, h = tf.unstack(refine_boxes_pred, axis=1)
            refine_boxes_pred_angle = tf.transpose(tf.stack([x, y, w, h, angle_cls]))

            if self.cfgs.ANGLE_RANGE == 180:
                # _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
                # indx = tf.reshape(tf.where(tf.logical_and(tf.less(theta, 0), tf.greater_equal(theta, -180))), [-1, ])
                # boxes_pred = tf.gather(boxes_pred, indx)
                # scores = tf.gather(scores, indx)

                # boxes_pred = tf.py_func(coordinate_present_convert,
                #                         inp=[boxes_pred, 1],
                #                         Tout=[tf.float32])
                # boxes_pred = tf.reshape(boxes_pred, [-1, 5])

                refine_boxes_pred_angle = tf.py_func(coordinate_present_convert,
                                                     inp=[refine_boxes_pred_angle, 1],
                                                     Tout=[tf.float32])
                refine_boxes_pred_angle = tf.reshape(refine_boxes_pred_angle, [-1, 5])

            nms_indices = nms_rotate.nms_rotate(decode_boxes=refine_boxes_pred_angle,
                                                scores=scores,
                                                iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                                                max_output_size=100 if self.is_training else 1000,
                                                use_gpu=True,
                                                gpu_id=gpu_id)

            # tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices), [-1, 5])
            tmp_refine_boxes_pred_angle = tf.reshape(tf.gather(refine_boxes_pred_angle, nms_indices), [-1, 5])
            tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [-1, ])

            # return_boxes_pred.append(tmp_boxes_pred)
            return_boxes_pred_angle.append(tmp_refine_boxes_pred_angle)
            return_scores.append(tmp_scores)
            return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

        # return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
        return_boxes_pred_angle = tf.concat(return_boxes_pred_angle, axis=0)
        return_scores = tf.concat(return_scores, axis=0)
        return_labels = tf.concat(return_labels, axis=0)

        return return_scores, return_labels, return_boxes_pred_angle
Beispiel #4
0
    def postprocess_fastrcnn(self, rois, bbox_ppred, scores, gpu_id):
        '''
        :param rois:[-1, 4]
        :param bbox_ppred: [-1, (cfgs.Class_num+1) * 5]
        :param scores: [-1, cfgs.Class_num + 1]
        :return:
        '''

        with tf.name_scope('postprocess_fastrcnn'):
            rois = tf.stop_gradient(rois)
            scores = tf.stop_gradient(scores)
            bbox_ppred = tf.reshape(bbox_ppred,
                                    [-1, self.cfgs.CLASS_NUM + 1, 5])
            bbox_ppred = tf.stop_gradient(bbox_ppred)

            bbox_pred_list = tf.unstack(bbox_ppred, axis=1)
            score_list = tf.unstack(scores, axis=1)

            allclasses_boxes = []
            allclasses_scores = []
            categories = []

            x_c = (rois[:, 2] + rois[:, 0]) / 2
            y_c = (rois[:, 3] + rois[:, 1]) / 2
            h = rois[:, 2] - rois[:, 0] + 1
            w = rois[:, 3] - rois[:, 1] + 1
            theta = -90 * tf.ones_like(x_c)
            rois = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))
            for i in range(1, self.cfgs.CLASS_NUM + 1):

                # 1. decode boxes in each class
                tmp_encoded_box = bbox_pred_list[i]
                tmp_score = score_list[i]

                tmp_decoded_boxes = bbox_transform.rbbox_transform_inv(
                    boxes=rois,
                    deltas=tmp_encoded_box,
                    scale_factors=self.cfgs.ROI_SCALE_FACTORS)

                # 2. clip to img boundaries
                # tmp_decoded_boxes = boxes_utils.clip_boxes_to_img_boundaries(decode_boxes=tmp_decoded_boxes,
                #                                                              img_shape=img_shape)

                # 3. NMS
                if self.cfgs.SOFT_NMS:
                    print("Using Soft NMS.......")
                    raise NotImplementedError(
                        "soft NMS for rotate has not implemented")

                else:
                    max_output_size = 4000 if 'DOTA' in self.cfgs.NET_NAME else 200
                    keep = nms_rotate.nms_rotate(
                        decode_boxes=tmp_decoded_boxes,
                        scores=tmp_score,
                        iou_threshold=self.cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
                        max_output_size=100
                        if self.is_training else max_output_size,
                        use_gpu=self.cfgs.ROTATE_NMS_USE_GPU,
                        gpu_id=gpu_id)

                perclass_boxes = tf.gather(tmp_decoded_boxes, keep)
                perclass_scores = tf.gather(tmp_score, keep)

                allclasses_boxes.append(perclass_boxes)
                allclasses_scores.append(perclass_scores)
                categories.append(tf.ones_like(perclass_scores) * i)

            final_boxes = tf.concat(allclasses_boxes, axis=0)
            final_scores = tf.concat(allclasses_scores, axis=0)
            final_category = tf.concat(categories, axis=0)

            if self.is_training:
                '''
                in training. We should show the detecitons in the tensorboard. So we add this.
                '''
                kept_indices = tf.reshape(
                    tf.where(
                        tf.greater_equal(final_scores, self.cfgs.VIS_SCORE)),
                    [-1])
            else:
                kept_indices = tf.reshape(
                    tf.where(
                        tf.greater_equal(final_scores,
                                         self.cfgs.FILTERED_SCORE)), [-1])
            final_boxes = tf.gather(final_boxes, kept_indices)
            final_scores = tf.gather(final_scores, kept_indices)
            final_category = tf.gather(final_category, kept_indices)

            return final_boxes, final_scores, final_category
Beispiel #5
0
    def postprocess_detctions(self, rpn_bbox_pred, rpn_cls_prob,
                              rpn_angle_prob, anchors, gpu_id):

        return_boxes_pred = []
        return_boxes_pred_angle = []
        return_scores = []
        return_labels = []
        for j in range(0, self.cfgs.CLASS_NUM):
            scores = rpn_cls_prob[:, j]
            if self.is_training:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [
                        -1,
                    ])
            else:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [
                        -1,
                    ])

            anchors_ = tf.gather(anchors, indices)
            rpn_bbox_pred_ = tf.gather(rpn_bbox_pred, indices)
            scores = tf.gather(scores, indices)
            rpn_angle_prob_ = tf.gather(rpn_angle_prob, indices)
            angle_cls = tf.cast(tf.argmax(rpn_angle_prob_, axis=1), tf.float32)

            if self.cfgs.METHOD == 'H':
                x_c = (anchors_[:, 2] + anchors_[:, 0]) / 2
                y_c = (anchors_[:, 3] + anchors_[:, 1]) / 2
                h = anchors_[:, 2] - anchors_[:, 0] + 1
                w = anchors_[:, 3] - anchors_[:, 1] + 1
                theta = -90 * tf.ones_like(x_c)
                anchors_ = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))

            if self.cfgs.ANGLE_RANGE == 180:
                anchors_ = tf.py_func(coordinate_present_convert,
                                      inp=[anchors_, -1],
                                      Tout=[tf.float32])
                anchors_ = tf.reshape(anchors_, [-1, 5])

            boxes_pred = bbox_transform.rbbox_transform_inv(
                boxes=anchors_, deltas=rpn_bbox_pred_)

            boxes_pred = tf.reshape(boxes_pred, [-1, 5])
            angle_cls = (tf.reshape(angle_cls, [
                -1,
            ]) * -1 - 0.5) * self.cfgs.OMEGA

            x, y, w, h, theta = tf.unstack(boxes_pred, axis=1)
            boxes_pred_angle = tf.transpose(tf.stack([x, y, w, h, angle_cls]))

            if self.cfgs.ANGLE_RANGE == 180:
                # _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
                # indx = tf.reshape(tf.where(tf.logical_and(tf.less(theta, 0), tf.greater_equal(theta, -180))), [-1, ])
                # boxes_pred = tf.gather(boxes_pred, indx)
                # scores = tf.gather(scores, indx)

                boxes_pred = tf.py_func(coordinate_present_convert,
                                        inp=[boxes_pred, 1],
                                        Tout=[tf.float32])
                boxes_pred = tf.reshape(boxes_pred, [-1, 5])

                boxes_pred_angle = tf.py_func(coordinate_present_convert,
                                              inp=[boxes_pred_angle, 1],
                                              Tout=[tf.float32])
                boxes_pred_angle = tf.reshape(boxes_pred_angle, [-1, 5])

            max_output_size = 4000 if 'DOTA' in self.cfgs.NET_NAME else 200
            nms_indices = nms_rotate.nms_rotate(
                decode_boxes=boxes_pred,
                scores=scores,
                iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                max_output_size=100 if self.is_training else max_output_size,
                use_gpu=True,
                gpu_id=gpu_id)

            tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices),
                                        [-1, 5])
            tmp_boxes_pred_angle = tf.reshape(
                tf.gather(boxes_pred_angle, nms_indices), [-1, 5])
            tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [
                -1,
            ])

            return_boxes_pred.append(tmp_boxes_pred)
            return_boxes_pred_angle.append(tmp_boxes_pred_angle)
            return_scores.append(tmp_scores)
            return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

        return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
        return_boxes_pred_angle = tf.concat(return_boxes_pred_angle, axis=0)
        return_scores = tf.concat(return_scores, axis=0)
        return_labels = tf.concat(return_labels, axis=0)

        return return_boxes_pred, return_scores, return_labels, return_boxes_pred_angle
Beispiel #6
0
    def postprocess_detctions(self, rpn_bbox_pred, rpn_cls_prob, anchors):

        return_boxes_pred = []
        return_scores = []
        return_labels = []
        for j in range(0, self.cfgs.CLASS_NUM):
            scores = rpn_cls_prob[:, j]
            if self.is_training:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [
                        -1,
                    ])
            else:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [
                        -1,
                    ])

            anchors_ = tf.gather(anchors, indices)
            rpn_bbox_pred_ = tf.gather(rpn_bbox_pred, indices)
            scores = tf.gather(scores, indices)

            if self.method == 'H':
                x_c = (anchors_[:, 2] + anchors_[:, 0]) / 2
                y_c = (anchors_[:, 3] + anchors_[:, 1]) / 2
                h = anchors_[:, 2] - anchors_[:, 0] + 1
                w = anchors_[:, 3] - anchors_[:, 1] + 1
                theta = -90 * tf.ones_like(x_c)
                anchors_ = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))

            if self.cfgs.ANGLE_RANGE == 180:
                anchors_ = tf.py_func(coordinate_present_convert,
                                      inp=[anchors_, -1],
                                      Tout=[tf.float32])
                anchors_ = tf.reshape(anchors_, [-1, 5])

            boxes_pred = bbox_transform.rbbox_transform_inv(
                boxes=anchors_, deltas=rpn_bbox_pred_)

            if self.cfgs.ANGLE_RANGE == 180:
                _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
                indx = tf.reshape(
                    tf.where(
                        tf.logical_and(tf.less(theta, 0),
                                       tf.greater_equal(theta, -180))), [
                                           -1,
                                       ])
                boxes_pred = tf.gather(boxes_pred, indx)
                scores = tf.gather(scores, indx)

                boxes_pred = tf.py_func(coordinate_present_convert,
                                        inp=[boxes_pred, 1],
                                        Tout=[tf.float32])
                boxes_pred = tf.reshape(boxes_pred, [-1, 5])

            nms_indices = nms_rotate.nms_rotate(
                decode_boxes=boxes_pred,
                scores=scores,
                iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                max_output_size=100 if self.is_training else 1000,
                use_gpu=False)

            tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices),
                                        [-1, 5])
            tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [
                -1,
            ])

            return_boxes_pred.append(tmp_boxes_pred)
            return_scores.append(tmp_scores)
            return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

        return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
        return_scores = tf.concat(return_scores, axis=0)
        return_labels = tf.concat(return_labels, axis=0)

        return return_boxes_pred, return_scores, return_labels
    def postprocess_detctions(self, rpn_bbox_pred, rpn_cls_prob, anchors,
                              gpu_id):

        return_boxes_pred = []
        return_scores = []
        return_labels = []
        for j in range(0, self.cfgs.CLASS_NUM):
            scores = rpn_cls_prob[:, j]
            if self.is_training:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.VIS_SCORE)), [
                        -1,
                    ])
            else:
                indices = tf.reshape(
                    tf.where(tf.greater(scores, self.cfgs.FILTERED_SCORE)), [
                        -1,
                    ])

            anchors_ = tf.gather(anchors, indices)
            rpn_bbox_pred_ = tf.gather(rpn_bbox_pred, indices)
            scores = tf.gather(scores, indices)

            if self.method == 'H':
                x_c = (anchors_[:, 2] + anchors_[:, 0]) / 2
                y_c = (anchors_[:, 3] + anchors_[:, 1]) / 2
                h = anchors_[:, 2] - anchors_[:, 0] + 1
                w = anchors_[:, 3] - anchors_[:, 1] + 1
                theta = -90 * tf.ones_like(x_c)
                anchors_ = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))

            if self.cfgs.ANGLE_RANGE == 180:
                anchors_ = tf.py_func(coordinate_present_convert,
                                      inp=[anchors_, -1],
                                      Tout=[tf.float32])
                anchors_ = tf.reshape(anchors_, [-1, 5])

            boxes_pred = bbox_transform.rbbox_transform_inv(
                boxes=anchors_, deltas=rpn_bbox_pred_)

            x, y, w, h, _ = tf.unstack(boxes_pred, axis=1)
            theta = tf.atan(rpn_bbox_pred_[:, -2] /
                            rpn_bbox_pred_[:, -1]) * 180 / 3.1415926
            boxes_pred = tf.transpose(tf.stack([x, y, w, h, theta]))

            if self.cfgs.ANGLE_RANGE == 180:

                boxes_pred = tf.py_func(coordinate_present_convert,
                                        inp=[boxes_pred, 1, False],
                                        Tout=[tf.float32])
                boxes_pred = tf.reshape(boxes_pred, [-1, 5])

            # max_output_size = 4000 if 'DOTA' in self.cfgs.NET_NAME else 200
            max_output_size = 100
            nms_indices = nms_rotate.nms_rotate(
                decode_boxes=boxes_pred,
                scores=scores,
                iou_threshold=self.cfgs.NMS_IOU_THRESHOLD,
                max_output_size=100 if self.is_training else max_output_size,
                use_gpu=True,
                gpu_id=gpu_id)

            tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices),
                                        [-1, 5])
            tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [
                -1,
            ])

            return_boxes_pred.append(tmp_boxes_pred)
            return_scores.append(tmp_scores)
            return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

        return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
        return_scores = tf.concat(return_scores, axis=0)
        return_labels = tf.concat(return_labels, axis=0)

        return return_boxes_pred, return_scores, return_labels