示例#1
0
def get_coordinate(predicted_offsets,
                   anchor_boxes,
                   gt_classes,
                   nms_threshold=0.1):

    #cur_target = torch.from_numpy(target)
    #gt_classes = original_gt_classes

    inds = (gt_classes != 0)
    anchor_boxes = anchor_boxes[inds]
    predicted_offsets = predicted_offsets[inds]
    gt_classes = gt_classes[inds]

    delta_x = predicted_offsets[:, 0]
    delta_y = predicted_offsets[:, 1]
    delta_scaleX = predicted_offsets[:, 2]
    delta_scaleY = predicted_offsets[:, 3]

    gt_widths = anchor_boxes[:, 2] - anchor_boxes[:, 0]
    gt_heights = anchor_boxes[:, 3] - anchor_boxes[:, 1]
    gt_center_x = anchor_boxes[:, 0] + 0.5 * gt_widths
    gt_center_y = anchor_boxes[:, 1] + 0.5 * gt_heights

    ex_width = gt_widths / torch.exp(delta_scaleX)
    ex_height = gt_heights / torch.exp(delta_scaleY)
    ex_center_x = gt_center_x - delta_x * ex_width
    ex_center_y = gt_center_y - delta_y * ex_height

    ex1 = ex_center_x - 0.5 * ex_width
    ex2 = ex_center_x + 0.5 * ex_width
    ey1 = ex_center_y - 0.5 * ex_height
    ey2 = ex_center_y + 0.5 * ex_height

    pred_boxes = torch.cat([
        ex1.unsqueeze(0),
        ey1.unsqueeze(0),
        ex2.unsqueeze(0),
        ey2.unsqueeze(0)
    ],
                           dim=0).permute(1, 0)
    pred_boxes = pred_boxes.type(torch.float32)
    gt_classes = gt_classes.type(torch.float32)
    #cur_target = cur_target.type(torch.float32)

    inds = nms(pred_boxes, gt_classes, nms_threshold)
    pred_boxes = pred_boxes[inds]
    coordinate_list = []

    for box in pred_boxes:
        x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
        x1 = (x1 - 400) / 10
        x2 = (x2 - 400) / 10
        y1 = (y1 - 400) / -10
        y2 = (y2 - 400) / -10
        width = abs(x1 - x2)
        height = abs(y1 - y2)
        coordinate_list.append(
            torch.tensor([x2, x2, x1, x1, y2, y1, y2, y1]).view(-1, 4))
    coordinate_list = torch.stack(coordinate_list)
    return coordinate_list
示例#2
0
def Transform_coor(anchor_boxes,
                   gt_offsets,
                   gt_classes,
                   batch_sz,
                   nms_threshold=0.1,
                   plot=False):
    #2,25600000,4
    # print(anchor_boxes.shape)
    # print(gt_offsets.shape)
    #2,2560000
    # print(gt_classes)
    inds = (gt_classes != 0)
    #print(inds.shape)
    anchor_boxes = anchor_boxes[inds]
    gt_offsets = gt_offsets[inds]
    gt_classes = gt_classes[inds]

    delta_x = gt_offsets[:, 0]
    delta_y = gt_offsets[:, 1]
    delta_scaleX = gt_offsets[:, 2]
    delta_scaleY = gt_offsets[:, 3]
    gt_widths = anchor_boxes[:, 2] - anchor_boxes[:, 0]
    gt_heights = anchor_boxes[:, 3] - anchor_boxes[:, 1]
    gt_center_x = anchor_boxes[:, 0] + 0.5 * gt_widths
    gt_center_y = anchor_boxes[:, 1] + 0.5 * gt_heights

    ex_width = gt_widths / torch.exp(delta_scaleX)
    ex_height = gt_heights / torch.exp(delta_scaleY)
    ex_center_x = gt_center_x - delta_x * ex_width
    ex_center_y = gt_center_y - delta_y * ex_height

    ex1 = ex_center_x - 0.5 * ex_width
    ex2 = ex_center_x + 0.5 * ex_width
    ey1 = ex_center_y - 0.5 * ex_height
    ey2 = ex_center_y + 0.5 * ex_height
    #print(ex1.shape)

    batch_coor = []
    for i in range(batch_sz):
        pred_boxes = torch.cat([
            ex1[i].unsqueeze(0), ey1[i].unsqueeze(0), ex2[i].unsqueeze(0),
            ey2[i].unsqueeze(0)
        ],
                               dim=0).permute(1, 0)
        pred_boxes = pred_boxes.type(torch.float32)
        gt_classes = gt_classes[i].type(torch.float32)
        #749543,4
        #print(pred_boxes.shape)
        #749543
        #print(gt_classes.shape)
        inds = nms(pred_boxes, gt_classes, nms_threshold)
        pred_boxes = pred_boxes[inds]
        coordinate_list = []

        for box in pred_boxes:
            x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
            width = abs(x1 - x2)
            height = abs(y1 - y2)
            coordinate_list.append(
                torch.tensor([x2, x2, x1, x1, y2, y1, y2, y1]).view(-1, 4))

        coordinate_list = torch.stack(coordinate_list)
        #print(coordinate_list.shape)
        batch_coor.append(coordinate_list)
        #print(batch_coor.shape)
    final_coor = torch.stack(batch_coor)

    # if plot:
    #   fig,ax = plt.subplots(1)
    #   a = torch.zeros(800,800)
    #   ax.imshow(a)
    #   for box in pred_boxes:
    #     x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
    #     rect = patches.Rectangle((x1,y1),abs(x1 - x2),abs(y1 - y2),linewidth=1,edgecolor='r',facecolor='none')
    #     ax.add_patch(rect)

    # plt.show()

    return final_coor
