def train_multi_augment1(image, **kwargs):
    seq = iaa.SomeOf(1, [
        iaa.Noop(),
        iaa.Fliplr(p=1),
    ])
    image = seq.augment_images([image])[0]
    return image
Пример #2
0
def matsushita_aug():
    return iaa.SomeOf((1, 4), [
        iaa.Noop(),
        iaa.Superpixels(p_replace=0.5, n_segments=64),
        iaa.Add((-50, 50), per_channel=True),
        iaa.Grayscale(alpha=(0.0, 1.0)),
        iaa.GaussianBlur(sigma=(0.1, 1.0)),
        iaa.ContrastNormalization((0.5, 2), per_channel=0.5),
        iaa.AdditiveGaussianNoise(scale=0.075 * 255, per_channel=0.5),
        iaa.CoarseDropout(
            (0.1, 0.25), size_percent=(0.02, 0.25), per_channel=0.5),
        iaa.OneOf([
            iaa.Emboss(alpha=0.25),
            iaa.Sharpen(alpha=0.5),
            iaa.Invert(1, per_channel=1.0),
        ]),
        iaa.Affine(scale={
            'x': (0.84, 1.16),
            'y': (0.84, 1.16)
        },
                   mode=['edge', 'wrap']),
        iaa.Affine(translate_percent={
            'x': (-0.16, 0.16),
            'y': (-0.16, 0.16)
        },
                   mode=['edge', 'wrap']),
        iaa.Affine(rotate=(-16, 16), mode=['edge', 'wrap']),
        iaa.Affine(shear=(-16, 16), mode=['edge', 'wrap']),
        iaa.Fliplr(1)
    ])
Пример #3
0
    def epistemic(self, model, iterations=1000):
        """
	estimates data uncertanity
        iterations: montecarlo sample iterations

        """
        self.aug = iaa.SomeOf(
            1,
            [
                iaa.Affine(rotate=iap.Normal(0.0, 3),
                           translate_px=iap.Normal(0.0, 3)),
                # iaa.AdditiveGaussianNoise(scale=0.3 * np.ptp(test_image) - 9),
                iaa.Noop(),
                iaa.MotionBlur(k=3, angle=[-2, 2])
            ],
            random_order=True)

        predictions = []

        for i in range(iterations):
            aug_image = self.aug.augment_images(self.test_image)
            predictions.append(model.predict(aug_image[None, ...]))

        predictions = np.array(predictions)
        mean = np.mean(predictions, axis=0)
        var = np.var(predictions, axis=0)

        return mean, var
Пример #4
0
    def aleatoric(self, model, iterations=1000):
        """
	    estimates data uncertanity
        iterations: montecarlo sample iterations

        """
        self.aug = iaa.Sequential(
            [
                iaa.Affine(rotate=(-1, 1),
                           # translate_px={"x": (-1, 1), "y": (-1, 1)}
                           ),
                # iaa.SaltAndPepper(0.001),
                iaa.AdditiveGaussianNoise(scale=0.2),
                iaa.Noop(),
                iaa.MotionBlur(k=3, angle=[-1, 1])
            ],
            random_order=False)

        predictions = []

        for i in range(iterations):
            aug_image = self.aug.augment_images(
                self.test_image.astype(np.float32))
            predictions.append(model.predict(aug_image[None, ...]))

        predictions = np.array(predictions)
        mean = np.mean(predictions, axis=0)
        var = np.var(predictions, axis=0)

        return mean, var
Пример #5
0
    def __getitem__(self, idx):

        # When truths are resized, the values change
        # we apply a threshold to get back to binary

        im = self.imgs[idx]
        im = self.reshaper_img.augment_image(im)

        shape = im.shape

        truth = None

        if (self.truth_paths is not None):
            truth_path = self.truth_paths[idx]
            truth = imread(truth_path, scale=False)[..., :1]
            truth = truth > 0

            if (len(truth.shape) == 3):
                truth = np.mean(truth, axis=-1)

            truth = truth.astype(np.uint8)
        else:
            truth = np.zeros(im.shape[:2]).astype(np.uint8)

        # Apply data augmentation
        if (self.augmentations is not None):
            aug_det = self.augmentations.to_deterministic()
        else:
            aug_det = iaa.Noop()

        im_aug = aug_det.augment_images([im])[0]
        im_unnormal = im_aug.copy()

        truth = ia.SegmentationMapsOnImage(truth, shape=truth.shape)
        labels = ia.SegmentationMapsOnImage(self.labels[..., idx].astype(
            np.int16),
                                            shape=self.labels[..., idx].shape)

        if (self.normalization is not None):
            im = self.normalization.augment_image(im)
            im_aug = self.normalization.augment_image(im_aug)

        truth = truth.get_arr()[..., np.newaxis]
        labels = labels.get_arr()[..., np.newaxis]

        im_aug = self.reshaper_img.augment_image(im_aug)
        im_unnormal = self.reshaper_img.augment_image(im_unnormal)
        truth = self.reshaper_seg.augment_image(truth)
        labels = self.reshaper_seg.augment_image(labels)

        return {
            'image': im_aug,
            'image_noaug': im,
            'image_unnormal': im_unnormal,
            'frame_name': os.path.split(self.img_paths[idx])[-1],
            'labels': labels,
            'frame_idx': idx,
            'n_frames': self.__len__(),
            'label/segmentation': truth
        }
