Пример #1
0
    def test_augment_images__list_as_value(self):
        base_img = np.zeros((2, 2, 3), dtype=np.uint8)
        base_img[..., 0] += 20
        base_img[..., 1] += 40
        base_img[..., 2] += 60

        aug = iaa.AddToHueAndSaturation([0, 10, 20])
        base_img = base_img[0:1, 0:1, :]
        expected_imgs = [
            iaa.AddToHueAndSaturation(0).augment_image(base_img),
            iaa.AddToHueAndSaturation(10).augment_image(base_img),
            iaa.AddToHueAndSaturation(20).augment_image(base_img)
        ]

        assert not np.array_equal(expected_imgs[0], expected_imgs[1])
        assert not np.array_equal(expected_imgs[1], expected_imgs[2])
        assert not np.array_equal(expected_imgs[0], expected_imgs[2])
        nb_iterations = 300
        seen = dict([(i, 0) for i, _ in enumerate(expected_imgs)])
        for _ in sm.xrange(nb_iterations):
            observed = aug.augment_image(base_img)
            for i, expected_img in enumerate(expected_imgs):
                if np.allclose(observed, expected_img):
                    seen[i] += 1
        assert np.sum(list(seen.values())) == nb_iterations
        n_exp = nb_iterations / 3
        n_exp_tol = nb_iterations * 0.1
        assert all(
            [n_exp - n_exp_tol < v < n_exp + n_exp_tol for v in seen.values()])
def main():
    image = data.astronaut()

    cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
    cv2.imshow("aug", image)
    cv2.waitKey(TIME_PER_STEP)

    # for value in cycle(np.arange(-255, 255, VAL_PER_STEP)):
    for value in np.arange(-255, 255, VAL_PER_STEP):
        aug = iaa.AddToHueAndSaturation(value=value)
        img_aug = aug.augment_image(image)
        img_aug = iaa.pad(img_aug, bottom=40)
        img_aug = ia.draw_text(img_aug,
                               x=0,
                               y=img_aug.shape[0] - 38,
                               text="value=%d" % (value, ),
                               size=30)

        cv2.imshow("aug", img_aug)
        cv2.waitKey(TIME_PER_STEP)

    images_aug = iaa.AddToHueAndSaturation(
        value=(-255, 255), per_channel=True).augment_images([image] * 64)
    ia.imshow(ia.draw_grid(images_aug))

    image = ia.quokka_square((128, 128))
    images_aug = []
    images_aug.extend(iaa.AddToHue().augment_images([image] * 10))
    images_aug.extend(iaa.AddToSaturation().augment_images([image] * 10))
    ia.imshow(ia.draw_grid(images_aug, rows=2))
Пример #3
0
    def test_augment_images(self):
        base_img = np.zeros((2, 2, 3), dtype=np.uint8)
        base_img[..., 0] += 20
        base_img[..., 1] += 40
        base_img[..., 2] += 60

        gen = itertools.product([False, True], ["cv2", "numpy"])
        for per_channel, backend in gen:
            with self.subTest(per_channel=per_channel, backend=backend):
                aug = iaa.AddToHueAndSaturation(0, per_channel=per_channel)
                aug.backend = backend
                observed = aug.augment_image(base_img)
                expected = base_img
                assert np.allclose(observed, expected)

                aug = iaa.AddToHueAndSaturation(30, per_channel=per_channel)
                aug.backend = backend
                observed = aug.augment_image(base_img)
                expected = self._add_hue_saturation(base_img, 30)
                diff = np.abs(observed.astype(np.float32) - expected)
                assert np.all(diff <= 1)

                aug = iaa.AddToHueAndSaturation(255, per_channel=per_channel)
                aug.backend = backend
                observed = aug.augment_image(base_img)
                expected = self._add_hue_saturation(base_img, 255)
                diff = np.abs(observed.astype(np.float32) - expected)
                assert np.all(diff <= 1)

                aug = iaa.AddToHueAndSaturation(-255, per_channel=per_channel)
                aug.backend = backend
                observed = aug.augment_image(base_img)
                expected = self._add_hue_saturation(base_img, -255)
                diff = np.abs(observed.astype(np.float32) - expected)
                assert np.all(diff <= 1)
