示例#1
0
def visualize_config_anchors(image, gt_anchors, cfg):

    # anchor_generator = make_anchor_generator(cfg)
    anchor_sizes = cfg.RPN.ANCHOR_SIZES
    anchor_ratios = cfg.RPN.ASPECT_RATIOS
    stride = cfg.RPN.ANCHOR_STRIDE[0]
    anchor_angles = cfg.RPN.ANCHOR_ANGLES
    H, W = image.shape[:2]

    total_anchors = len(anchor_angles) * len(anchor_ratios) * len(anchor_sizes)
    anchors = generate_anchors(anchor_sizes, anchor_ratios, anchor_angles,
                        height=H // stride,
                        width=W // stride,
                        stride=stride)

    iou_matrix = rotate_iou(FCT(gt_anchors), FCT(anchors)).cpu().numpy()
    # sorted_matrix = np.argsort(iou_matrix, axis=1)[:, ::-1]

    fg_iou_thresh = cfg.RPN.FG_IOU_THRESHOLD
    nms_thresh = cfg.RPN.NMS_THRESH

    a,b = np.nonzero(iou_matrix > fg_iou_thresh)
    # gg = iou_matrix > fg_iou_thresh
    # b = np.unique([ix for g in gg for ix,b in enumerate(g) if b!=0])
    # best_anchors = anchors[sorted_matrix[:,0]]
    best_anchors = anchors[b]#[(iou_matrix > 0.8]
    # best_anchors = best_anchors[nms_rotate_cpu(best_anchors, nms_thresh, 1000)]

    img_best_anchors = draw_anchors(image, best_anchors)
    cv2.imshow("img", img)
    cv2.imshow("best_anchors", img_best_anchors)

    batch = total_anchors
    start = 0 # (len(anchors) // total_anchors // 2)
    for ix in np.arange(start, len(anchors), batch):
        stride_anchors = anchors[ix:ix+batch]
        img_stride_anchors = draw_anchors(image, stride_anchors)
        valid_idx = b[np.logical_and(ix <= b, b < ix + batch)]

        # print("Valids: %d"%(len(valid_idx)))
        if len(valid_idx) == 0:
            continue

        valid_anchors = anchors[valid_idx]
        img_valid_stride_anchors = draw_anchors(image, valid_anchors)

        post_nms_anchors = valid_anchors[nms_rotate_cpu(valid_anchors, nms_thresh, 100)]
        img_valid_stride_anchors_post_nms = draw_anchors(image, post_nms_anchors)
        # post_nms_iou_matrix = iou_rotate_cpu(gt_anchors, post_nms_anchors)
        # print(post_nms_iou_matrix)

        cv2.imshow("stride_anchors", img_stride_anchors)
        cv2.imshow("valid_stride_anchors (>%.2f)"%(fg_iou_thresh), img_valid_stride_anchors)
        cv2.imshow("valid_stride_anchors (post NMS)", img_valid_stride_anchors_post_nms)
        cv2.waitKey(0)
示例#2
0
def vis_scaled_rois(rois_in, scale=100):
    # rr = rois[-1,1:]
    rois = rois_in.copy()
    rois[:, :-1] *= scale
    img = np.zeros((H*scale,W*scale,3), dtype=np.uint8)
    img[::scale] = WHITE
    img[:, ::scale] = WHITE
    img = draw_anchors(img, rois, [RED])
    cv2.imshow("img", img)
    cv2.waitKey(0)