Пример #6
0
    def setup_image_augment(self):
        """Prepare the image augmenter using imgaug package."""
        ops = []
        params = self.augment_ops.gauss_blur
        ops.append(
            iaa.Sometimes(
                params.use_prob,
                iaa.GaussianBlur(sigma=(params.sigma[0], params.sigma[1]))))
        if self.augment_ops.contrast.use:
            params = self.augment_ops.contrast
            ops.append(
                iaa.ContrastNormalization(alpha=(params.alpha[0],
                                                 params.alpha[1])))
        if self.augment_ops.add_gauss_noise.use:
            params = self.augment_ops.add_gauss_noise
            ops.append(
                iaa.AdditiveGaussianNoise(loc=params.loc,
                                          scale=(params.scale[0],
                                                 params.scale[1]),
                                          per_channel=params.per_channel))
        if self.augment_ops.brightness.use:
            params = self.augment_ops.brightness
            ops.append(
                iaa.Multiply((params.scale[0], params.scale[1]),
                             per_channel=params.per_channel))
        if self.augment_ops.hue_saturation.use:
            params = self.augment_ops.hue_saturation
            ops.append(iaa.AddToHueAndSaturation((params.min, params.max)))

        if not ops:
            logging.warning(
                'Pixel distortion has been switched on but no operations defined!'
            )
            ops += iaa.Noop()
        self.image_augmenter = iaa.Sequential(ops, random_order=True)
Пример #7
0
    def _setup(self):
        """Setup the augmentation chain."""
        if self._is_setup:
            return

        # Only setup once
        self._is_setup = True

        methods = [
            '_pre_process',
            '_images_only',
            '_masks_only',
            '_mask_and_images'
        ]

        for method in methods:
            attr = getattr(self, method)
            if len(attr) == 0:
                new = iaa.Noop()
            elif len(attr) == 1:
                new = attr[0]
            else:
                new = iaa.Sequential(attr)

            if method == '_mask_and_images':
                new = new.to_deterministic()

            setattr(self, method, new)

        # Change out normalization
        self.apply_image_normalization = self._normalize_method['img']
        self.apply_mask_normalization = self._normalize_method['mask']
Пример #8
0
    def _test_map_batches_both(self, call_async):
        augseq = iaa.Noop()
        mock_Pool = mock.MagicMock()
        mock_Pool.return_value = mock_Pool
        mock_Pool.map.return_value = "X"
        mock_Pool.map_async.return_value = "X"
        with mock.patch("multiprocessing.Pool", mock_Pool):
            batches = [
                ia.Batch(images=[ia.quokka()]),
                ia.Batch(images=[ia.quokka() + 1])
            ]
            with multicore.Pool(augseq, processes=1) as pool:
                if call_async:
                    _ = pool.map_batches_async(batches)
                else:
                    _ = pool.map_batches(batches)

            if call_async:
                to_check = mock_Pool.map_async
            else:
                to_check = mock_Pool.map

            assert to_check.call_count == 1
            # args, arg 0
            assert to_check.call_args[0][0] == multicore._Pool_starworker
            # args, arg 1 (batches with ids), tuple 0, entry 0 in tuple (=> batch id)
            assert to_check.call_args[0][1][0][0] == 0
            # args, arg 1 (batches with ids), tuple 0, entry 1 in tuple (=> batch)
            assert np.array_equal(to_check.call_args[0][1][0][1].images_unaug,
                                  batches[0].images_unaug)
            # args, arg 1 (batches with ids), tuple 1, entry 0 in tuple (=> batch id)
            assert to_check.call_args[0][1][1][0] == 1
            # args, arg 1 (batches with ids), tuple 1, entry 1 in tuple (=> batch)
            assert np.array_equal(to_check.call_args[0][1][1][1].images_unaug,
                                  batches[1].images_unaug)