Пример #4
0
def chapter_alpha_masks_introduction():
    # -----------------------------------------
    # example introduction
    # -----------------------------------------
    import imgaug as ia
    from imgaug import augmenters as iaa

    ia.seed(2)

    # Example batch of images.
    # The array has shape (8, 128, 128, 3) and dtype uint8.
    images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)],
                      dtype=np.uint8)

    seqs = [
        iaa.BlendAlpha((0.0, 1.0),
                       foreground=iaa.MedianBlur(11),
                       per_channel=True),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0),
                                   per_channel=False),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0),
                                   background=iaa.LinearContrast((0.5, 2.0)),
                                   per_channel=0.5),
        iaa.BlendAlphaFrequencyNoise(foreground=iaa.Affine(rotate=(-10, 10),
                                                           translate_px={
                                                               "x": (-4, 4),
                                                               "y": (-4, 4)
                                                           }),
                                     background=iaa.AddToHueAndSaturation(
                                         (-40, 40)),
                                     per_channel=0.5),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.BlendAlphaSimplexNoise(
            foreground=iaa.EdgeDetect(1.0),
            background=iaa.LinearContrast((0.5, 2.0)),
            per_channel=True),
                                   background=iaa.BlendAlphaFrequencyNoise(
                                       exponent=(-2.5, -1.0),
                                       foreground=iaa.Affine(rotate=(-10, 10),
                                                             translate_px={
                                                                 "x": (-4, 4),
                                                                 "y": (-4, 4)
                                                             }),
                                       background=iaa.AddToHueAndSaturation(
                                           (-40, 40)),
                                       per_channel=True),
                                   per_channel=True,
                                   aggregation_method="max",
                                   sigmoid=False)
    ]

    cells = []
    for seq in seqs:
        images_aug = seq(images=images)
        cells.extend(images_aug)

    # ------------

    save("alpha", "introduction.jpg", grid(cells, cols=8, rows=5))
Пример #5
0
def imag_aug_iaa_fun(imgn, idx=0):
    img_aug_list = []
    img_aug_list.append(imgn.copy())

    if idx == 0:  #单一方式增强
        seq = iaa.Sequential(
            [iaa.Sharpen(alpha=(0.0, 0.45), lightness=(0.65, 1.35))])
        # print('-------------------->>> imgaug 0 : Sharpen')
    elif idx == 1:
        seq = iaa.Sequential([
            iaa.AverageBlur(k=(2))
        ])  # blur image using local means with kernel sizes between 2 and 4
        # print('-------------------->>> imgaug 1 : AverageBlur')
    elif idx == 2:
        seq = iaa.Sequential([
            iaa.MedianBlur(k=(3))
        ])  # blur image using local means with kernel sizes between 3 and 5
        # print('-------------------->>> imgaug 2 : MedianBlur')
    elif idx == 3:
        seq = iaa.Sequential([iaa.GaussianBlur((0.0, 0.55))])
        # print('-------------------->>> imgaug 3 : GaussianBlur')
    elif idx == 4:
        seq = iaa.Sequential([iaa.ContrastNormalization((0.90, 1.10))])
        # print('-------------------->>> imgaug 4 : ContrastNormalization') #  对比度
    elif idx == 5:
        seq = iaa.Sequential([iaa.Add((-55, 55))])
        # print('-------------------->>> imgaug 5 : Add')
    elif idx == 6:
        seq = iaa.Sequential(
            [iaa.AddToHueAndSaturation((-10, 10), per_channel=True)])
        # print('-------------------->>> imgaug 6 : AddToHueAndSaturation')
    elif idx == 7:
        # seq = iaa.Sequential([iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.02*255), per_channel=False, name=None, deterministic=False, random_state=None)])
        seq = iaa.Sequential([
            iaa.AdditiveGaussianNoise(loc=0,
                                      scale=(0.0, 0.1 * 255),
                                      per_channel=False,
                                      name=None,
                                      deterministic=False,
                                      random_state=None)
        ])
        # print('-------------------->>> imgaug 7 : AdditiveGaussianNoise')
    elif idx == 8:  #复合增强方式
        # print(' *** 复合增强方式')
        # print('-------------------->>> 符合增强')
        seq = iaa.Sequential([
            iaa.Sharpen(alpha=(0.0, 0.05), lightness=(0.9, 1.1)),
            iaa.GaussianBlur((0, 0.8)),
            iaa.ContrastNormalization((0.9, 1.1)),
            iaa.Add((-5, 5)),
            iaa.AddToHueAndSaturation((-5, 5)),
        ])
    images_aug = seq.augment_images(img_aug_list)
    return images_aug[0].copy()
Пример #6
0
def aug_image(image, is_infer=False, augment = 1):
    if is_infer:
        flip_code = augment

        if flip_code == 1:
            seq = iaa.Sequential([iaa.Fliplr(1.0)])
        elif flip_code == 2:
            seq = iaa.Sequential([iaa.Flipud(1.0)])
        elif flip_code == 3:
            seq = iaa.Sequential([iaa.Flipud(1.0),
                                  iaa.Fliplr(1.0)])
        elif flip_code ==0:
            return image

    else:

        seq = iaa.Sequential([
            iaa.Affine(rotate= (-15, 15),
                       shear = (-15, 15),
                       mode='edge'),

            iaa.SomeOf((0, 2),
                       [
                           iaa.GaussianBlur((0, 1.5)),
                           iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5),
                           iaa.AddToHueAndSaturation((-5, 5)),
                           iaa.EdgeDetect(alpha=(0, 0.5)), 
                           iaa.CoarseSaltAndPepper(0.2, size_percent=(0.05, 0.1)), 
                       ],
                       random_order=True
                       )
        ])

    image = seq.augment_image(image)
    return image