示例#3
0
def batched_coor_threat(anchor_boxes,
                        gt_offsets,
                        target,
                        gt_classes,
                        batch_sz,
                        nms_threshold=0.1,
                        plot=False):
    # predicted offsets, target_offests, coor_in_meter
    batch_coor = []
    batched_threat_sum = 0

    for i in range(batch_sz):
        anchor_boxes = anchor_boxes[i]
        gt_offsets = gt_offsets[i]
        #target = target[1]['bounding_box']
        if i == 0:
            target = torch.from_numpy(target[0])
        # print(target)
        # print(type(target[i]))
        #target = torch.from_numpy(target[i])
        gt_classes = gt_classes[i]
        #2,25600000,4
        # print(anchor_boxes.shape)
        # print(gt_offsets.shape)
        #2,2560000
        # print(gt_classes)
        inds = (gt_classes != 0)
        #print(inds.shape)
        anchor_boxes = anchor_boxes[inds]
        gt_offsets = gt_offsets[inds]
        gt_classes = gt_classes[inds]

        # target offset
        delta_x = gt_offsets[:, 0]
        delta_y = gt_offsets[:, 1]
        delta_scaleX = gt_offsets[:, 2]
        delta_scaleY = gt_offsets[:, 3]
        gt_widths = anchor_boxes[:, 2] - anchor_boxes[:, 0]
        gt_heights = anchor_boxes[:, 3] - anchor_boxes[:, 1]
        gt_center_x = anchor_boxes[:, 0] + 0.5 * gt_widths
        gt_center_y = anchor_boxes[:, 1] + 0.5 * gt_heights

        ex_width = gt_widths / torch.exp(delta_scaleX)
        ex_height = gt_heights / torch.exp(delta_scaleY)
        ex_center_x = gt_center_x - delta_x * ex_width
        ex_center_y = gt_center_y - delta_y * ex_height

        ex1 = ex_center_x - 0.5 * ex_width
        ex2 = ex_center_x + 0.5 * ex_width
        ey1 = ex_center_y - 0.5 * ex_height
        ey2 = ex_center_y + 0.5 * ex_height
        #print(ex1.shape)

        pred_boxes = torch.cat([
            ex1.unsqueeze(0),
            ey1.unsqueeze(0),
            ex2.unsqueeze(0),
            ey2.unsqueeze(0)
        ],
                               dim=0).permute(1, 0)
        pred_boxes = pred_boxes.type(torch.float32)
        gt_classes = gt_classes.type(torch.float32)
        target = target.type(torch.float32)
        #749543,4
        #print(pred_boxes.shape)
        #749543
        #print(gt_classes.shape)
        # print(pred_boxes)
        # print(min(gt_classes))
        # print(gt_classes)
        # print(gt_classes.shape)
        inds = nms(pred_boxes, gt_classes, nms_threshold)
        pred_boxes = pred_boxes[inds]
        coordinate_list = []

        for box in pred_boxes:
            x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
            width = abs(x1 - x2)
            height = abs(y1 - y2)
            coordinate_list.append(
                torch.tensor([x2, x2, x1, x1, y2, y1, y2, y1]).view(-1, 4))

        coordinate_list = torch.stack(coordinate_list)
        batched_threat_sum += compute_ats_bounding_boxes(
            coordinate_list, target)

        #print(coordinate_list.shape)
        batch_coor.append(coordinate_list)
        #print(len(batch_coor))
    #final_coor = torch.stack(batch_coor)

    # if plot:
    #   fig,ax = plt.subplots(1)
    #   a = torch.zeros(800,800)
    #   ax.imshow(a)
    #   for box in pred_boxes:
    #     x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
    #     rect = patches.Rectangle((x1,y1),abs(x1 - x2),abs(y1 - y2),linewidth=1,edgecolor='r',facecolor='none')
    #     ax.add_patch(rect)

    # plt.show()

    return batch_coor, batched_threat_sum
