Ejemplo n.º 1
0
    def get_transform(self, image):
        h, w = image.shape[:2]
        if self.is_range:
            size = np.random.randint(self.short_edge_length[0],
                                     self.short_edge_length[1] + 1)
        else:
            size = np.random.choice(self.short_edge_length)
        if size == 0:
            return NoOpTransform()

        scale = size * 1.0 / min(h, w)
        if h < w:  # scale = size / h
            # h 设置成短边,大边要进行相应的缩放
            newh, neww = size, scale * w
        else:
            # 否则w是小边
            newh, neww = scale * h, size
        #here, 超过最大,还得缩回来
        if max(newh, neww) > self.max_size:
            scale = self.max_size * 1.0 / max(newh, neww)
            newh = newh * scale
            neww = neww * scale
        neww = int(neww + 0.5)  # 为什么+0.5呢
        newh = int(newh + 0.5)
        return ResizeTransform(h, w, newh, neww, self.interp)
Ejemplo n.º 2
0
Archivo: crop.py Proyecto: zivzone/d2go
    def get_transform(self, image, annotations):
        """
        This function will modify instances to set the iscrowd flag to 1 for
        annotations not picked. It relies on the dataset mapper to filter those
        items out
        """
        assert isinstance(annotations, (list, tuple)), annotations
        assert all("bbox" in x for x in annotations), annotations
        assert all("bbox_mode" in x for x in annotations), annotations

        image_size = image.shape[:2]

        # filter out iscrowd
        annotations = [x for x in annotations if x.get("iscrowd", 0) == 0]
        if len(annotations) == 0:
            return NoOpTransform()

        sel_index = np.random.randint(len(annotations))
        # set iscrowd flag of other annotations to 1 so that they will be
        #   filtered out by the datset mapper (https://fburl.com/diffusion/fg64cb4h)
        for idx, instance in enumerate(annotations):
            if idx != sel_index:
                instance["iscrowd"] = 1
        instance = annotations[sel_index]

        bbox_xywh = BoxMode.convert(instance["bbox"], instance["bbox_mode"],
                                    BoxMode.XYWH_ABS)

        scale = np.random.uniform(*self.crop_scale)
        bbox_xywh = bu.scale_bbox_center(bbox_xywh, scale)
        bbox_xywh = bu.clip_box_xywh(bbox_xywh, image_size).int()

        return CropTransform(*bbox_xywh.tolist())
Ejemplo n.º 3
0
 def get_transform(self, img):
     h, w = img.shape[:2]
     do = self._rand_range() < self.prob
     if do:
         return Rotated180Transform(h, w)
     else:
         return NoOpTransform()
Ejemplo n.º 4
0
    def get_transform(self, img):
        h, w = img.shape[:2]

        if self.is_range:
            size = np.random.randint(self.short_edge_length[0], self.short_edge_length[1] + 1)
        else:
            size = np.random.choice(self.short_edge_length)
        if size == 0:
            return NoOpTransform()

        scale = size * 1.0 / min(h, w)
        if h < w:
            newh, neww = size, scale * w
        else:
            newh, neww = scale * h, size
        if max(newh, neww) > self.max_size:
            scale = self.max_size * 1.0 / max(newh, neww)
            newh = newh * scale
            neww = neww * scale
        neww = int(neww + 0.5)
        newh = int(newh + 0.5)
        #print('img shape:', (h, w))
        #print('new img shape:', (newh, neww))

        return ResizeTransform(h, w, newh, neww, self.interp)
Ejemplo n.º 5
0
    def get_transform(self, img):
        h, w = img.shape[:2]
        center = None
        if self.is_range:
            angle = np.random.uniform(self.angle[0], self.angle[1])
            if self.center is not None:
                center = (
                    np.random.uniform(self.center[0][0], self.center[1][0]),
                    np.random.uniform(self.center[0][1], self.center[1][1]),
                )
        else:
            angle = np.random.choice(self.angle)
            if self.center is not None:
                center = np.random.choice(self.center)

        if center is not None:
            center = (w * center[0], h * center[1]
                      )  # Convert to absolute coordinates

        if angle % 360 == 0:
            return NoOpTransform()

        return RotationTransform(h,
                                 w,
                                 angle,
                                 expand=self.expand,
                                 center=center,
                                 interp=self.interp)