Пример #7
0
            def __init__(self,
                         blur=True,
                         flip=False,
                         rotate=10,
                         shear=10,
                         **kwargs):
                from imgaug import augmenters as iaa

                sequence = []

                sequence += [
                    iaa.Scale((INPUT_SIZE, INPUT_SIZE)),
                    iaa.ContrastNormalization((0.75, 1.25)),
                    iaa.AddElementwise((-10, 10), per_channel=0.5),
                    iaa.AddToHueAndSaturation(value=(-20, 20),
                                              per_channel=True),
                    iaa.Multiply((0.75, 1.25)),
                ]
                sequence += [
                    iaa.PiecewiseAffine(scale=(0.0005, 0.005)),
                    iaa.Affine(rotate=(-rotate, rotate),
                               shear=(-shear, shear),
                               mode='symmetric'),
                    iaa.Grayscale(alpha=(0.0, 0.5)),
                ]
                if flip:
                    sequence += [
                        iaa.Fliplr(0.5),
                    ]
                if blur:
                    sequence += [
                        iaa.Sometimes(0.01, iaa.GaussianBlur(sigma=(0, 1.0))),
                    ]
                self.aug = iaa.Sequential(sequence)
    def __getitem__(self, idx):

        pair = self.pair_list[idx]

        input_img = cv2.imread(pair[0])
        input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
        img_label = pair[1]  # normal is 0

        # shape must be deterministic so it can be reused
        if self.shape_augs is not None:
            shape_augs = self.shape_augs.to_deterministic()
            input_img = shape_augs.augment_image(input_img)

        if self.input_augs is not None:
            input_img = iaa.Sequential(
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(
                            (0, 3.0)),  # gaussian blur with random sigma
                        iaa.MedianBlur(
                            k=(3, 5)),  # median with random kernel sizes
                        iaa.AdditiveGaussianNoise(
                            loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                    ]),
                    iaa.Add((-26, 26)),
                    iaa.AddToHueAndSaturation((-10, 10)),
                    iaa.LinearContrast((0.8, 1.2), per_channel=1.0),
                ],
                random_order=True).augment_image(input_img)

        return input_img, img_label
Пример #9
0
def image_aug(image):
    """
    @param image:
    @return:
    """
    seq = iaa.SomeOf(
        (1, 3),
        [
            iaa.Crop(px=(0, 16)),  # 裁剪
            iaa.Multiply((0.7, 1.3)),  # 改变色调
            iaa.Affine(scale=(0.5, 0.7)),  # 放射变换
            iaa.GaussianBlur(sigma=(0, 1.5)),  # 高斯模糊
            iaa.AddToHueAndSaturation(value=(25, -25)),
            iaa.ChannelShuffle(1),  # RGB三通道随机交换
            iaa.ElasticTransformation(alpha=0.1),
            iaa.Grayscale(alpha=(0.2, 0.5)),
            iaa.Pepper(p=0.03),
            iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)),
            iaa.Dropout(p=(0.03, 0.05)),
            iaa.Salt(p=(0.03, 0.05)),
            iaa.AverageBlur(k=(1, 3)),
            iaa.Add((-10, 10)),
            iaa.CoarseSalt(size_percent=0.01)
        ])
    seq_det = seq.to_deterministic()
    image_aug = seq_det.augment_images([image])[0]

    return image_aug
Пример #10
0
    def test_augment_images__different_hue_and_saturation__mixed_perchan(self):
        base_img = np.zeros((2, 2, 3), dtype=np.uint8)
        base_img[..., 0] += 20
        base_img[..., 1] += 40
        base_img[..., 2] += 60

        class _DummyParamValue(iap.StochasticParameter):
            def _draw_samples(self, size, random_state):
                arr = np.float32([10, 20])
                return np.tile(arr[np.newaxis, :], (size[0], 1))

        class _DummyParamPerChannel(iap.StochasticParameter):
            def _draw_samples(self, size, random_state):
                assert size == (3, )
                return np.float32([1.0, 0.0, 1.0])

        aug = iaa.AddToHueAndSaturation(value=_DummyParamValue(),
                                        per_channel=_DummyParamPerChannel())

        img_expected1 = self._add_hue_saturation(base_img,
                                                 value_hue=10,
                                                 value_saturation=20)
        img_expected2 = self._add_hue_saturation(base_img,
                                                 value_hue=10,
                                                 value_saturation=10)
        img_expected3 = self._add_hue_saturation(base_img,
                                                 value_hue=10,
                                                 value_saturation=20)

        img_observed1, img_observed2, img_observed3, = \
            aug.augment_images([base_img] * 3)

        assert np.array_equal(img_observed1, img_expected1)
        assert np.array_equal(img_observed2, img_expected2)
        assert np.array_equal(img_observed3, img_expected3)
