Ejemplo n.º 1
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
    def __call__(self, data, label, gt, seg_gt, seg_gt_to_compare):
        #-----------------------------------------------------------------------
        # Initialize the data vector and other variables
        #-----------------------------------------------------------------------
        if not self.initialized:
            self.initialize()

        vec = np.zeros((self.vheight, self.vwidth), dtype=np.float32)

        #-----------------------------------------------------------------------
        # For every box compute the best match and all the matches above 0.5
        # Jaccard overlap
        #-----------------------------------------------------------------------
        overlaps = {}
        for box in gt.boxes:
            box_arr = box2array(box, self.img_size)
            overlaps[box] = compute_overlap(box_arr, self.anchors_arr, 0.5)

        #-----------------------------------------------------------------------
        # Set up the training vector resolving conflicts in favor of a better
        # match
        #-----------------------------------------------------------------------
        vec[:, self.num_classes] = 1  # background class
        vec[:, self.num_classes + 1] = 0  # x offset
        vec[:, self.num_classes + 2] = 0  # y offset
        vec[:, self.num_classes + 3] = 0  # log width scale
        vec[:, self.num_classes + 4] = 0  # log height scale

        matches = {}
        for box in gt.boxes:
            for overlap in overlaps[box].good:
                anchor = self.anchors[overlap.idx]

                process_overlap(overlap, box, anchor, matches,
                                self.num_classes, vec)

        matches = {}
        for box in gt.boxes:
            overlap = overlaps[box].best
            if not overlap:
                continue
            anchor = self.anchors[overlap.idx]
            process_overlap(overlap, box, anchor, matches, self.num_classes,
                            vec)

        return data, vec, gt, seg_gt, seg_gt_to_compare
Ejemplo n.º 3
0
    def __call__(self, data, label, gt):
        if not self.initialized:
            self.initialize()
        vec = np.zeros((self.vheight, self.vwidth), dtype=np.float32)
        overlaps = {}
        for box in gt.boxes:
            # box = 0~1
            box_arr = box2array(
                box, self.img_size
            )  # Convert proportional center-width bounds to absolute min-max bounds
            # box_arr = (0~300, 0~300)
            # gt_box xmin, xmax, ymin, ymax
            overlaps[box] = compute_overlap(box_arr, self.anchors_arr, 0.4)

        vec[:, self.num_classes] = 1.  # background
        vec[:, self.num_classes + 1] = 0.  # x offset
        vec[:, self.num_classes + 2] = 0.  # y offset
        vec[:, self.num_classes + 3] = 0.  # log width scale
        vec[:, self.num_classes + 4] = 0.  # log height scale

        matches = {}
        for box in gt.boxes:
            for overlap in overlaps[box].good:
                anchor = self.anchors[overlap.idx]
                process_overlap(overlap, box, anchor, matches,
                                self.num_classes, vec)

        matches = {}
        for box in gt.boxes:
            overlap = overlaps[box].best
            if not overlap:
                continue
            anchor = self.anchors[overlap.idx]
            process_overlap(overlap, box, anchor, matches, self.num_classes,
                            vec)

        return data, vec, gt.filename
Ejemplo n.º 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