Ejemplo n.º 6
0
 def get_transform(self, img):
     _, w = img.shape[:2]
     do = self._rand_range() < self.prob
     if do:
         return HFlipTransform(w)
     else:
         return NoOpTransform()
Ejemplo n.º 7
0
    def get_transform(self, image: np.ndarray, sem_seg: np.ndarray) -> Transform:
        # HWC or HW for image, HW for sem_seg
        assert len(image.shape) in [2, 3]
        assert len(sem_seg.shape) == 2

        mask_box_xywh = bu.get_box_from_mask(sem_seg)
        # do nothing if the mask is empty (the whole image is background)
        if mask_box_xywh is None:
            return NoOpTransform()

        crop_ar = self._pick_aspect_ratio()
        target_scale = self._pick_scale()
        target_offset = self._pick_offset(*mask_box_xywh[2:])

        mask_box_xywh = bu.offset_bbox(mask_box_xywh, target_offset)
        mask_box_xywh = bu.scale_bbox_center(mask_box_xywh, target_scale)

        target_box_xywh = bu.get_min_box_aspect_ratio(mask_box_xywh, crop_ar)
        target_bbox_xyxy = bu.get_bbox_xyxy_from_xywh(target_box_xywh)

        return ExtentTransform(
            src_rect=target_bbox_xyxy,
            output_size=(
                int(target_box_xywh[3].item()),
                int(target_box_xywh[2].item()),
            ),
        )
Ejemplo n.º 8
0
    def get_transform(self, img):
        h, w = img.shape[:2]
        if self.max_height == 0:
            return NoOpTransform()

        scale = min(self.max_height * 1.0 / h, self.max_width * 1.0 / w)
        newh = int(h * scale + 0.5)
        neww = int(w * scale + 0.5)
        return ResizeTransform(h, w, newh, neww, self.interp)
Ejemplo n.º 9
0
 def get_transform(self, img):
     do = self._rand_range() < self.prob
     if do:
         if isinstance(self.transform, Augmentation):
             return self.transform.get_transform(img)
         else:
             return self.transform
     else:
         return NoOpTransform()
 def get_transform(self, img):
     _, w = img.shape[:2]
     do_horiz = self._rand_range() < self.prob/2
     do_vert = self._rand_range() >= self.prob/2 and self._rand_range() < self.prob
     if do_horiz:
         return HFlipTransform(w)
     elif do_vert:
         return VFlipTransform(w)
     else:
         return NoOpTransform()
Ejemplo n.º 11
0
 def get_transform(self, *args):
     do = self._rand_range() < self.prob
     if do:
         shift_x = np.random.randint(low=-self.max_shifts,
                                     high=self.max_shifts)
         shift_y = np.random.randint(low=-self.max_shifts,
                                     high=self.max_shifts)
         return YOLOFShiftTransform(shift_x, shift_y)
     else:
         return NoOpTransform()
Ejemplo n.º 12
0
 def get_transform(self, img):
     h, w = img.shape[:2]
     do = self._rand_range() < self.prob
     if do:
         if self.horizontal:
             return HFlipTransform(w)
         elif self.vertical:
             return VFlipTransform(h)
     else:
         return NoOpTransform()
    def get_transform(self, img):
        h, w = img.shape[:2]
        if min(h, w) >= self.short_edge_length[0]:
            return NoOpTransform()

        scale = self.short_edge_length[0] * 1.0 / min(h, w)
        newh = h * scale
        neww = w * scale
        neww = int(neww + 0.5)
        newh = int(newh + 0.5)
        return ResizeTransform(h, w, newh, neww, self.interp)