Пример #9
0
    def get_augumentor(self, split):
        if split != 'train':
            self.augumentor = T.Compose(
                [T.ToPILImage(), T.ToTensor(),
                 T.Normalize([0.0789, 0.0529, 0.0546, 0.0814], [0.147, 0.113, 0.157, 0.148])])
            return

        aug = iaa.SomeOf(
            n=(1, 6), children=[
                iaa.Noop(),
                iaa.Sequential([iaa.Add((-5, 5), per_channel=True),
                                iaa.Multiply((0.8, 1.2), per_channel=True)]),
                iaa.Crop(percent=(0, 0.15)),
                iaa.Affine(shear=(-16, 16)),
                iaa.OneOf([
                    iaa.Affine(rotate=90),
                    iaa.Affine(rotate=180),
                    iaa.Affine(rotate=270),
                    iaa.Fliplr(1),
                    iaa.Flipud(1),
                ])
            ])

        self.augumentor = T.Compose(
            [aug.augment_image,
             T.ToPILImage(),
             T.ToTensor(),
             T.Normalize([0.0789, 0.0529, 0.0546, 0.0814], [0.147, 0.113, 0.157, 0.148])])
Пример #10
0
    def __init__(self, data_config, transform=False, mode="train", visualize=False):

        # Set the mode (train/val)
        self.mode = mode

        self.visualize = visualize

        # Read in necessary configs
        file_data_path = data_config[mode]
        self.image_directory = data_config["image_directory"]
        self.annotation_directory = data_config["annotation_directory"]
        self.classes = tuple(data_config['classes'])

        self.file_list = self.create_file_list(file_data_path)

        self.transform = transform

        self.transformations = iaa.Noop()

        self.height = data_config["figure_size"]
        self.width = data_config["figure_size"]

        self.resize_transformation = iaa.Resize({
            "height": self.height,
            "width": self.width
        })

        self.data_encoder = DataEncoder(data_config)
        self.default_boxes = self.data_encoder.default_boxes
Пример #11
0
def display_random_data(data: FashionDataset):
    augmentation = iaa.SomeOf(1, [
        iaa.AdditiveGaussianNoise(scale=0.15 * 255),
        iaa.Noop(),
        iaa.MotionBlur(k=18),
        iaa.Fliplr(),
        iaa.Affine(translate_percent={
            "x": (-0.2, 0.2),
            "y": (-0.2, 0.2)
        }),
        iaa.CropToFixedSize(width=data.image_shape[0],
                            height=data.image_shape[1])
    ],
                              random_order=True)

    selected_image_ids = random.sample(list(data.image_ids), 4)
    selected_images = [data.get_image(im_id) for im_id in selected_image_ids]
    selected_images_class = [
        data.class_names[data.get_class_for_image(im_id)]
        for im_id in selected_image_ids
    ]

    plt.figure(figsize=(20, 10))
    plt.imshow(np.hstack(selected_images))
    plt.figtext(.5, .75, 'Orginal picture', fontsize=30, ha='center')
    plt.figtext(.5, .70, selected_images_class, fontsize=20, ha='center')
    plt.figure(figsize=(20, 10))
    plt.figtext(.5, .75, 'augmented picture', fontsize=30, ha='center')
    plt.imshow(np.hstack(augmentation(images=selected_images)))
Пример #12
0
def main():
    def z(shape):
        return np.zeros(shape, dtype=np.uint8)

    seq = iaa.Noop()

    print("This should generate NO warning:")
    _image_aug = seq.augment_images(z((1, 16, 16, 3)))

    print("This should generate NO warning:")
    _image_aug = seq.augment_images(z((16, 16, 8)))

    print("This should generate NO warning:")
    _image_aug = seq.augment_images([z((16, 16, 3))])

    print("This should generate NO warning:")
    _image_aug = seq.augment_images([z((16, 16))])

    print("This should generate a warning:")
    _image_aug = seq.augment_images(z((16, 16, 3)))

    print("This should generate a warning:")
    for _ in range(2):
        _image_aug = seq.augment_images(z((16, 16, 1)))

    print("This should fail:")
    _image_aug = seq.augment_images(z((16, 16)))
