示例#1
0
def iou_rotated_single_vs_multi_boxes_cpu(single_box, multi_boxes):
    """
    :param pred_box: Numpy array
    :param target_boxes: Numpy array
    :return:
    """
    #print('single {}'.format(single_box.shape))

    #print('multi_boxes {}'.format(multi_boxes.shape))

    s_x, s_y, s_w, s_l, s_yaw = single_box
    #print(single_box)
    s_area = s_w * s_l
    s_conners = get_corners(s_x, s_y, s_w, s_l, s_yaw)
    s_polygon = cvt_box_2_polygon(s_conners)
    m_boxes_conners = []
    targets_areas = []
    for multi_boxe in multi_boxes:
        m_x, m_y, m_w, m_l, m_yaw = multi_boxe
        #print(multi_boxe)
        targets_areas.append(m_w * m_l)
        m_boxes_conners.append(get_corners(m_x, m_y, m_w, m_l, m_yaw))

    m_boxes_polygons = [cvt_box_2_polygon(box_) for box_ in m_boxes_conners]

    ious = []
    for m_idx in range(multi_boxes.shape[0]):
        intersection = s_polygon.intersection(m_boxes_polygons[m_idx]).area
        # print(intersection)
        iou_ = intersection / (s_area + targets_areas[m_idx] - intersection + 1e-16)
        ious.append(iou_)

    return torch.tensor(ious, dtype=torch.float)
def iou_rotated_11_boxes(box1, box2):
    x, y, w, l, im, re = box1
    yaw = np.arctan2(im, re)
    bbox1 = bev_utils.get_corners(x, y, w, l, yaw)
    # use .buffer(0) to fix a line polygon
    # more infor: https://stackoverflow.com/questions/13062334/polygon-intersection-error-in-shapely-shapely-geos-topologicalerror-the-opera
    box1_polygon = Polygon([(bbox1[i, 0], bbox1[i, 1]) for i in range(len(bbox1))]).buffer(0)

    x, y, w, l, im, re = box2
    yaw = np.arctan2(im, re)
    bbox2 = bev_utils.get_corners(x, y, w, l, yaw)
    box2_polygon = Polygon([(bbox2[i, 0], bbox2[i, 1]) for i in range(len(bbox2))]).buffer(0)  # to fix a line polygon
    iou = box1_polygon.intersection(box2_polygon).area / (box1_polygon.union(box2_polygon).area + 1e-12)

    return iou
示例#3
0
    def __init__(self, dataset_dir, img_size, use_yaw_label=False):
        self.dataset_dir = dataset_dir
        self.img_size = img_size
        self.use_yaw_label = use_yaw_label

        self.lidar_dir = os.path.join(self.dataset_dir, 'training', "velodyne")
        self.image_dir = os.path.join(self.dataset_dir, 'training', "image_2")
        self.calib_dir = os.path.join(self.dataset_dir, 'training', "calib")
        self.label_dir = os.path.join(self.dataset_dir, 'training', "label_2")
        split_txt_path = os.path.join(self.dataset_dir, 'ImageSets',
                                      'trainval.txt')
        self.image_idx_list = [
            x.strip() for x in open(split_txt_path).readlines()
        ]

        self.sample_id_list = self.remove_invalid_idx(self.image_idx_list)
        self.boxes_wh = self.load_full_boxes_wh()
        # Take out the total number of boxes
        self.num_boxes = self.boxes_wh.shape[0]
        print("number of sample_id_list: {}, num_boxes: {}".format(
            len(self.sample_id_list), self.num_boxes))
        # Calculate the polygons and areas
        self.boxes_conners = np.array([
            kitti_bev_utils.get_corners(0, 0, b[0], b[1], b[2])
            for b in self.boxes_wh
        ])
        self.boxes_polygons = [
            self.cvt_box_2_polygon(b) for b in self.boxes_conners
        ]
        self.boxes_areas = [b.area for b in self.boxes_polygons]
        print("Done calculate boxes infor")
def iou_rotated_single_vs_multi_boxes_cpu(single_box, multi_boxes):
    """
    :param pred_box: Numpy array
    :param target_boxes: Numpy array
    :return:
    """

    s_x, s_y, s_w, s_l, s_im, s_re = single_box
    s_area = s_w * s_l
    s_yaw = np.arctan2(s_im, s_re)
    s_conners = bev_utils.get_corners(s_x, s_y, s_w, s_l, s_yaw)
    s_polygon = cvt_box_2_polygon(s_conners)

    m_x, m_y, m_w, m_l, m_im, m_re = multi_boxes.transpose(1, 0)
    targets_areas = m_w * m_l
    m_yaw = np.arctan2(m_im, m_re)
    m_boxes_conners = get_corners_vectorize(m_x, m_y, m_w, m_l, m_yaw)
    m_boxes_polygons = [cvt_box_2_polygon(box_) for box_ in m_boxes_conners]

    ious = []
    for m_idx in range(multi_boxes.shape[0]):
        intersection = s_polygon.intersection(m_boxes_polygons[m_idx]).area
        iou_ = intersection / (s_area + targets_areas[m_idx] - intersection + 1e-16)
        ious.append(iou_)

    return torch.tensor(ious, dtype=torch.float)