Ejemplo n.º 14
0
 def get_transform(self, img):
     h, w = img.shape[:2]
     # do = self._rand_range() < self.prob # original implementation
     do = False # No flip!
     if do:
         if self.horizontal:
             return HFlipTransform(w)
         elif self.vertical:
             return VFlipTransform(h)
     else:
         return NoOpTransform()
Ejemplo n.º 15
0
 def get_transform(self, img):
     do = self._rand_range() < self.prob
     if do:
         h, w, _ = img.shape
         ratio = np.random.uniform(*self.ratio_range)
         top = int(np.random.uniform(0, h * ratio - h))
         left = int(np.random.uniform(0, w * ratio - w))
         return ExpandTransform(ratio, top, left, self.img_value,
                                self.seg_value)
     else:
         return NoOpTransform()
Ejemplo n.º 16
0
    def get_transform(self, img, boxes):
        """
        Args:
            img (ndarray): of shape HxWxC(RGB). The array can be of type uint8
                in range [0, 255], or floating point in range [0, 255].
            annotations (list[dict[str->str]]):
                Each item in the list is a bbox label of an object. The object is
                    represented by a dict,
                which contains:
                 - bbox (list): bbox coordinates, top left and bottom right.
                 - bbox_mode (str): bbox label mode, for example: `XYXY_ABS`,
                    `XYWH_ABS` and so on...
        """
        sample_mode = (1, *self.min_ious, 0)
        h, w = img.shape[:2]

        boxes = torch.tensor(boxes)

        while True:
            mode = np.random.choice(sample_mode)
            if mode == 1:
                return NoOpTransform()

            min_iou = mode
            for _ in range(50):
                new_w = np.random.uniform(self.min_crop_size * w, w)
                new_h = np.random.uniform(self.min_crop_size * h, h)

                # h / w in [0.5, 2]
                if new_h / new_w < 0.5 or new_h / new_w > 2:
                    continue

                left = np.random.uniform(w - new_w)
                top = np.random.uniform(h - new_h)

                patch = torch.tensor([left, top, left + new_w, top + new_h],
                                     dtype=torch.int)

                overlaps = pairwise_iou(Boxes(patch.view(-1, 4)),
                                        Boxes(boxes.view(-1, 4)))

                if overlaps.min() < min_iou:
                    continue

                # center of boxes should inside the crop img
                center = (boxes[:, :2] + boxes[:, 2:]) / 2
                mask = ((center[:, 0] > patch[0]) * (center[:, 1] > patch[1]) *
                        (center[:, 0] < patch[2]) * (center[:, 1] < patch[3]))
                if not mask.any():
                    continue
                return CropTransform(int(left), int(top), int(new_w),
                                     int(new_h))
Ejemplo n.º 17
0
    def get_transform(self, image):
        h, w = image.shape[:2]
        if self.is_range:
            size = np.random.randint(self.short_edge_length[0],
                                     self.short_edge_length[1] + 1)
        else:
            size = np.random.choice(self.short_edge_length)
        if size == 0:
            return NoOpTransform()

        newh, neww = ResizeShortestEdge.get_output_shape(
            h, w, size, self.max_size)
        return ResizeTransform(h, w, newh, neww, self.interp)
Ejemplo n.º 18
0
    def get_transform(self, img):
        h, w = img.shape[:2]

        do = self.do[self.idx]
        # print('idx', self.idx)
        # print('do', do)
        # print('self.do', self.do)

        self.idx += 1
        if self.idx == self.ring:
            self.idx = 0
            self.do = [self._rand_range() < self.prob] * self.ring

        if do:
            if self.horizontal:
                # print('HFlipTransform')
                return HFlipTransform(w)
                # print([HFlipTransform(w)]*self.ring)
                # yield from [HFlipTransform(w)]*self.ring
            elif self.vertical:
                return VFlipTransform(h)
        else:
            # print('NoOpTransform')
            return NoOpTransform()