Пример #11
0
    def __init__(self):
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)

        self.seq = iaa.Sequential(
            [
                iaa.GaussianBlur(
                    (0, 1.0)),  # blur images with a sigma between 0 and 1.0
                sometimes(
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5)),
                sometimes(iaa.Sharpen(
                    alpha=(0, 1.0), lightness=(0.75, 1.5))),  # sharpen images
                sometimes(iaa.Emboss(alpha=(0, 1.0),
                                     strength=(0, 1.0))),  # emboss images
                sometimes(
                    iaa.ContrastNormalization(
                        (0.5, 2.0),
                        per_channel=0.5)),  # improve or worsen the contrast
                sometimes(iaa.Grayscale(alpha=(0.0, 1.0))),
                sometimes(
                    # change brightness of images (by -10 to 10 of original value)
                    iaa.Add((-10, 10), per_channel=0.5)),
                sometimes(
                    # change hue and saturation
                    iaa.AddToHueAndSaturation((-20, 20))),
                sometimes(
                    # randomly remove up to 10% of the pixels
                    iaa.Dropout((0.01, 0.1), per_channel=0.5)),
            ],
            random_order=True)
def imgAug(inputImage, crop=True, flip=True, gaussianBlur=True, channelInvert=True, brightness=True, hueSat=True):
    augList = []
    if crop:
        augList += [iaa.Crop(px=(0, 16))]  # crop images from each side by 0 to 16px (randomly chosen)
    if flip:
        augList += [iaa.Fliplr(0.5)]  # horizontally flip 50% of the images
    if gaussianBlur:
        augList += [iaa.GaussianBlur(sigma=(0, 3.0))]  # blur images with a sigma of 0 to 3.0
    if channelInvert:
        augList += [iaa.Invert(0.05, per_channel=True)]  # invert color channels
    if brightness:
        augList += [iaa.Add((-10, 10), per_channel=0.5)]  # change brightness of images (by -10 to 10 of original value)
    if hueSat:
        augList += [iaa.AddToHueAndSaturation((-20, 20))]  # change hue and saturation
    seq = iaa.Sequential(augList)
    # seq = iaa.Sequential([
    #     iaa.Crop(px=(0, 16)),  # crop images from each side by 0 to 16px (randomly chosen)
    #     # iaa.Fliplr(0.5),  # horizontally flip 50% of the images
    #     iaa.GaussianBlur(sigma=(0, 3.0)),  # blur images with a sigma of 0 to 3.0
    #     iaa.Invert(0.05, per_channel=True),  # invert color channels
    #     iaa.Add((-10, 10), per_channel=0.5),  # change brightness of images (by -10 to 10 of original value)
    #     iaa.AddToHueAndSaturation((-20, 20)),  # change hue and saturation
    # ])

    image_aug = seq.augment_image(inputImage)
    return image_aug
 def train_augmentors(self):
     shape_augs = [
         iaa.Resize((512, 512), interpolation='nearest'),
         # iaa.CropToFixedSize(width=800, height=800),
     ]
     #
     sometimes = lambda aug: iaa.Sometimes(0.2, aug)
     input_augs = [
         iaa.OneOf([
             iaa.GaussianBlur((0, 3.0)),  # gaussian blur with random sigma
             iaa.MedianBlur(k=(3, 5)),  # median with random kernel sizes
             iaa.AdditiveGaussianNoise(loc=0,
                                       scale=(0.0, 0.05 * 255),
                                       per_channel=0.5),
         ]),
         sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
         # move pixels locally around (with random strengths)
         sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))
                   ),  # sometimes move parts of the image around
         sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))),
         iaa.Sequential([
             iaa.Add((-26, 26)),
             iaa.AddToHueAndSaturation((-20, 20)),
             iaa.LinearContrast((0.75, 1.25), per_channel=1.0),
         ],
                        random_order=True),
         sometimes([
             iaa.CropAndPad(percent=(-0.05, 0.1),
                            pad_mode="reflect",
                            pad_cval=(0, 255)),
         ]),
     ]
     return shape_augs, input_augs