Пример #13
0
def zanoni_aug():
    return iaa.SomeOf(
        (1, 4),
        [
            # OKS
            iaa.Noop(),
            iaa.Grayscale(alpha=(0.25, 1.0)),
            iaa.Flipud(0.45),
            iaa.Fliplr(0.45),
            iaa.AddToHueAndSaturation((-40, 40)),
            iaa.ContrastNormalization((0.8, 1.2), per_channel=0.33),
            iaa.AdditiveGaussianNoise(scale=0.02 * 255),
            iaa.GaussianBlur(sigma=(0.1, 1.0)),
            iaa.OneOf([
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="BGR")
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="YCrCb"),
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSL")
            ]),
            iaa.OneOf([
                iaa.Emboss(alpha=(0.0, 0.33), strength=(0.02, 0.25)),
                iaa.Sharpen(alpha=(0.0, 0.33), lightness=(0.02, 0.25))
            ]),
            iaa.OneOf([
                iaa.Add((-15, 15), per_channel=0.25),
                iaa.Multiply((0.5, 2.0), per_channel=0.25)
            ])
            #iaa.CoarseDropout((0.1, 0.25), size_percent=(0.5, 0.75), per_channel=0.5)
        ])
Пример #14
0
def build_tta_transforms(cfg):
    """
    :param cfg:
    :param dataset_name: cfg.DATASETS.TEST
    :param is_train: False
    :return: List[transforms]
    """

    min_size = cfg.INPUT.MIN_SIZE_TEST
    max_size = cfg.INPUT.MAX_SIZE_TEST
    mean = cfg.INPUT.TEST_PIXEL_MEAN
    std = cfg.INPUT.TEST_PIXEL_STD

    augments = [
        iaa.Noop(),
        iaa.Affine(rotate=90),
        iaa.Affine(rotate=180),
        iaa.Affine(rotate=270),
        iaa.Fliplr(1.0),
        iaa.Flipud(1.0)
    ]

    transforms = []
    for aug in augments:
        transforms.append(
            T.Compose([
                aug.augment_image,
                T.ToPILImage(),  # magic fix for negative stride problem
                T.ToTensor(),
                T.Normalize(mean=mean, std=std)
            ]))

    return transforms
Пример #15
0
def my_aug(pics):
    # at labels generating step, we will not useaugmentation that would influence  0 value of background
    '''input  : [img]'''
    '''output : [aug_img]''' 
          
    aug = iaa.Sequential(
        [
            iaa.SomeOf(1, [
                iaa.Noop(),
                iaa.Grayscale(alpha=(0.0, 1.0)),
#                 iaa.Invert(0.5, per_channel=True),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.AddToHueAndSaturation((-20, 20)),
                iaa.GaussianBlur(sigma=(0, 3.0)),
                iaa.Dropout((0.01, 0.1), per_channel=0.5),
                iaa.SaltAndPepper(0.05),
                iaa.AverageBlur(k=(2, 7)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))
            ])
        ])
    # let every round use a same augmentation
    aug_pics = aug.augment_images(pics)
       
    return aug_pics
Пример #16
0
    def test_propagation_hooks_ctx(self):
        def propagator(images, augmenter, parents, default):
            if ia.is_np_array(images):
                return False
            else:
                return True

        hooks = ia.HooksImages(propagator=propagator)

        batch = ia.BatchInAugmentation(
            images=np.zeros((3, 3, 4, 1), dtype=np.uint8),
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(3, 4, 1)),
                ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 4, 1)),
                ia.KeypointsOnImage([ia.Keypoint(2, 2)], shape=(3, 4, 1))
            ])

        with batch.propagation_hooks_ctx(iaa.Noop(), hooks, []) as batch_prop:
            assert batch_prop.images is None
            assert batch_prop.keypoints is not None
            assert len(batch_prop.keypoints) == 3

            batch_prop.keypoints[0].keypoints[0].x = 10

        assert batch.images is not None
        assert batch.keypoints is not None
        assert batch.keypoints[0].keypoints[0].x == 10
Пример #17
0
    def test_processes(self):
        augseq = iaa.Noop()
        mock_Pool = mock.MagicMock()
        mock_cpu_count = mock.Mock()

        patch_pool = mock.patch("multiprocessing.Pool", mock_Pool)
        patch_cpu_count = mock.patch("multiprocessing.cpu_count",
                                     mock_cpu_count)
        with patch_pool, patch_cpu_count:
            # (cpu cores available, processes requested, processes started)
            combos = [(1, 1, 1), (2, 1, 1), (3, 1, 1), (1, 2, 2), (3, 2, 2),
                      (1, None, None), (2, None, None), (3, None, None),
                      (1, -1, 1), (2, -1, 1), (3, -1, 2), (4, -2, 2)]

            for cores_available, processes_req, expected in combos:
                with self.subTest(cpu_count_available=cores_available,
                                  processes_requested=processes_req):
                    mock_cpu_count.return_value = cores_available
                    with multicore.Pool(augseq,
                                        processes=processes_req) as _pool:
                        pass

                    if expected is None:
                        assert mock_Pool.call_args[0][0] is None
                    else:
                        assert mock_Pool.call_args[0][0] == expected