Ejemplo n.º 19
0
def Resize_rotated_box(transform, rotated_boxes):
    """
    Apply the resizing transform on rotated boxes. For details of how these (approximation)
    formulas are derived, please refer to :meth:`RotatedBoxes.scale`.

    Args:
        rotated_boxes (ndarray): Nx5 floating point array of
            (x_center, y_center, width, height, angle_degrees) format
            in absolute coordinates.
    """
    rotated_boxes = np.asarray(rotated_boxes).reshape(-1, 5)
    scale_factor_x = transform.new_w * 1.0 / transform.w
    scale_factor_y = transform.new_h * 1.0 / transform.h
    rotated_boxes[:, 0] *= scale_factor_x
    rotated_boxes[:, 1] *= scale_factor_y
    theta = rotated_boxes[:, 4] * np.pi / 180.0
    c = np.cos(theta)
    s = np.sin(theta)
    rotated_boxes[:, 2] *= np.sqrt(np.square(scale_factor_x * c) + np.square(scale_factor_y * s))
    rotated_boxes[:, 3] *= np.sqrt(np.square(scale_factor_x * s) + np.square(scale_factor_y * c))
    rotated_boxes[:, 4] = np.arctan2(scale_factor_x * s, scale_factor_y * c) * 180 / np.pi

    return rotated_boxes


HFlipTransform.register_type("rotated_box", HFlip_rotated_box)
VFlipTransform.register_type("rotated_box", VFlip_rotated_box)
# NoOpTransform.register_type("rotated_box", lambda t, x: x)
NoOpTransform.register_type("rotated_box", NoOp_rotated_box)
ResizeTransform.register_type("rotated_box", Resize_rotated_box)
Ejemplo n.º 20
0
 def __call__(self, aug_input):
     do = self._rand_range() < self.prob
     if do:
         return self.aug(aug_input)
     else:
         return NoOpTransform()
Ejemplo n.º 21
0
 def get_transform(self, *args):
     do = self._rand_range() < self.prob
     if do:
         return self.aug.get_transform(*args)
     else:
         return NoOpTransform()
 def get_transform(self):
     do = self._rand_range() < self.prob
     if do:
         return ZFlipTransform()
     else:
         return NoOpTransform()
Ejemplo n.º 23
0
 def inverse(self):
     return NoOpTransform()
Ejemplo n.º 24
0
    Apply the resizing transform on rotated boxes. For details of how these (approximation)
    formulas are derived, please refer to :meth:`RotatedBoxes.scale`.

    Args:
        rotated_boxes (ndarray): Nx5 floating point array of
            (x_center, y_center, width, height, angle_degrees) format
            in absolute coordinates.
    """
    scale_factor_x = transform.new_w * 1.0 / transform.w
    scale_factor_y = transform.new_h * 1.0 / transform.h
    rotated_boxes[:, 0] *= scale_factor_x
    rotated_boxes[:, 1] *= scale_factor_y
    theta = rotated_boxes[:, 4] * np.pi / 180.0
    c = np.cos(theta)
    s = np.sin(theta)
    rotated_boxes[:, 2] *= np.sqrt(
        np.square(scale_factor_x * c) + np.square(scale_factor_y * s))
    rotated_boxes[:, 3] *= np.sqrt(
        np.square(scale_factor_x * s) + np.square(scale_factor_y * c))
    rotated_boxes[:, 4] = np.arctan2(scale_factor_x * s,
                                     scale_factor_y * c) * 180 / np.pi

    return rotated_boxes


HFlipTransform.register_type("rotated_box", HFlip_rotated_box)
ResizeTransform.register_type("rotated_box", Resize_rotated_box)

# not necessary any more with latest fvcore
NoOpTransform.register_type("rotated_box", lambda t, x: x)
Ejemplo n.º 25
0
 def get_transform(self, image):
     do = self._rand_range() < self.prob
     if do:
         return MotionBlurTransform(prob=self.prob, kernel_size=self.kernel_size)
     else:
         return NoOpTransform()
    def get_transform(self, img):

        return NoOpTransform()