Пример #14
0
  def __init__(self):
    sometimes = lambda aug: iaa.Sometimes(0.3, aug)

    self.aug = iaa.Sequential(iaa.SomeOf((1, 5), 
        [
        # blur

        sometimes(iaa.OneOf([iaa.GaussianBlur(sigma=(0, 1.0)),
                            iaa.MotionBlur(k=3)])),
        
        # color
        sometimes(iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)),
        sometimes(iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True)),
        sometimes(iaa.Invert(0.25, per_channel=0.5)),
        sometimes(iaa.Solarize(0.5, threshold=(32, 128))),
        sometimes(iaa.Dropout2d(p=0.5)),
        sometimes(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        sometimes(iaa.Add((-40, 40), per_channel=0.5)),

        sometimes(iaa.JpegCompression(compression=(5, 80))),
        
        # distort
        sometimes(iaa.Crop(percent=(0.01, 0.05), sample_independently=True)),
        sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.01))),
        sometimes(iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), 
#                            rotate=(-5, 5), shear=(-5, 5), 
                            order=[0, 1], cval=(0, 255), 
                            mode=ia.ALL)),
        sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))),
        sometimes(iaa.OneOf([iaa.Dropout(p=(0, 0.1)),
                            iaa.CoarseDropout(p=(0, 0.1), size_percent=(0.02, 0.25))])),

    ],
        random_order=True),
    random_order=True)
Пример #15
0
def aug_image(image, is_infer=False, augment=None):
    if is_infer:
        flip_code = augment[0]

        if flip_code == 1:
            seq = iaa.Sequential([iaa.Fliplr(1.0)])
        elif flip_code == 2:
            seq = iaa.Sequential([iaa.Flipud(1.0)])
        elif flip_code == 3:
            seq = iaa.Sequential([iaa.Flipud(1.0), iaa.Fliplr(1.0)])
        elif flip_code == 0:
            return image

    else:

        seq = iaa.Sequential([
            iaa.Affine(rotate=(-15, 15), shear=(-15, 15), mode='edge'),
            iaa.SomeOf(
                (0, 2),
                [
                    iaa.GaussianBlur((0, 1.5)),
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5),
                    iaa.AddToHueAndSaturation(
                        (-5, 5)),  # change hue and saturation
                    iaa.PiecewiseAffine(scale=(0.01, 0.03)),
                    iaa.PerspectiveTransform(scale=(0.01, 0.1))
                ],
                random_order=True)
        ])

    image = seq.augment_image(image)
    return image
Пример #16
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
Пример #17
0
    def __init__(self, settings, show_image=False, jitter=0, input_size=(416,416), instances_path="", labels_path="", augmentate=True):
        self.settings = settings
        self.show_image = show_image
        self.jitter = jitter
        self.input_size = input_size
        self.instances_path = instances_path
        self.labels_path = labels_path
        self.classes = self.settings["CLASSES_LABEL"]
        self.augmentate_flag = augmentate
        self.augmentate = iaa.Sequential(
            [
                iaa.Add((-20, 20)),
                iaa.ContrastNormalization((0.8, 1.6)),
                iaa.AddToHueAndSaturation((-21, 21)),
            ],
            random_order=False
        )

        self.augmentate_off = iaa.Sequential(
            [
                #iaa.Sharpen(alpha=(0.4, 0.4)),
                #iaa.ContrastNormalization((0.8, 0.8), per_channel=1.0),
                # iaa.Add((-50, 50)),
                #iaa.Multiply((0.8, 0.8))
            ],
            random_order=False
        )
Пример #18
0
 def test___init___per_channel(self):
     aug = iaa.AddToHueAndSaturation(per_channel=0.5)
     assert aug.value is None
     assert aug.value_hue is None
     assert aug.value_saturation is None
     assert isinstance(aug.per_channel, iap.Binomial)
     assert np.isclose(aug.per_channel.p.value, 0.5)
Пример #19
0
    def test_augment_images__value_hue_and_value_saturation(self):
        base_img = np.zeros((2, 2, 3), dtype=np.uint8)
        base_img[..., 0] += 20
        base_img[..., 1] += 40
        base_img[..., 2] += 60

        class _DummyParam(iap.StochasticParameter):
            def _draw_samples(self, size, random_state):
                return np.float32([10, 20, 30])

        aug = iaa.AddToHueAndSaturation(value_hue=_DummyParam(),
                                        value_saturation=_DummyParam() + 40)

        img_expected1 = self._add_hue_saturation(base_img,
                                                 value_hue=10,
                                                 value_saturation=40 + 10)
        img_expected2 = self._add_hue_saturation(base_img,
                                                 value_hue=20,
                                                 value_saturation=40 + 20)
        img_expected3 = self._add_hue_saturation(base_img,
                                                 value_hue=30,
                                                 value_saturation=40 + 30)

        img_observed1, img_observed2, img_observed3 = \
            aug.augment_images([base_img] * 3)

        assert np.array_equal(img_observed1, img_expected1)
        assert np.array_equal(img_observed2, img_expected2)
        assert np.array_equal(img_observed3, img_expected3)