def vis_scaled_rois(rois_in, pool_dims, spatial_scale, scale=100):
    # rr = rois[-1,1:]
    N = len(rois_in)
    PH, PW = pool_dims

    # rois = rois_in.copy()
    # rois[:, :-1] *= scale
    pooling_pts = get_rotated_roi_pooling_pts(rois_in, pool_dims, spatial_scale=spatial_scale)
    pooling_pts *= scale
    pooling_pts_flat = pooling_pts.reshape((N*PH*PW, 8))
    img = np.zeros((H*scale,W*scale,3), dtype=np.uint8)
    img[::scale] = WHITE
    img[:, ::scale] = WHITE
    img = draw_anchors(img, pooling_pts_flat, [RED])

    mean_x = np.mean(pooling_pts_flat[:, ::2], axis=-1)
    mean_y = np.mean(pooling_pts_flat[:, 1::2], axis=-1)
    mid_pts = np.vstack((mean_x, mean_y)).T
    mid_pts = np.round(mid_pts).astype(np.int32)

    sampling_ratio = 0

    img2 = img.copy()
    for n in range(N):
        mpts = mid_pts[n*4:(n+1)*4]  # 4 pts [x,y,...]
        roi = rois_in[n][1:]  # xc yc w h angle
        roi_w, roi_h = roi[2:4] * spatial_scale

        for ix,mpt in enumerate(mpts):
            mpt = tuple(mpt)
            cv2.circle(img, mpt, 2, GREEN)
            cv2.putText(img, "%d"%(ix), tuple(mpt), cv2.FONT_HERSHEY_SIMPLEX, 0.5, BLUE)

        for ph in range(PH):
            for pw in range(PW):
                P = pooling_pts[n,ph,pw]
                line_params = get_pts_line_params(P)
                roi_bin_grid_h = int(np.ceil(roi_h / PH)) if sampling_ratio <= 0 else sampling_ratio
                roi_bin_grid_w = int(np.ceil(roi_w / PW)) if sampling_ratio <= 0 else sampling_ratio

                mw = 1.0 / roi_bin_grid_w
                mh = 1.0 / roi_bin_grid_h
                for iy in range(roi_bin_grid_h):
                    for ix in range(roi_bin_grid_w):
                        x = P[0] + (iy + 0.5) * line_params[0] * mh + (ix + 0.5) * line_params[2] * mw
                        y = P[1] + (iy + 0.5) * line_params[1] * mh + (ix + 0.5) * line_params[3] * mw
                        cv2.circle(img2, (int(round(x)), int(round(y))), 3, GREEN, -1)

    cv2.imshow("img", img)
    cv2.imshow("img2", img2)
    cv2.waitKey(0)