示例#5
0
def rotated_bbox_iou_polygon(box1, box2):
    box1 = to_cpu(box1).numpy()
    box2 = to_cpu(box2).numpy()

    x, y, w, l, im, re = box1
    angle = np.arctan2(im, re)
    bbox1 = np.array(bev_utils.get_corners(x, y, w, l,
                                           angle)).reshape(-1, 4, 2)
    bbox1 = convert_format(bbox1)

    bbox2 = []
    for i in range(box2.shape[0]):
        x, y, w, l, im, re = box2[i, :]
        angle = np.arctan2(im, re)
        bev_corners = bev_utils.get_corners(x, y, w, l, angle)
        bbox2.append(bev_corners)
    bbox2 = convert_format(np.array(bbox2))

    return compute_iou(bbox1[0], bbox2)
def rotated_bbox_iou_polygon_cpu(box1, box2):
    """
    :param box1: Numpy array
    :param box2: Numpy array
    :return:
    """

    x, y, w, l, im, re = box1
    angle = np.arctan2(im, re)
    bbox1 = np.array(bev_utils.get_corners(x, y, w, l,
                                           angle)).reshape(-1, 4, 2)
    bbox1 = cvt_box_2_polygon(bbox1)

    bbox2 = []
    for i in range(box2.shape[0]):
        x, y, w, l, im, re = box2[i, :]
        angle = np.arctan2(im, re)
        bev_corners = bev_utils.get_corners(x, y, w, l, angle)
        bbox2.append(bev_corners)
    bbox2 = cvt_box_2_polygon(np.array(bbox2))

    return compute_iou_polygons(bbox1[0], bbox2)
def compute_polygons(boxes):
    """

    :param boxes: [num, 6]
    :return:
    """
    polygons = []
    for (x, y, w, l, im, re) in boxes:
        angle = np.arctan2(im, re)
        bev_corners = bev_utils.get_corners(x, y, w, l, angle)
        polygons.append(bev_corners)
    polygons = cvt_box_2_polygon(np.array(polygons))

    return polygons
示例#8
0
    def kmeans(self, num_anchors):
        # The position of each point in each box
        distance = np.empty((self.num_boxes, num_anchors))

        # Last cluster position
        last_clu = np.zeros((self.num_boxes, ))

        np.random.seed(0)

        # Randomly select k cluster centers
        self.cluster = self.boxes_wh[np.random.choice(self.num_boxes,
                                                      num_anchors,
                                                      replace=False)]
        self.cluster[:, 2] = 0  # Choose yaw = 0

        # cluster = random.sample(self.num_boxes, k)
        self.loop_cnt = 0
        while True:
            self.loop_cnt += 1

            print('\nThe new cluster of count {} is below:\n'.format(
                self.loop_cnt))
            for clus_ in self.cluster:
                w_, h_, yaw_ = clus_
                print('[{}, {}, {:.0f}],'.format(int(w_), int(h_), yaw_))

            self.cluster_conners = np.array([
                kitti_bev_utils.get_corners(0, 0, clus[0], clus[1], clus[2])
                for clus in self.cluster
            ])
            self.cluster_polygons = [
                self.cvt_box_2_polygon(clus) for clus in self.cluster_conners
            ]
            self.cluster_areas = [clus.area for clus in self.cluster_polygons]
            # Calculate the iou situation at five points from each line.
            for i in range(self.num_boxes):
                distance[i] = 1 - self.compute_iou(i)

            # Take out the smallest point
            near = np.argmin(distance, axis=1)

            if (last_clu == near).all():
                break

            # Find the median of each class
            for j in range(num_anchors):
                self.cluster[j] = np.median(self.boxes_wh[near == j], axis=0)
            self.cluster[:, 2] = 0  # Choose yaw = 0

            last_clu = near
def get_box_heatmap(hm_main_center, center_x, center_y, bbox_l, bbox_w, yaw,
                    center_int):
    from data_process.kitti_bev_utils import get_corners
    heatmap = np.zeros((112, 112), dtype=np.uint8)
    corners = get_corners(center_x, center_y, bbox_l, bbox_w, yaw)
    corners_int = corners.reshape(-1, 1, 2).astype(int)
    cv2.drawContours(heatmap, [corners_int], -1, 255, thickness=-1)
    for x in range(112):
        for y in range(112):
            if heatmap[y, x] == 255:
                pix = np.array([x, y]).astype(np.int32)
                dist = np.sqrt(((pix - center_int) * (pix - center_int)).sum())
                if dist == 1:
                    hm_main_center[y, x] = max(0.8, hm_main_center[y, x])
                elif dist > 0:
                    hm_main_center[y, x] = max(1 / dist, hm_main_center[y, x])
示例#10
0
def rotated_bbox_iou_polygon_vectorize(box1, box2):
    """ calculate IoU of polygons with vectorization

    :param box1: (6,)
    :param box2: (num, 6)
    :return:
    """
    box1 = to_cpu(box1).numpy()
    box2 = to_cpu(box2).numpy()

    x, y, w, l, im, re = box1
    angle = np.arctan2(im, re)
    bbox1 = np.array(bev_utils.get_corners(x, y, w, l,
                                           angle)).reshape(-1, 4, 2)
    bbox1 = convert_format(bbox1)

    bbox2 = bev_utils.get_corners_vectorize(box2)
    bbox2 = convert_format(bbox2)

    return compute_iou(bbox1[0], bbox2)