Пример #20
0
def MarkerAug():
    """
    Marker arugmentatio that does not include noise as that augmentation is applied much later.
    :return:
    """
    sometimes = lambda aug: iaa.Sometimes(0.8, aug)
    aug_seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontally flip 50% of the images
            iaa.Flipud(0.5),  # horizontally flip 50% of the images
            iaa.PerspectiveTransform(scale=(0.01, 0.1)),
            iaa.Multiply((0.25, 1.75), per_channel=True),
            iaa.AddToHueAndSaturation((-25, 25)),
            iaa.Affine(
                scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                # scale images to 80-120% of their size, individually per axis
                # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},  # translate by -20 to +20 percent (per axis)
                rotate=(-180, 180),  # rotate by -45 to +45 degrees
                shear=(-16, 16),  # shear by -16 to +16 degrees
            )
        ]
    )


    return aug_seq
Пример #21
0
def train(model, args, config):
    """Train the model."""
    # Training dataset.
    dataset_train = LegoDataset()
    dataset_train.load_lego(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = LegoDataset()
    dataset_val.load_lego(args.dataset, "val")
    dataset_val.prepare()

    # MW add augmentation like I did in Reginanet
    if args.enable_augmentation:
        print('Augmentation Enabled.')
        # Little augmentation (3)
        augmentation = iaa.Sometimes(0.8, [
            iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.015 * 255)),
            iaa.Add((-10, 10)),                                             # change brightness of images (by -10 to 10 RGB value of original value)
            iaa.AddToHueAndSaturation((-10, 10))                            # change hue and saturation
        ])

        # Normal augmentation (1) -> soweit schlechtere Resultate als mit "Little"
        # augmentation = iaa.Sometimes(0.8, [
        #     iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03 * 255)),
        #     iaa.Add((-40, 40)),                                             # change brightness of images (by -40 to 40 RGB value of original value)
        #     iaa.AddToHueAndSaturation((-50, 50))                            # change hue and saturation
        # ])
        
    else:
        augmentation = None

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.

    # Train all
    if config.USE_STAGE_TWO and config.USE_RPN_ROIS:
        layers = 'all'

    # Train MRCNN only
    elif config.USE_STAGE_TWO and not config.USE_RPN_ROIS:
        layers = 'mrcnn_only'

    # Train RPN only
    elif not config.USE_STAGE_TWO and config.USE_RPN_ROIS:
        
        layers = 'rpn_only'

    else:
        assert('No valid layer training configuration.')

    print("Training '{}' network".format(layers))

    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=config.NB_OF_EPOCHS,
                layers=layers,
                augmentation=augmentation)
Пример #22
0
def aug_iamges_backgroud(images,backgrouds):
  # images and heatmaps, just arrays filled with value 30
  #images = np.ones((16, 128, 128, 3), dtype=np.uint8) * 30
  #heatmaps = np.ones((16, 128, 128, 21), dtype=np.uint8) * 30

  # add vertical lines to see the effect of flip
  #images[:, 16:128-16, 120:124, :] = 120
  #heatmaps[:, 16:128-16, 120:124, :] = 120
  sometimes = lambda aug: iaa.Sometimes(0.5, aug)
  seq = iaa.Sequential([
    iaa.Fliplr(0.5, name="Flipper"),
    sometimes(iaa.GaussianBlur((0, 3.0), name="GaussianBlur")),

    sometimes(iaa.Add((-10, 10), per_channel=0.5, name='add')),  # change brightness of images (by -10 to 10 of original value)
    sometimes(iaa.AddToHueAndSaturation((-20, 20), name='addhue')),  # change hue and saturation
    sometimes(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5, name='contrast')),  # improve or worsen the contrast
  ])

# change the activated augmenters for heatmaps,
# we only want to execute horizontal flip, affine transformation and one of
# the gaussian noises
  def activator_heatmaps(images, augmenter, parents, default):
       if augmenter.name in ["GaussianBlur", "add", "contrast", "addhue"]:
            return False
       else:
          # default value for all other augmenters
            return default
  hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps)

  seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start
  images_aug = seq_det.augment_images(images)
  backgrouds_aug = seq_det.augment_images(backgrouds, hooks=hooks_heatmaps)
  return images_aug, backgrouds_aug