Пример #18
0
    def __init__(self, **config):
        from numpy.random import uniform
        from numpy.random import randint

        ## old photometric
        self.aug = iaa.Sequential([
            # iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))),
            # iaa.Sometimes(0.25,
            #               iaa.OneOf([iaa.Dropout(p=(0, 0.1)),
            #                          iaa.CoarseDropout(0.1, size_percent=0.5)])),
            # iaa.Sometimes(0.25,
            #               iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05), per_channel=0.5),
            #               )
        ])


        if config['photometric']['enable']:
            params = config['photometric']['params']
            aug_all = []
            # if params.get('random_brightness', False):
            #     change = params['random_brightness']['max_abs_change']
            #     aug = iaa.Add((-change, change))
            #     #                 aug_all.append(aug)
            #     aug_all.append(aug)

            # if params.get('random_contrast', False):
            #     change = params['random_contrast']['strength_range']
            #     aug = iaa.LinearContrast((change[0], change[1]))
            #     aug_all.append(aug)

            # if params.get('additive_gaussian_noise', False):
            #     change = params['additive_gaussian_noise']['stddev_range']
            #     aug = iaa.AdditiveGaussianNoise(scale=(change[0], change[1]))
            #     aug_all.append(aug)

            if params.get('additive_speckle_noise', False):
                change = params['additive_speckle_noise']['prob_range']
                aug = iaa.ImpulseNoise(p=(change[0], change[1]))
                aug_all.append(aug)

            # if params.get('motion_blur', False):
            #     change = params['motion_blur']['max_kernel_size']
            #     if change > 3:
            #         change = randint(3, change)
            #     elif change == 3:
            #         aug = iaa.Sometimes(0.5, iaa.MotionBlur(change))
            #     aug_all.append(aug)

            # if params.get('GaussianBlur', False):
            #     change = params['GaussianBlur']['sigma']
            #     aug = iaa.GaussianBlur(sigma=(change))
            #     aug_all.append(aug)

            self.aug = iaa.Sequential(aug_all)


        else:
            self.aug = iaa.Sequential([
                iaa.Noop(),
            ])
 def augmentator(images):
     """Apply data augmentation"""
     augmenter = iaa.Sequential([
         # Invert pixel values on 25% images
         iaa.Invert(0.25, per_channel=0.5),
         # Blur 30% of images
         iaa.Sometimes(
             .3,
             iaa.OneOf([
                 iaa.GaussianBlur(sigma=(0.0, 3.0)),
                 iaa.AverageBlur(k=(2, 2)),
                 iaa.MedianBlur(k=(1, 3)),
             ]),
         ),
         # Do embossing or sharpening
         iaa.OneOf([
             iaa.Sometimes(.2, iaa.Emboss(alpha=(0.0, .3),
                                          strength=(.2, .8))),
             iaa.Sometimes(
                 .2, iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))),
         ]),
         # Add one noise (or none)
         iaa.OneOf([
             iaa.Dropout((0, 0.01)),
             iaa.AdditiveGaussianNoise(scale=0.01 * 255),
             iaa.SaltAndPepper(0.01),
             iaa.Noop(),
         ]),
         # Convert to grayscale
         iaa.Sometimes(.2, iaa.Grayscale(alpha=(0.0, 1.0))),
         iaa.Sometimes(.4, iaa.LinearContrast((0.5, 1.5), per_channel=0.5)),
         # iaa.PiecewiseAffine(scale=(0.005, 0.05)),
     ])
     images = augmenter(images=images)
     return images
 def augment():
     with open(CONFIG, "r") as file:
         config = json.loads(file.read())
     if config[ImageAugmentation.AUGMENT_DATA]:
         matrix = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])
         return iaa.Sometimes(ImageAugmentation.AUG_PERCENTAGE, [
             iaa.GaussianBlur(sigma=2.0),
             iaa.Sequential([iaa.Affine(rotate=45),
                             iaa.Sharpen(alpha=1.0)]),
             iaa.WithColorspace(to_colorspace="HSV",
                                from_colorspace="RGB",
                                children=iaa.WithChannels(
                                    0, iaa.Add((10, 50)))),
             iaa.AdditiveGaussianNoise(scale=0.2 * 255),
             iaa.Add(50, per_channel=True),
             iaa.Sharpen(alpha=0.5),
             iaa.WithChannels(0, iaa.Add((10, 100))),
             iaa.WithChannels(0, iaa.Affine(rotate=(0, 45))),
             iaa.Noop(),
             iaa.Superpixels(p_replace=0.5, n_segments=64),
             iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128)),
             iaa.ChangeColorspace(from_colorspace="RGB",
                                  to_colorspace="HSV"),
             iaa.WithChannels(0, iaa.Add((50, 100))),
             iaa.ChangeColorspace(from_colorspace="HSV",
                                  to_colorspace="RGB"),
             iaa.Grayscale(alpha=(0.0, 1.0)),
             iaa.GaussianBlur(sigma=(0.0, 3.0)),
             iaa.AverageBlur(k=(2, 11)),
             iaa.AverageBlur(k=((5, 11), (1, 3))),
             iaa.MedianBlur(k=(3, 11)),
             iaa.Convolve(matrix=matrix),
             iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
             iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
             iaa.EdgeDetect(alpha=(0.0, 1.0)),
             iaa.DirectedEdgeDetect(alpha=(0.0, 1.0), direction=(0.0, 1.0)),
             iaa.Add((-40, 40)),
             iaa.Add((-40, 40), per_channel=0.5),
             iaa.AddElementwise((-40, 40)),
             iaa.AddElementwise((-40, 40), per_channel=0.5),
             iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
             iaa.AdditiveGaussianNoise(scale=0.05 * 255),
             iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5),
             iaa.Multiply((0.5, 1.5), per_channel=0.5),
             iaa.Dropout(p=(0, 0.2)),
             iaa.Dropout(p=(0, 0.2), per_channel=0.5),
             iaa.CoarseDropout(0.02, size_percent=0.5),
             iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
             iaa.CoarseDropout(0.02, size_percent=0.15, per_channel=0.5),
             iaa.Invert(0.25, per_channel=0.5),
             iaa.Invert(0.5),
             iaa.ContrastNormalization((0.5, 1.5)),
             iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
             iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25)
         ])
     else:
         return None
