Beispiel #1
0
    def recall(self, image, mask):
        if self.p <= self.probability:
            image = ReplayCompose.replay(self.replay, image=image)["image"]

            if self.apply_to_mask:
                mask = ReplayCompose.replay(self.replay, image=mask)["image"]

        return image, mask
Beispiel #2
0
    def __init__(self, transformation, probability, apply_to_mask=False):
        self.transformation = transformation
        self.transformation.p = 1
        self.transformation = ReplayCompose([self.transformation])

        self.probability = probability
        self.p = None

        self.apply_to_mask = apply_to_mask
Beispiel #3
0
 def __init__(self,
              func: ImageOnlyTransform,
              inputs: Union[str, List[str]],
              outputs: Union[str, List[str]],
              mode: Union[None, str, Iterable[str]] = None):
     super().__init__(inputs=inputs, outputs=outputs, mode=mode)
     assert len(self.inputs) == len(
         self.outputs), "Input and Output lengths must match"
     self.func = Compose(transforms=[func])
     self.replay_func = ReplayCompose(transforms=[deepcopy(func)])
     self.in_list, self.out_list = True, True
    def augment(self, augmentator):
        self.reset_augmentation()
        aug_output = augmentator(image=self.image, mask=self._encoded_masks)
        self.image = aug_output['image']
        self._encoded_masks = aug_output['mask']

        aug_replay = aug_output.get('replay', None)
        if aug_replay:
            assert len(self._ignored_regions) == 0
            mask_replay = remove_image_only_transforms(aug_replay)
            self._soft_mask_aug = ReplayCompose._restore_for_replay(
                mask_replay)

        self._compute_objects_areas()
        self.remove_small_objects(min_area=1)

        self._augmented = True
Beispiel #5
0
class ImageOnlyAlbumentation(NumpyOp):
    """Operators which apply to single images (as opposed to images + masks or images + bounding boxes).

    This is a wrapper for functionality provided by the Albumentations library:
    https://github.com/albumentations-team/albumentations. A useful visualization tool for many of the possible effects
    it provides is available at https://albumentations-demo.herokuapp.com.

    Args:
        func: An Albumentation function to be invoked.
        inputs: Key(s) from which to retrieve data from the data dictionary. If more than one key is provided, the
            `func` will be run in replay mode so that the exact same augmentation is applied to each value.
        outputs: Key(s) under which to write the outputs of this Op back to the data dictionary.
        mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
            regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
            like "!infer" or "!train".
        ds_id: What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all
            ds_ids except for a particular one, you can pass an argument like "!ds1".
    """
    def __init__(self,
                 func: ImageOnlyTransform,
                 inputs: Union[str, List[str]],
                 outputs: Union[str, List[str]],
                 mode: Union[None, str, Iterable[str]] = None,
                 ds_id: Union[None, str, Iterable[str]] = None):
        super().__init__(inputs=inputs,
                         outputs=outputs,
                         mode=mode,
                         ds_id=ds_id)
        assert len(self.inputs) == len(
            self.outputs), "Input and Output lengths must match"
        self.func = Compose(transforms=[func])
        self.replay_func = ReplayCompose(transforms=[deepcopy(func)])
        self.in_list, self.out_list = True, True

    def forward(self, data: List[np.ndarray],
                state: Dict[str, Any]) -> List[np.ndarray]:
        results = [
            self.replay_func(image=data[0]) if len(data) > 1 else self.func(
                image=data[0])
        ]
        for i in range(1, len(data)):
            results.append(
                self.replay_func.replay(results[0]['replay'], image=data[i]))
        return [result["image"] for result in results]
Beispiel #6
0
class Transformation(object):
    def __init__(self, transformation, probability, apply_to_mask=False):
        self.transformation = transformation
        self.transformation.p = 1
        self.transformation = ReplayCompose([self.transformation])

        self.probability = probability
        self.p = None

        self.apply_to_mask = apply_to_mask

    def __call__(self, image, mask):
        self.p = random()

        return self._call(image, mask)

    def _call(self, image, mask):
        if self.p <= self.probability:
            data = self.transformation(image=image, force_apply=True)

            image = data["image"]

            self.replay = data["replay"]

            if self.apply_to_mask:
                mask = self.transformation.replay(self.replay,
                                                  image=mask)["image"]

        mask[mask > 1] = 255
        mask[mask <= 1] = 0

        return image, mask

    def recall(self, image, mask):
        if self.p <= self.probability:
            image = ReplayCompose.replay(self.replay, image=image)["image"]

            if self.apply_to_mask:
                mask = ReplayCompose.replay(self.replay, image=mask)["image"]

        return image, mask