Пример #23
0
    def __init__(self):
        """
            init Constructor

            @param self Object.
        """
        self.aug = iaa.Sequential([
            # Blur or Sharpness
            iaa.Sometimes(
                0.25,
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0, 1.0)),
                    iaa.pillike.EnhanceSharpness(factor=(0.8, 1.5))
                ])),
            # Flip horizontally
            iaa.Fliplr(0.5),
            # Rotation
            iaa.Rotate((-20, 20)),
            # Pixel Dropout
            iaa.Sometimes(
                0.25,
                iaa.OneOf([
                    iaa.Dropout(p=(0, 0.1)),
                    iaa.CoarseDropout(0.1, size_percent=0.5)
                ])),
            # Color
            iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True),
        ])
def customizedImgAug(input_img):
    rarely = lambda aug: iaa.Sometimes(0.1, aug)
    sometimes = lambda aug: iaa.Sometimes(0.25, aug)
    often = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential([
        iaa.Fliplr(0.5),
        often(
            iaa.Affine(
                scale={
                    "x": (0.9, 1.1),
                    "y": (0.9, 1.1)
                },
                translate_percent={
                    "x": (-0.1, 0.1),
                    "y": (-0.12, 0)
                },
                rotate=(-10, 10),
                shear=(-8, 8),
                order=[0, 1],
                cval=(0, 255),
            )),
        iaa.SomeOf((0, 4), [
            rarely(iaa.Superpixels(p_replace=(0, 0.3), n_segments=(20, 200))),
            iaa.OneOf([
                iaa.GaussianBlur((0, 2.0)),
                iaa.AverageBlur(k=(2, 4)),
                iaa.MedianBlur(k=(3, 5)),
            ]),
            iaa.Sharpen(alpha=(0, 0.3), lightness=(0.75, 1.5)),
            iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.5)),
            rarely(
                iaa.OneOf([
                    iaa.EdgeDetect(alpha=(0, 0.3)),
                    iaa.DirectedEdgeDetect(alpha=(0, 0.7),
                                           direction=(0.0, 1.0)),
                ])),
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            iaa.OneOf([
                iaa.Dropout((0.0, 0.05), per_channel=0.5),
                iaa.CoarseDropout(
                    (0.03, 0.05), size_percent=(0.01, 0.05), per_channel=0.2),
            ]),
            rarely(iaa.Invert(0.05, per_channel=True)),
            often(iaa.Add((-40, 40), per_channel=0.5)),
            iaa.Multiply((0.7, 1.3), per_channel=0.5),
            iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
            iaa.Grayscale(alpha=(0.0, 1.0)),
            sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))),
            sometimes(iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25)),
        ],
                   random_order=True),
        iaa.Fliplr(0.5),
        iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)
    ],
                         random_order=True)  # apply augmenters in random order

    output_img = seq.augment_image(input_img)
    return output_img
Пример #25
0
    def __init__(self, scale_size, crop_size):
        self.aug = iaa.Sequential([
            iaa.Scale((scale_size, scale_size)),
            iaa.Sometimes(
                0.25,
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0, 3.0)),
                    iaa.AverageBlur(k=(1, 5)),
                    iaa.MedianBlur(k=(1, 5))
                ])),
            iaa.Fliplr(p=0.5),
            iaa.Affine(rotate=(-20, 20), mode='symmetric'),
            iaa.Sometimes(
                0.25,
                iaa.OneOf([
                    iaa.Dropout(p=(0, 0.1)),
                    iaa.Add((-20, 20)),
                    iaa.SaltAndPepper(p=0.01),
                ])),
            iaa.Sometimes(
                0.25,
                iaa.OneOf([
                    iaa.AddToHueAndSaturation(value=(-10, 10),
                                              per_channel=True),
                    iaa.Grayscale(alpha=(0, 1.0))
                ])),
            iaa.GammaContrast(gamma=(0.5, 1.5))
        ])

        self.crop_size = crop_size
Пример #26
0
def get_augmentation_sequence():
    """
    Define the augmentation for training
    """
    # Macro to apply something with 50% chance
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)  # 50%
    rarely = lambda aug: iaa.Sometimes(0.1, aug)  # 10%

    # Augmentation applied to every image
    # Augmentors sampled one value per channel
    aug_sequence = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5),  # vertically flip 50% of all images

            # crop images by -0.25% to 0.25% of their height/width
            # positive values crop the image, negative pad
            sometimes(
                iaa.CropAndPad(
                    percent=(-0.25, 0.25),
                    pad_mode=['constant', 'edge'
                              ],  # pad with constant value of the edge value
                    pad_cval=(
                        0, 0
                    )  # if mode is constant, use a cval between 0 and 0 to ensure mask background is preserved
                )),
            sometimes(
                iaa.Affine(
                    scale={
                        "x": (0.8, 1.2),
                        "y": (0.8, 1.2)
                    },  # scale images to 80-120% of their size, individually per axis
                    translate_percent={
                        "x": (-0.2, 0.2),
                        "y": (-0.2, 0.2)
                    },  # translate by -20 to +20 percent (per axis)
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    order=[
                        0,
                        1
                    ],  # use nearest neighbour or bilinear interpolation (fast)
                    cval=(
                        0, 0
                    ),  # if mode is constant, use a cval between 0 and 0 to ensure mask background is preserved
                    mode=
                    'constant'  # ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            # rarely(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
            iaa.GaussianBlur((0, 3.0)),
            iaa.Add(
                (-10, 10), per_channel=0.7
            ),  # change brightness of images (by -10 to 10 of original value)
            iaa.AddToHueAndSaturation((-20, 20)),
            # sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
        ],
        random_order=True)

    return aug_sequence