Пример #21
0
def test_BackgroundAugmenter__augment_images_worker():
    reseed()

    warnings.simplefilter("always")
    with warnings.catch_warnings(record=True) as caught_warnings:

        def gen():
            yield ia.Batch(images=np.zeros((1, 4, 4, 3), dtype=np.uint8))

        bl = multicore.BatchLoader(gen(), queue_size=2)
        bgaug = multicore.BackgroundAugmenter(bl,
                                              iaa.Noop(),
                                              queue_size=1,
                                              nb_workers=1)

        queue_source = multiprocessing.Queue(2)
        queue_target = multiprocessing.Queue(2)
        queue_source.put(
            pickle.dumps(
                ia.Batch(images=np.zeros((1, 4, 8, 3), dtype=np.uint8)),
                protocol=-1))
        queue_source.put(pickle.dumps(None, protocol=-1))
        bgaug._augment_images_worker(iaa.Add(1), queue_source, queue_target, 1)

        batch_aug = pickle.loads(queue_target.get())
        assert isinstance(batch_aug, ia.Batch)
        assert batch_aug.images_unaug is not None
        assert batch_aug.images_unaug.dtype == np.uint8
        assert batch_aug.images_unaug.shape == (1, 4, 8, 3)
        assert np.array_equal(batch_aug.images_unaug,
                              np.zeros((1, 4, 8, 3), dtype=np.uint8))
        assert batch_aug.images_aug is not None
        assert batch_aug.images_aug.dtype == np.uint8
        assert batch_aug.images_aug.shape == (1, 4, 8, 3)
        assert np.array_equal(batch_aug.images_aug,
                              np.zeros((1, 4, 8, 3), dtype=np.uint8) + 1)

        finished_signal = pickle.loads(queue_target.get())
        assert finished_signal is None

        source_finished_signal = pickle.loads(queue_source.get())
        assert source_finished_signal is None

        assert queue_source.empty()
        assert queue_target.empty()

        queue_source.close()
        queue_target.close()
        queue_source.join_thread()
        queue_target.join_thread()
        bl.terminate()
        bgaug.terminate()

    assert len(caught_warnings) > 0
    for warning in caught_warnings:
        assert ("BatchLoader is deprecated" in str(warning.message)
                or "BackgroundAugmenter is deprecated" in str(warning.message))