示例#4
0
def batched_coor_threat_updated(ite,
                                predicted_offsets,
                                anchor_boxes,
                                target,
                                gt_classes,
                                batch_sz,
                                nms_threshold=0.1,
                                plot=False):
    # predicted offsets, target_offests, coor_in_meter
    batch_coor = []
    batched_threat_sum = 0
    original_anchor = anchor_boxes.clone()
    original_predicted_offsets = predicted_offsets.clone()
    original_gt_classes = gt_classes.clone()

    for i in range(batch_sz):
        anchor_boxes = original_anchor
        predicted_offsets = original_predicted_offsets[i]
        if i == 0:
            cur_target = torch.from_numpy(target[0])
        else:
            cur_target = torch.from_numpy(target[1])

        gt_classes = original_gt_classes[i]

        inds = (gt_classes != 0)
        anchor_boxes = anchor_boxes[inds]
        predicted_offsets = predicted_offsets[inds]
        gt_classes = gt_classes[inds]

        delta_x = predicted_offsets[:, 0]
        delta_y = predicted_offsets[:, 1]
        delta_scaleX = predicted_offsets[:, 2]
        delta_scaleY = predicted_offsets[:, 3]

        gt_widths = anchor_boxes[:, 2] - anchor_boxes[:, 0]
        gt_heights = anchor_boxes[:, 3] - anchor_boxes[:, 1]
        gt_center_x = anchor_boxes[:, 0] + 0.5 * gt_widths
        gt_center_y = anchor_boxes[:, 1] + 0.5 * gt_heights

        ex_width = gt_widths / torch.exp(delta_scaleX)
        ex_height = gt_heights / torch.exp(delta_scaleY)
        ex_center_x = gt_center_x - delta_x * ex_width
        ex_center_y = gt_center_y - delta_y * ex_height

        ex1 = ex_center_x - 0.5 * ex_width
        ex2 = ex_center_x + 0.5 * ex_width
        ey1 = ex_center_y - 0.5 * ex_height
        ey2 = ex_center_y + 0.5 * ex_height

        pred_boxes = torch.cat([
            ex1.unsqueeze(0),
            ey1.unsqueeze(0),
            ex2.unsqueeze(0),
            ey2.unsqueeze(0)
        ],
                               dim=0).permute(1, 0)
        pred_boxes = pred_boxes.type(torch.float32)
        gt_classes = gt_classes.type(torch.float32)
        cur_target = cur_target.type(torch.float32)

        inds = nms(pred_boxes, gt_classes, nms_threshold)
        pred_boxes = pred_boxes[inds]
        coordinate_list = []

        # fig,ax = plt.subplots(1)
        # a = torch.zeros(800,800)
        # ax.imshow(a)
        # for box in pred_boxes:
        #   x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
        #   rect = patches.Rectangle((x1,y1),abs(x1 - x2),abs(y1 - y2),linewidth=1,edgecolor='r',facecolor='none')
        #   ax.add_patch(rect)

        # plt.savefig('/content/drive/My Drive/self_dl/predict_{}_{}.png'.format(ite, i))

        for box in pred_boxes:
            x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
            x1 = (x1 - 400) / 10
            x2 = (x2 - 400) / 10
            y1 = (y1 - 400) / -10
            y2 = (y2 - 400) / -10
            width = abs(x1 - x2)
            height = abs(y1 - y2)
            coordinate_list.append(
                torch.tensor([x2, x2, x1, x1, y2, y1, y2, y1]).view(-1, 4))

        coordinate_list = torch.stack(coordinate_list)
        batched_threat_sum += compute_ats_bounding_boxes(
            coordinate_list, cur_target)
        batch_coor.append(coordinate_list)
        #visActual(cur_target, ite,i)
    return batch_coor, batched_threat_sum