Пример #27
0
def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = CustomDataset()
    dataset_train.load_custom(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = CustomDataset()
    dataset_val.load_custom(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    augmentation = iaa.Sequential([
        iaa.AddToHueAndSaturation(value=(-20, 20), per_channel=True),
        iaa.Fliplr(0.5),
        iaa.Affine(rotate=(-20, 20), mode='constant'),
    ])
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=40,
                layers='heads',
                augmentation=augmentation)
Пример #28
0
    def augmentation(self):
        aug = iaa.Sequential(
            [
                # iaa.Scale((224, 224)),
                iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 1.5))),
                # iaa.Fliplr(0.5),

                # iaa.Sometimes(0.5, iaa.Affine(

                #     # translate by -20 to +20 percent (per axis)
                #     translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
                #     rotate=(-45, 45),  # rotate by -45 to +45 degrees
                #     shear=(-16, 16),  # shear by -16 to +16 degrees

                # )),
                iaa.Sometimes(
                    0.401,
                    iaa.OneOf([
                        iaa.Dropout(p=(0, 0.0071)),
                        iaa.CoarseDropout(0.041, size_percent=0.5)
                    ])),
                iaa.Sometimes(
                    0.3,
                    iaa.AddToHueAndSaturation(value=(-40, 40),
                                              per_channel=True)),
                # iaa.Sometimes(0.3, iaa.PerspectiveTransform(
                #     scale=(0.061, 0.071))),
                iaa.Sometimes(0.3, iaa.Add((-10, 10), per_channel=0.5)),
                iaa.Sometimes(
                    0.1,
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.007 * 255), per_channel=0.5)),
            ],
            random_order=True)
        return aug
Пример #29
0
def img_aug(img_name, base_dir):
    image = mpimg.imread(Path(base_dir, 'datasets', 'images', img_name))

    ran = random.randint(1, 5)
    if ran == 1:
        aug = iaa.Sequential([
            iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"),
            iaa.AddToHueAndSaturation((-60, 60)),
            iaa.ElasticTransformation(alpha=90, sigma=9),
            iaa.Cutout()
        ],
                             random_order=True)
        image = aug(image=image)
    elif ran == 2:
        aug = iaa.BlendAlphaRegularGrid(nb_rows=2,
                                        nb_cols=2,
                                        foreground=iaa.Multiply(0.0),
                                        background=iaa.AveragePooling(8),
                                        alpha=[0.0, 0.0, 1.0])
        image = aug(image=image)

    elif ran == 3:
        image = tf.image.adjust_brightness(image, 0.4)
    elif ran == 4:
        image = tf.image.random_flip_left_right(image)
        image = tf.image.rot90(image)
        image = tf.image.random_flip_up_down(image)
    elif ran == 5:
        image = tf.image.central_crop(image, central_fraction=0.5)
    return image
Пример #30
0
def data_aug_v2(image, label, prob=0.5):
    image = image.numpy()
    aug = iaa.Sequential([
        iaa.Sometimes(
            prob,
            iaa.OneOf(
                [iaa.Multiply((0.4, 1.5)),
                 iaa.GammaContrast((0.3, 2.0))])),
        iaa.Sometimes(
            prob,
            iaa.OneOf([
                iaa.Grayscale((0, 0.3)),
                iaa.ContrastNormalization((0.3, 2.5)),
                iaa.AddToHueAndSaturation((-50, 50))
            ])),
        iaa.Sometimes(
            prob,
            iaa.OneOf([
                iaa.AverageBlur(k=((1, 4), (1, 4))),
                iaa.GaussianBlur(sigma=(0.0, 1.5)),
                iaa.MedianBlur(k=(1, 5)),
                iaa.MotionBlur(k=[5, 19], angle=(0, 360))
            ])),
        iaa.Sometimes(
            prob, iaa.AdditiveGaussianNoise(scale=(0, 10), per_channel=0.5))
    ])

    image = aug.augment_image(image)
    image = (image / 255).astype(dtype=np.float32)
    return image, label