Пример #22
0
def main():
    images = [
        misc.imresize(
            ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)),
        misc.imresize(data.astronaut(), (128, 128))
    ]

    augmenters = [
        iaa.Noop(name="Noop"),
        iaa.Crop(px=(0, 8), name="Crop-px"),
        iaa.Crop(percent=(0, 0.1), name="Crop-percent"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Grayscale(0.5, name="Grayscale0.5"),
        iaa.Grayscale(1.0, name="Grayscale1.0"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1 * 255),
                                  name="AdditiveGaussianNoise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_px={
                       "x": (-16, 16),
                       "y": (-16, 16)
                   },
                   rotate=(-45, 45),
                   shear=(-16, 16),
                   order=ia.ALL,
                   cval=(0, 1.0),
                   mode=ia.ALL,
                   name="Affine"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation")
    ]

    #for i, aug in enumerate(augmenters):
    #print(i)
    #aug.deepcopy()
    #import copy
    #copy.deepcopy(aug)
    seq = iaa.Sequential([aug.copy() for aug in augmenters], name="Sequential")
    st = iaa.Sometimes(0.5, seq.copy(), name="Sometimes")
    augmenters.append(seq)
    augmenters.append(st)

    for augmenter in augmenters:
        print("Augmenter: %s" % (augmenter.name, ))
        grid = augmenter.draw_grid(images, rows=1, cols=16)
        misc.imshow(grid)
Пример #23
0
    def _compose_transforms(self):
        self.aug_functions = list()
        for transform_name in self._transforms.keys():
            if transform_name == 'resize':
                self.aug_functions.append(
                    iaa.Resize(
                        {
                            "height": self._transforms[transform_name].height,
                            "width": self._transforms[transform_name].width
                        },
                        name='resize'))
                continue
            elif transform_name == 'fliplr':
                self.aug_functions.append(
                    iaa.Fliplr(self._transforms[transform_name].p,
                               name='fliplr'))
                continue
            elif transform_name == 'flipud':
                self.aug_functions.append(
                    iaa.Flipud(self._transforms[transform_name].p,
                               name='flipud'))
                continue
            elif transform_name == 'rotate':
                self.aug_functions.append(
                    iaa.Rotate(self._transforms[transform_name].degrees,
                               name='rotate'))
                continue
            elif transform_name == 'JpegCompression':
                self.aug_functions.append(
                    iaa.JpegCompression(
                        compression=(self._transforms[transform_name].low,
                                     self._transforms[transform_name].high)))
                continue
            elif transform_name == 'GaussianBlur':
                self.aug_functions.append(
                    iaa.GaussianBlur(
                        sigma=(self._transforms[transform_name].low,
                               self._transforms[transform_name].high)))
                continue
            elif transform_name == 'CropToFixedSize':
                self.aug_functions.append(
                    iaa.CropToFixedSize(
                        width=self._transforms[transform_name].width,
                        height=self._transforms[transform_name].height,
                        position=self._transforms[transform_name].position))
                continue
            else:
                self.logger.info(
                    f'{transform_name} is not support in augment build')
                self.aug_functions.append(iaa.Noop())
                continue
        iaa_seq = iaa.Sequential(
            self.aug_functions,
            name=f'{self.cfg.DATASET.name}_{self.flag}_iaa_seq')

        return iaa_seq
Пример #24
0
    def test__to_deterministic(self):
        aug = iaa.WithHueAndSaturation([iaa.Noop()], from_colorspace="BGR")
        aug_det = aug.to_deterministic()

        assert not aug.deterministic  # ensure copy
        assert not aug.children[0].deterministic

        assert aug_det.deterministic
        assert isinstance(aug_det.children[0], iaa.Noop)
        assert aug_det.children[0].deterministic