示例#5
0
def box_results_with_nms_and_limit(
        scores,
        boxes,
        num_classes=81,
        score_thresh=0.05,
        overlap_thresh=0.5,
        do_soft_nms=False,
        soft_nms_sigma=0.5,
        soft_nms_method='linear',
        do_bbox_vote=False,
        bbox_vote_thresh=0.8,
        bbox_vote_method='ID',
        max_detections_per_img=100,  ### over all classes ###
):
    """Returns bounding-box detection results by thresholding on scores and
    applying non-maximum suppression (NMS).
    
    A number of #detections presist after this and are returned, sorted by class

    `boxes` has shape (#detections, 4 * #classes), where each row represents
    a list of predicted bounding boxes for each of the object classes in the
    dataset (including the background class). The detections in each row
    originate from the same object proposal.

    `scores` has shape (#detection, #classes), where each row represents a list
    of object detection confidence scores for each of the object classes in the
    dataset (including the background class). `scores[i, j]`` corresponds to the
    box at `boxes[i, j * 4:(j + 1) * 4]`.
    """
    cls_boxes = [[] for _ in range(num_classes)]
    # Apply threshold on detection probabilities and apply NMS
    # Skip j = 0, because it's the background class
    for j in range(1, num_classes):
        inds = np.where(scores[:, j] > score_thresh)[0]
        scores_j = scores[inds, j]
        boxes_j = boxes[inds, j * 4:(j + 1) * 4]
        dets_j = np.hstack((boxes_j, scores_j[:,
                                              np.newaxis])).astype(np.float32,
                                                                   copy=False)
        if do_soft_nms:
            nms_dets, _ = box_utils.soft_nms(dets_j,
                                             sigma=soft_nms_sigma,
                                             overlap_thresh=overlap_thresh,
                                             score_thresh=0.0001,
                                             method=soft_nms_method)
        else:
            keep = box_utils.nms(dets_j, overlap_thresh)
            nms_dets = dets_j[keep, :]
        # Refine the post-NMS boxes using bounding-box voting
        if do_bbox_vote:
            nms_dets = box_utils.box_voting(nms_dets,
                                            dets_j,
                                            bbox_vote_thresh,
                                            scoring_method=bbox_vote_method)
        cls_boxes[j] = nms_dets

    # Limit to max_per_image detections **over all classes**
    if max_detections_per_img > 0:
        image_scores = np.hstack(
            [cls_boxes[j][:, -1] for j in range(1, num_classes)])
        if len(image_scores) > max_detections_per_img:
            image_thresh = np.sort(image_scores)[-max_detections_per_img]
            for j in range(1, num_classes):
                keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0]
                cls_boxes[j] = cls_boxes[j][keep, :]

    im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)])
    boxes = im_results[:, :-1]
    scores = im_results[:, -1]
    return scores, boxes, cls_boxes
示例#6
0
def Transform_coor(anchor_boxes,
                   gt_offsets,
                   gt_classes,
                   nms_threshold=0.1,
                   plot=False):
    inds = (gt_classes != 0)
    anchor_boxes = anchor_boxes[inds]
    gt_offsets = gt_offsets[inds]
    gt_classes = gt_classes[inds]

    delta_x = gt_offsets[:, 0]
    delta_y = gt_offsets[:, 1]
    delta_scaleX = gt_offsets[:, 2]
    delta_scaleY = gt_offsets[:, 3]
    gt_widths = anchor_boxes[:, 2] - anchor_boxes[:, 0]
    gt_heights = anchor_boxes[:, 3] - anchor_boxes[:, 1]
    gt_center_x = anchor_boxes[:, 0] + 0.5 * gt_widths
    gt_center_y = anchor_boxes[:, 1] + 0.5 * gt_heights

    ex_width = gt_widths / torch.exp(delta_scaleX)
    ex_height = gt_heights / torch.exp(delta_scaleY)
    ex_center_x = gt_center_x - delta_x * ex_width
    ex_center_y = gt_center_y - delta_y * ex_height

    ex1 = ex_center_x - 0.5 * ex_width
    ex2 = ex_center_x + 0.5 * ex_width
    ey1 = ex_center_y - 0.5 * ex_height
    ey2 = ex_center_y + 0.5 * ex_height

    pred_boxes = torch.cat([
        ex1.unsqueeze(0),
        ey1.unsqueeze(0),
        ex2.unsqueeze(0),
        ey2.unsqueeze(0)
    ],
                           dim=0).permute(1, 0)
    pred_boxes = pred_boxes.type(torch.float32)
    gt_classes = gt_classes.type(torch.float32)

    inds = nms(pred_boxes, gt_classes, nms_threshold)
    pred_boxes = pred_boxes[inds]
    coordinate_list = []
    for box in pred_boxes:
        x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
        width = abs(x1 - x2)
        height = abs(y1 - y2)
        coordinate_list.append(
            torch.tensor([x2, x2, x1, x1, y2, y1, y2, y1]).view(-1, 4))

    coordinate_list = torch.stack(coordinate_list)

    if plot:
        fig, ax = plt.subplots(1)
        a = torch.zeros(800, 800)
        ax.imshow(a)
        for box in pred_boxes:
            x1, y1, x2, y2 = box[0], box[1], box[2], box[3]
            rect = patches.Rectangle((x1, y1),
                                     abs(x1 - x2),
                                     abs(y1 - y2),
                                     linewidth=1,
                                     edgecolor='r',
                                     facecolor='none')
            ax.add_patch(rect)

    plt.show()

    return coordinate_list