コード例 #1
0
 def initialize(self):
     self.anchors = get_anchors_for_preset(self.preset)
     self.vheight = len(self.anchors)
     self.vwidth = self.num_classes + 5  # background class + location offsets
     self.img_size = Size(1000, 1000)
     self.anchors_arr = anchors2array(self.anchors, self.img_size)
     self.initialized = True
コード例 #2
0
    def __call__(self, data, label, gt):
        #-----------------------------------------------------------------------
        # Check whether to sample or not
        #-----------------------------------------------------------------------
        if not self.sample:
            return data, label, gt

        #-----------------------------------------------------------------------
        # Retry sampling a couple of times
        #-----------------------------------------------------------------------
        source_boxes = anchors2array(gt.boxes, gt.imgsize)
        box = None
        box_arr = None
        for _ in range(self.max_trials):
            #-------------------------------------------------------------------
            # Sample a bounding box
            #-------------------------------------------------------------------
            scale = random.uniform(self.min_scale, self.max_scale)
            aspect_ratio = random.uniform(self.min_aspect_ratio,
                                          self.max_aspect_ratio)

            # make sure width and height will not be larger than 1
            aspect_ratio = max(aspect_ratio, scale**2)
            aspect_ratio = min(aspect_ratio, 1 / (scale**2))

            width = scale * sqrt(aspect_ratio)
            height = scale / sqrt(aspect_ratio)
            cx = 0.5 * width + random.uniform(0, 1 - width)
            cy = 0.5 * height + random.uniform(0, 1 - height)
            center = Point(cx, cy)
            size = Size(width, height)

            #-------------------------------------------------------------------
            # Check if the box satisfies the jaccard overlap constraint
            #-------------------------------------------------------------------
            box_arr = np.array(prop2abs(center, size, gt.imgsize))
            overlap = compute_overlap(box_arr, source_boxes, 0)
            if overlap.best and overlap.best.score >= self.min_jaccard_overlap:
                box = Box(None, None, center, size)
                break

        if box is None:
            return None

        #-----------------------------------------------------------------------
        # Crop the box and adjust the ground truth
        #-----------------------------------------------------------------------
        new_size = Size(box_arr[1] - box_arr[0], box_arr[3] - box_arr[2])
        w_off = -box_arr[0]
        h_off = -box_arr[2]
        data = data[box_arr[2]:box_arr[3], box_arr[0]:box_arr[1]]
        gt = transform_gt(gt, new_size, h_off, w_off)

        return data, label, gt
コード例 #3
0
    def __call__(self, data, label, gt):
        if not self.initialized:
            self.initialize()

        box_cordinate = list()
        gt_label = list()
        for boxes in gt.boxes:
            box = Anchor(Point(boxes[2][0], boxes[2][1]),
                         Size(boxes[3][0], boxes[3][1]))
            box_cordinate.append(box)
            gt_label.append(boxes[1])
        box_arr = anchors2array(box_cordinate,
                                self.img_size)  # gt h,w over 300?
        vec = np.zeros((100 - len(box_arr), 4), dtype=np.double)
        label = np.zeros(100 - len(box_arr), dtype=np.int64)
        vec = np.r_[box_arr, vec]
        label = np.r_[gt_label, label]

        return data, gt.filename, label, tuple(gt.imgsize), vec  #, gt[1]
コード例 #4
0
    def __call__(self, data, label, gt):
        #-----------------------------------------------------------------------
        # Check whether to sample or not
        #-----------------------------------------------------------------------
        if not self.sample:
            return data, label, gt

        #-----------------------------------------------------------------------
        # Retry sampling a couple of times
        #-----------------------------------------------------------------------
        source_boxes = anchors2array(
            gt.boxes, gt.imgsize)  # get abs value(xmin,xmax,ymin,ymax)
        box = None
        box_arr = None
        for _ in range(self.max_trials):
            #-------------------------------------------------------------------
            # Sample a bounding box
            #-------------------------------------------------------------------
            # min_scale=0.3, max_scale=1.0,
            # min_aspect_ratio=0.5, max_aspect_ratio=2.0,
            scale = random.uniform(self.min_scale, self.max_scale)
            aspect_ratio = random.uniform(self.min_aspect_ratio,
                                          self.max_aspect_ratio)

            # make sure width and height will not be larger than 1
            aspect_ratio = max(aspect_ratio, scale**2)
            aspect_ratio = min(aspect_ratio, 1 / (scale**2))

            width = scale * sqrt(aspect_ratio)
            height = scale / sqrt(aspect_ratio)
            cx = 0.5 * width + random.uniform(0, 1 - width)
            cy = 0.5 * height + random.uniform(0, 1 - height)
            center = Point(cx, cy)
            size = Size(width, height)

            #-------------------------------------------------------------------
            # Check if the box satisfies the jaccard overlap constraint
            #-------------------------------------------------------------------
            box_arr = np.array(prop2abs(center, size, gt.imgsize))
            overlap = compute_overlap(box_arr, source_boxes, 0)
            if overlap.best and overlap.best.score >= self.min_jaccard_overlap:
                box = Box(None, None, center, size)
                break

        if box is None:
            return None

        #-----------------------------------------------------------------------
        # Crop the box and adjust the ground truth
        #-----------------------------------------------------------------------
        new_size = Size(box_arr[1] - box_arr[0], box_arr[3] - box_arr[2])
        w_off = -box_arr[0]
        h_off = -box_arr[2]

        data = data.crop(
            (box_arr[0], box_arr[2], box_arr[1], box_arr[3])
        )  # gt와 gt근처의 가상의 anchor를 잡은뒤, iou 0.1, 0.3, 0.5, 0.7, 0.9 이상이면 그 가상의 anchor부분을 crop하고 gt 좌표도 같이 변경
        # 즉 일부로 gt와 iou 연관있는 그림부분을 crop함
        gt = transform_gt(gt, new_size, h_off, w_off)

        return data, label, gt  # change gt and image_size