Пример #25
0
def get_smart_crop_augmenter(
        image_size: Size2D,
        annotations: List[AnnotationInformation],
        min_crop_ratio: float,
        max_crop_ratio: float,
        min_crop_pix_size: int,
        max_crop_pix_size: int) -> iaa.Augmenter:
    repeats = 0

    if len(annotations) <= 1:
        return iaa.Noop()

    # Did not read crop algorithm with attention, may be this rules can be passed to _compute_crop_coordinates_1d
    #  directly for making more effective algorithm
    while True:
        x_intervals = [(ann.annotation.get_xyxy()[0], ann.annotation.get_xyxy()[2])
                       for ann in annotations
                       if type(ann.annotation) == Bbox]
        x_begin, x_end = _compute_crop_coordinates_1d(x_intervals)

        y_intervals = [(ann.annotation.get_xyxy()[1], ann.annotation.get_xyxy()[3])
                       for ann in annotations
                       if type(ann.annotation) == Bbox]
        y_begin, y_end = _compute_crop_coordinates_1d(y_intervals)

        w, h = x_end - x_begin, y_end - y_begin
        ratio = w / h

        if min_crop_ratio <= ratio <= max_crop_ratio and \
                min_crop_pix_size <= int(w * image_size.width) <= max_crop_pix_size and \
                min_crop_pix_size <= int(h * image_size.height) <= max_crop_pix_size:
            break

        repeats += 1
        if repeats > 100:
            return iaa.Noop()

    x_begin, x_end = int(x_begin * image_size.width), int(x_end * image_size.width)
    y_begin, y_end = int(y_begin * image_size.height), int(y_end * image_size.height)

    return iaa.Crop(px=(y_begin, image_size.width - x_end, image_size.height - y_end, x_begin),
                    deterministic=True,
                    keep_size=False)
Пример #26
0
    def __init__(self, X, y, train_mode,
                 image_transform, image_augment_with_target,
                 mask_transform, image_augment,
                 image_source='memory'):
        super().__init__()
        self.X = X
        if y is not None:
            self.y = y
        else:
            self.y = None

        self.train_mode = train_mode
        self.image_transform = image_transform
        self.mask_transform = mask_transform
        self.image_augment = image_augment if image_augment is not None else ImgAug(iaa.Noop())
        self.image_augment_with_target = image_augment_with_target if image_augment_with_target is not None else ImgAug(
            iaa.Noop())

        self.image_source = image_source
Пример #27
0
    def __init__(self,
                 root_path,
                 depth=2,
                 augmentations=None,
                 normalization=None,
                 resize_shape=None,
                 locs_fname='2dlocs.csv',
                 sp_labels_fname='sp_labels.npy',
                 sig_prior=0.05):
        """

        """
        super().__init__(root_path=root_path,
                         locs_fname=locs_fname,
                         sig_prior=sig_prior,
                         sp_labels_fname=sp_labels_fname)
        self.depth = depth

        self.graphs_path = pjoin(root_path, 'precomp_desc',
                                 'graphs_depth_{}'.format(self.depth))
        if (not os.path.exists(self.graphs_path)):
            self._prepare_graphs()

            os.makedirs(self.graphs_path)
            for i, g in enumerate(self.graphs):
                pickle.dump(
                    g,
                    open(pjoin(self.graphs_path, 'graph_{:04d}.p'.format(i)),
                         "wb"))

        self.____augmentations = iaa.Noop()
        self.____reshaper = iaa.Noop()
        self.____normalization = iaa.Noop()

        self.____normalization = make_normalizer(
            normalization, map(lambda s: s['image'], self))

        if (augmentations is not None):
            self.____augmentations = augmentations

        if (resize_shape is not None):
            self.____reshaper = iaa.size.Resize(resize_shape)
Пример #28
0
 def test___str__(self):
     child = iaa.Sequential([iaa.Noop(name="foo")])
     aug = iaa.WithHueAndSaturation(child)
     observed = aug.__str__()
     expected = ("WithHueAndSaturation("
                 "from_colorspace=RGB, "
                 "name=UnnamedWithHueAndSaturation, "
                 "children=[%s], "
                 "deterministic=False"
                 ")" % (child.__str__(), ))
     assert observed == expected
Пример #29
0
class ImgaugImageFormatterConfig(ImgaugTransformerConfig):
    new_image_size: Size2D
    format_options: ImageFormatOptions = ImageFormatOptions.KeepAspectRatioAndCenterPad
    use_smart_crop: bool = False
    min_crop_ratio: float = 9 / 16
    max_crop_ratio: float = 16 / 9
    min_crop_pix_size: int = 50
    max_crop_pix_size: int = 5000
    aug_pipeline: iaa.Augmenter = iaa.Noop()

    def owner_type(self) -> Type['ImgaugImageFormatter']:
        return ImgaugImageFormatter
    def test_warns_that_it_is_deprecated(self):
        children_fg = iaa.Noop()

        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            _ = overlay.FrequencyNoiseAlpha(first=children_fg)

        assert len(caught_warnings) == 1
        assert (
            "imgaug.augmenters.blend.FrequencyNoiseAlpha"
            in str(caught_warnings[-1].message)
        )