Exemplo n.º 1
0
 def get_transform(self, img):
     h, w = img.shape[:2]
     do = self._rand_range() < self.prob
     if do:
         return Rotation90Transform(h, w)
     else:
         return NoOpTransform()
Exemplo n.º 2
0
    def get_transform(self, image):
        h, w = image.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)
Exemplo n.º 3
0
 def get_transform(self, image):
     h, w = image.shape[:2]
     trans_x = int(self.t_range * w)
     trans_y = int(self.t_range * h)
     trans_x = random.randint(-trans_x, trans_x + 1)
     trans_y = random.randint(-trans_y, trans_y + 1)
     if trans_x == trans_y == 0:
         return NoOpTransform()
     else:
         return TranslationTransform(trans_x, trans_y)
    def __call__(self, dataset_dict):
        """
        Args:
            dict: a dict in standard model input format. See tutorials for details.

        Returns:
            list[dict]:
                a list of dicts, which contain augmented version of the input image.
                The total number of dicts is ``len(min_sizes) * (2 if flip else 1)``.
                Each dict has field "transforms" which is a TransformList,
                containing the transforms that are used to generate this image.
        """
        numpy_image = dataset_dict["image"].permute(1, 2, 0).numpy()
        shape = numpy_image.shape
        orig_shape = (dataset_dict["height"], dataset_dict["width"])
        if shape[:2] != orig_shape:
            # It transforms the "original" image in the dataset to the input image
            pre_tfm = ResizeTransform(orig_shape[0], orig_shape[1], shape[0],
                                      shape[1])
        else:
            pre_tfm = NoOpTransform()

        # Create all combinations of augmentations to use
        aug_candidates = []  # each element is a list[Augmentation]
        for min_size in self.min_sizes:
            resize = ResizeShortestEdge(min_size, self.max_size)
            aug_candidates.append([resize])  # resize only
            if self.flip:
                flip = RandomFlip(prob=1.0)
                aug_candidates.append([resize, flip])  # resize + flip

        # Apply all the augmentations
        ret = []
        for aug in aug_candidates:
            new_image, tfms = apply_augmentations(aug, np.copy(numpy_image))
            torch_image = torch.from_numpy(
                np.ascontiguousarray(new_image.transpose(2, 0, 1)))

            dic = copy.deepcopy(dataset_dict)
            dic["transforms"] = pre_tfm + tfms
            dic["image"] = torch_image

            if self.proposal_topk is not None:
                image_shape = new_image.shape[:2]  # h, w
                transform_proposals(dic,
                                    image_shape,
                                    tfms,
                                    proposal_topk=self.proposal_topk)

            ret.append(dic)
        return ret
Exemplo n.º 5
0
    def get_transform(self, image):
        if self.direction == "":
            return NoOpTransform()

        direction = self.direction

        do_horizontal = False
        do_vertical = False
        if direction == "horizontal" or direction == "both":
            do_horizontal = (self._rand_range() < self.prob)
        if direction == "vertical" or direction == "both":
            do_vertical = (self._rand_range() < self.prob)

        if do_horizontal:
            direction = "horizontal"
        if do_vertical:
            direction = "vertical"
        if do_horizontal and do_vertical:
            direction = "both"

        return FlipTransform(direction)
Exemplo n.º 6
0
    def __call__(self, dataset_dict):
        """
        Args:
            dict: a detection dataset dict in standard format

        Returns:
            list[dict]:
                a list of dataset dicts, which contain augmented version of the input image.
                The total number of dicts is ``len(min_sizes) * (2 if flip else 1)``.
                Each dict has field "transforms" which is a TransformList,
                containing the transforms that are used to generate this image.
        """
        numpy_image = dataset_dict["image"].permute(1, 2, 0).numpy()
        shape = numpy_image.shape
        orig_shape = (dataset_dict["height"], dataset_dict["width"])
        if shape[:2] != orig_shape:
            # It transforms the "original" image in the dataset to the input image
            pre_tfm = ResizeTransform(orig_shape[0], orig_shape[1], shape[0],
                                      shape[1])
        else:
            pre_tfm = NoOpTransform()

        # Create all combinations of augmentations to use
        tfm_gen_candidates = []  # each element is a list[TransformGen]
        for min_size in self.min_sizes:
            resize = ResizeShortestEdge(min_size, self.max_size)
            tfm_gen_candidates.append([resize])  # resize only
            if self.flip:
                flip = RandomFlip(prob=1.0)
                tfm_gen_candidates.append([resize, flip])  # resize + flip
            if self.vertical_flip:
                flip = RandomFlip(prob=1.0, horizontal=False, vertical=True)
                tfm_gen_candidates.append([resize, flip])  # resize + flip

            tfm_gen_candidates.append([resize, Rotation90Gen(prob=1.0)])
            # tfm_gen_candidates.append([resize, CLAHEGen(prob=1.0, img_format=self.image_format)])
            tfm_gen_candidates.append([
                resize,
                Rotation90Gen(prob=1.0),
                Rotation90Gen(prob=1.0),
                Rotation90Gen(prob=1.0)
            ])

        # Apply all the augmentations
        ret = []
        for tfm_gen in tfm_gen_candidates:
            new_image, tfms = apply_transform_gens(tfm_gen,
                                                   np.copy(numpy_image))
            torch_image = torch.from_numpy(
                np.ascontiguousarray(new_image.transpose(2, 0, 1)))

            dic = copy.deepcopy(dataset_dict)
            dic["transforms"] = pre_tfm + tfms
            dic["image"] = torch_image

            # pop original image width and height to prevent rescale boxes in postprocess of detector
            dic.pop("width")
            dic.pop("height")

            ret.append(dic)
        return ret
Exemplo n.º 7
0
 def get_transform(self, img):
     do = self._rand_range() < self.prob
     if do:
         return CLAHE(self.img_format)
     else:
         return NoOpTransform()