示例#4
0
    def next_batch(self, batch_sz):
        blank_img = np.zeros((self.img_size, self.img_size, 3), dtype=np.uint8)
        H, W = blank_img.shape[:2]

        image_list = []  # [(H*W*3 nd.array), ...]
        all_rects_list = [] # [[(xc,yc,w,h,theta), ...], ...]

        for i in range(batch_sz):
            num_objects = npr.randint(self.min_objects, self.max_objects + 1)
            # img = blank_img.copy()

            rect_list = []
            color_list = []
            for n in range(num_objects):
                w = npr.randint(self.min_width, self.max_width)
                # h = w // (np.random.randint(15,30) / 10.)
                max_h = min(self.max_area // w, self.max_height)
                h = npr.randint(self.min_height, max_h) if max_h > self.min_height else max_h
                h = max(self.min_area // w, h)
                theta = npr.randint(-90, 90)
                # theta = np.random.permutation([-30,0,30])[0] #npr.randint(-90, 90)   # degrees
                rect = np.array([0, 0, w, h, theta], dtype=np.float32)  # center at 0,0

                rect_pts = convert_rect_to_pts(rect)  # (4,2) 4 corners (x,y)
                rect_bb_lt = np.min(rect_pts,axis=0)  # left top point of rect's bounding box. Will be negative since rect is centered at 0,0
                rect_bb_rb = -rect_bb_lt  # right bottom is negative of left top, since rect is symmetric and centered at 0
                x_center, y_center = -rect_bb_lt + 1

                # shift the rect (by some random amount) so that all the rect points are never out of bounds
                x_center = npr.randint(x_center, W - x_center)
                y_center = npr.randint(y_center, H - y_center)
                rect = np.array([x_center,y_center,w,h,theta], dtype=np.float32)

                # # DEBUG ONLY
                # rect = np.array([W // 2, H // 2, 80, 50, theta], dtype=np.float32)
                # color = [0, 255, 0]
                color = get_random_color()
                if self.is_training:
                    # if training, make the rect outputs consistent via minAreaRect
                    rect = np.array(convert_pts_to_rect(convert_rect_to_pts(rect)), dtype=np.float32)

                color_list.append(color)
                rect_list.append(rect)

            img = draw_anchors(blank_img, rect_list, color_list=color_list, fill=self.fill)
            image_list.append(img)

            all_rects_list.append(np.array(rect_list))
            
        return image_list, all_rects_list
def visualize_box_preds(img, box_preds, min_score=0.9, color=RED):
    score = box_preds[:, -1]
    box_pred = box_preds[:, :-1]

    valids = score > min_score
    if np.sum(valids) > 0:
        score = score[valids]
        valid_box_pred = box_pred[valids]

        sorted_idx = np.argsort(score)[::-1]
        # sorted_score = score[sorted_idx]
        sorted_pred = valid_box_pred[sorted_idx]

        # H,W = img.shape[:2]
        # canvas = np.zeros(img.shape, dtype=np.uint8)
        canvas = draw_anchors(img, sorted_pred, color)
    else:
        canvas = img.copy()

    return canvas
    print("GPU keep: ", keep)
    # keep = keep2

    s_keep = standard_nms_cpu(bounding_boxes, iou_thresh)

    # import os
    # os.environ["CUDA_VISIBLE_DEVICES"] = '0'
    # keep = nms_rotate(tf.convert_to_tensor(boxes, dtype=tf.float32), tf.convert_to_tensor(scores, dtype=tf.float32),
    #                   iou_thresh, 5)
    # with tf.Session() as sess:
    #     keep = sess.run(keep)

    out_boxes = boxes[keep]

    img = np.zeros((400, 400, 3), dtype=np.uint8)
    img1 = draw_anchors(img, boxes, RED)
    img2 = draw_anchors(img, out_boxes, GREEN)

    img3 = draw_bounding_boxes(img1, bounding_boxes, BLUE)
    img4 = draw_anchors(img, boxes[s_keep], GREEN)
    cv2.imshow("pre NMS", img3)
    cv2.imshow("post NMS", img4)
    cv2.imshow("pre rotate NMS", img1)
    cv2.imshow("post rotate NMS", img2)

    cv2.waitKey(0)

    # # =============================IOU ROTATE TEST===================================== #
    #
    # def iou_rotate_torch(boxes1, boxes2, use_gpu=False):
    #
示例#7
0
    VIS_SCALE = 4
    for r_pairs in rect_pairs:

        r1 = r_pairs[0].copy()  # target
        r2 = r_pairs[1].copy()  # anchor

        w1, h1, angle1 = r1[2:]
        w2, h2, angle2 = r2[2:]

        r3 = r1.copy()
        if np.abs(angle1 - angle2) > 45:
            r3[-1] -= np.sign(angle1 - angle2) * 90
            r3[2:4] = r1[2:4][::-1].copy()
        print(r3, r1)

        # rr1 = convert_pts_to_rect(convert_rect_to_pts(r1), make_width_larger=False)
        # rr2 = convert_pts_to_rect(convert_rect_to_pts(r2), make_width_larger=False)
        # print(rr1, rr2)

        r1[:-1] *= VIS_SCALE
        r2[:-1] *= VIS_SCALE
        r3[:-1] *= VIS_SCALE

        img = np.zeros((H * VIS_SCALE, W * VIS_SCALE, 3), dtype=np.uint8)
        img = draw_anchors(img, [r1], [RED])
        img = draw_anchors(img, [r2], [BLUE])
        img = draw_anchors(img, [r3], [GREEN])
        cv2.imshow("img", img)
        cv2.waitKey(0)
示例#8
0
    iou_matrix = iou_rotate_cpu(gt_anchors, anchors)
    fg_iou_thresh = cfg.RPN.FG_IOU_THRESHOLD
    nms_thresh = cfg.RPN.NMS_THRESH

    gt_ids, a_ids = np.nonzero(iou_matrix > fg_iou_thresh)

    for gt_id, a_id in zip(gt_ids, a_ids):
        gt = gt_anchors[gt_id]
        a = anchors[a_id]
        iou = iou_matrix[gt_id, a_id]
        angle_diff = gt[-1] - a[-1]
        print(gt.astype(np.int32), a.astype(np.int32), angle_diff)
        print(iou)
        # if np.abs(angle_diff) >= 90:
        #     angle_diff = 0
        out = draw_anchors(img, [gt, a], [RED, BLUE])

        gt_mid_pt = tuple(gt[:2])

        txt_color = RED
        if np.abs(angle_diff) >= 45:
            txt_color = BLUE
        out = cv2.putText(out, "%d" % (angle_diff), gt_mid_pt,
                          cv2.FONT_HERSHEY_SIMPLEX, 0.5, txt_color)

        a2 = a.copy()
        a2[2:4] = gt[2:4]
        a2[-1] += angle_diff
        cv2.imshow("adjusted", draw_anchors(img, [a2], [BLUE]))
        cv2.imshow("out", out)
        cv2.waitKey(0)