示例#1
0
 def test_failure_on_invalid_datatype_for_strength(self):
     # don't use assertRaisesRegex, because it doesnt exist in 2.7
     got_exception = False
     try:
         _ = iaa.Emboss(alpha=1.0, strength="test")
     except Exception as exc:
         assert "Expected " in str(exc)
         got_exception = True
     assert got_exception
 def build_augmentor(self):
     self.augmentor = iaa.Sometimes(
         0.5,
         iaa.OneOf([
             iaa.ChannelShuffle(p=1.0),
             iaa.Invert(p=1.0, per_channel=True),
             iaa.AddToHueAndSaturation((-45, 45), per_channel=True),
             iaa.Emboss(alpha=1, strength=(0.5, 1.0))
         ]))
def data_augmentation(ratio, images):
    '''
    Augmentation of images [batch_size, h, w, channel]
    :param ratio: the number of augmentation of each image
    :param images:
    :return: [batch_size * ratio, h, w, channel], normed
    '''
    images_aug = None
    seq = []
    st = lambda aug: iaa.Sometimes(0.3, aug)
    for i in range(ratio):
        seq_one = iaa.Sequential([
            # iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            # iaa.Flipud(0.5),  # vertically flip 50% of all images
            # st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
            # convert images into their superpixel representation
            st(iaa.Crop(percent=(0, 0.1))),  # crop images by 0-10% of their height/width
            # st(iaa.GaussianBlur((0, 3.0))),  # blur images with a sigma between 0 and 3.0
            st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),  # sharpen images
            st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))),  # emboss images
            # search either for all edges or for directed edges
            st(iaa.Sometimes(0.5,
                             iaa.EdgeDetect(alpha=(0, 0.7)),
                             iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
                             )),
            # st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)),  # add gaussian noise to images
            # st(iaa.Dropout((0.0, 0.1), per_channel=0.5)),  # randomly remove up to 10% of the pixels
            # st(iaa.Invert(0.25, per_channel=True)),  # invert color channels
            st(iaa.Add((-10, 10), per_channel=0.5)),  # change brightness of images (by -10 to 10 of original value)
            st(iaa.Multiply((0.5, 1.5), per_channel=0.5)),  # change brightness of images (50-150% of original value)
            st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)),  # improve or worsen the contrast
            st(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_px={"x": (-16, 16), "y": (-16, 16)},  # translate by -16 to +16 pixels (per axis)
                rotate=(-45, 45),  # rotate by -45 to +45 degrees
                shear=(-16, 16),  # shear by -16 to +16 degrees
                order=ia.ALL,  # use any of scikit-image's interpolation methods
                cval=(0, 255),  # if mode is constant, use a cval between 0 and 255
                mode=ia.ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
            )),
            # st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25))
            # apply elastic transformations with random strengths
        ],
            random_order=True  # do all of the above in random order
        )
        seq.append(seq_one)
        if i == 0:
            images_aug = seq[0].augment_images(images)
        else:
            aug_temp = seq[i].augment_images(images)
            images_aug = np.concatenate((images_aug, aug_temp), axis=0)

    print('Augmentation shape', images_aug.shape)
    images_aug = np.array(images_aug, dtype=np.float32)
    return images_aug/255
def emboss():
    import imgaug.augmenters as iaa

    cfg = base_cfg(inspect.currentframe().f_code.co_name)
    cfg.render_cfg.height = 48
    cfg.render_cfg.corpus_effects = Effects([
        Padding(p=1, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True),
        ImgAugEffect(aug=iaa.Emboss(alpha=(0.9, 1.0), strength=(1.5, 1.6))),
    ])
    return cfg
 def __init__(self):
     self._seq = iaa.Sequential([
         iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
         iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
         iaa.AdditiveGaussianNoise(loc=0,
                                   scale=(0.0, 0.05 * 255),
                                   per_channel=0.5),
         iaa.Invert(0.05, per_channel=True),
         iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
     ])
    def augmentor(self, images, targets):
        '''Augments each batch of data with random transformations'''
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)
        seq = iaa.Sequential([
            iaa.Fliplr(0.5, name="Fliplr"),
            iaa.Flipud(0.5, name="Flipud"),
            sometimes(
                iaa.SomeOf((0, 2), [
                    iaa.Affine(translate_percent={
                        "x": (-0.1, 0.1),
                        "y": (-0.1, 0.1)
                    },
                               rotate=(-25, 25),
                               name="Affine"),
                    iaa.ElasticTransformation(alpha=(0.01, 0.1),
                                              sigma=0.15,
                                              name="ElasticTransformation"),
                    iaa.PiecewiseAffine(scale=(0.001, 0.03),
                                        name="PiecewiseAffine"),
                    iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                             name="PerspectiveTransform"),
                ],
                           random_order=True)),
            sometimes(
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0, 0.2)),
                    iaa.AverageBlur(k=3),
                    iaa.MedianBlur(k=3),
                ])),
            sometimes(
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255)),
                    iaa.AddElementwise((-5, 5)),
                ])),
            sometimes(
                iaa.OneOf([
                    iaa.GammaContrast(gamma=(0.75, 1.50)),
                    iaa.HistogramEqualization(),
                    iaa.Multiply((0.80, 1.15)),
                    iaa.Add((-20, 15)),
                    iaa.Sharpen(alpha=(0, 0.5), lightness=(0.7, 1.5)),
                    iaa.Emboss(alpha=(0, 0.5), strength=(0.7, 1.5)),
                ])),
        ],
                             random_order=True)

        seq_det = seq.to_deterministic()
        images = seq_det.augment_images(images)
        targets = seq_det.augment_segmentation_maps([
            ia.SegmentationMapOnImage(t.astype(bool), shape=t.shape)
            for t in targets
        ])
        targets = np.array([t.get_arr_int() for t in targets])

        return images, targets
示例#7
0
def load_aug():

    sometimes = lambda aug: iaa.Sometimes(0.3, aug)

    seq[0] = iaa.Sequential(
        [
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf(
                (0, 5),
                [
                    iaa.CropAndPad(percent=(-0.2, 0.2),
                                   pad_mode=ia.ALL,
                                   pad_cval=(0, 255)),
                    iaa.Crop(percent=0.25, keep_size=True),
                    iaa.OneOf([
                        iaa.GaussianBlur(
                            (0, 0.2
                             )),  # blur images with a sigma between 0 and 2.0
                        iaa.AverageBlur(k=(2,
                                           3)),  # blur image using local means
                        iaa.MedianBlur(
                            k=(1, 3)),  # blur image using local medians
                    ]),
                    iaa.Sharpen(alpha=(0, 0.5),
                                lightness=(0.75, 1.5)),  # sharpen images
                    iaa.Emboss(alpha=(0, 0.5),
                               strength=(0, 0.5)),  # emboss images
                    # search either for all edges or for directed edges,
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05 * 255),
                        per_channel=0.5),  # add gaussian noise to images
                    iaa.Add(
                        (-10, 10), per_channel=0.5
                    ),  # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation(
                        (-10, 10)),  # change hue and saturation
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.contrast.LinearContrast(
                        (0.5, 2.0),
                        per_channel=0.5),  # improve or worsen the contrast
                    iaa.Grayscale(alpha=(0.0, 0.5)),
                    iaa.AdditiveLaplaceNoise(scale=0.05 * 255),
                    iaa.AdditivePoissonNoise(lam=2),
                    iaa.Multiply(mul=(0.5, 1.5)),
                    iaa.Dropout(p=(0.1, 0.2)),
                    iaa.CoarseDropout(p=0.1, size_percent=0.05),
                    iaa.MotionBlur(k=3),
                    iaa.LinearContrast(),
                    iaa.AveragePooling(2)
                ],
                random_order=True)
        ],
        random_order=True)
示例#8
0
 def test_alpha_1_strength_stochastic_parameter(self):
     aug = iaa.Emboss(alpha=1.0, strength=iap.Choice([1.0, 2.5]))
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=1.0)
     expected2 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=2.5)
     assert (self._allclose(observed, expected1)
             or self._allclose(observed, expected2))
示例#9
0
def imgaug_emboss_example():
    return base_cfg(
        inspect.currentframe().f_code.co_name,
        corpus=get_char_corpus(),
        corpus_effects=Effects(
            [
                Padding(p=1, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True),
                ImgAugEffect(aug=iaa.Emboss(alpha=(0.9, 1.0), strength=(1.5, 1.6))),
            ]
        ),
    )
示例#10
0
 def test_alpha_stochastic_parameter_strength_1(self):
     aug = iaa.Emboss(alpha=iap.Choice([0.5, 1.0]), strength=1)
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=0.5,
                                                 strength=1)
     expected2 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=1)
     assert (self._allclose(observed, expected1)
             or self._allclose(observed, expected2))
def get_augmentations():
    # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
    # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    # Define our sequence of augmentation steps that will be applied to every image
    # All augmenters with per_channel=0.5 will sample one value _per image_
    # in 50% of all cases. In all other cases they will sample new values
    # _per channel_.
    seq = iaa.Sequential([
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                [
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                        iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                        iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                    # search either for all edges or for directed edges,
                    # blend the result with the original image using a blobby mask
                    iaa.SimplexNoiseAlpha(iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0.5, 1.0)),
                        iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                    ])),
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                    iaa.OneOf([
                        iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                        iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                    ]),
                    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
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.5, 1.5), per_channel=0.5),
                        iaa.FrequencyNoiseAlpha(
                            exponent=(-4, 0),
                            first=iaa.Multiply((0.5, 1.5), per_channel=True),
                            second=iaa.ContrastNormalization((0.5, 2.0))
                        )
                    ]),
                    iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                    sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                ],
                random_order=True
            )
        ],
        random_order=True
    )
    return seq
示例#12
0
 def prep_decor(self):
     if self.level < 1: return None
     if self.level < 2:
         return [iaa.SomeOf((0, 1), iaa.Multiply((0.9, 1.12)))]
     if self.level < 3:
         return [
             iaa.SomeOf((0, 2), [
                 iaa.Multiply((0.9, 1.12)),
                 iaa.ContrastNormalization((0.93, 1.1), per_channel=True),
                 iaa.Grayscale(alpha=(0.0, 0.14)),
                 iaa.AddToHueAndSaturation((-9, 9))
             ])
         ]
     if self.level < 4:
         return [
             iaa.SomeOf((0, 3), [
                 iaa.Multiply((0.9, 1.12)),
                 iaa.ContrastNormalization((0.93, 1.1), per_channel=True),
                 iaa.Grayscale(alpha=(0.0, 0.14)),
                 iaa.AddToHueAndSaturation((-9, 9))
             ]),
             iaa.SomeOf((0, 3), [
                 iaa.GaussianBlur((0, 0.3)),
                 iaa.Sharpen((0, 0.3), lightness=(0.95, 1.1)),
                 iaa.Emboss(alpha=(0, 0.2), strength=(0.9, 1.1))
             ])
         ]
     return [
         iaa.SomeOf((0, 4), [
             iaa.Multiply((0.9, 1.12)),
             iaa.ContrastNormalization((0.93, 1.1), per_channel=True),
             iaa.Grayscale(alpha=(0.0, 0.14)),
             iaa.AddToHueAndSaturation((-9, 9))
         ]),
         iaa.SomeOf((0, 3), [
             iaa.GaussianBlur((0, 0.6)),
             iaa.Sharpen((0, 0.3), lightness=(0.9, 1.1)),
             iaa.Emboss(alpha=(0, 0.3), strength=(0.9, 1.1))
         ])
     ]
示例#13
0
class Augmenter:
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq_geom = iaa.Sequential([
        sometimes(
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       translate_percent={
                           "x": (-0.08, 0.08),
                           "y": (-0.02, 0.02)
                       },
                       rotate=(-5, 5),
                       shear=(-6, 6),
                       order=[0],
                       mode='edge')),
        iaa.Sometimes(0.2, iaa.PiecewiseAffine(scale=(0.01, 0.05), order=0))
    ],
                              random_order=True)
    seq_color = iaa.Sequential(
        [
            iaa.SomeOf(
                (0, 4),
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(
                            (0, 3.0
                             )),  # blur images with a sigma between 0 and 3.0
                        iaa.AverageBlur(
                            k=(2, 7)
                        ),  # blur image using local means with kernel sizes between 2 and 7
                        iaa.MedianBlur(
                            k=(3, 11)
                        ),  # blur image using local medians with kernel sizes between 2 and 7
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0),
                                lightness=(0.75, 1.5)),  # sharpen images
                    iaa.Emboss(alpha=(0, 1.0),
                               strength=(0, 2.0)),  # emboss images
                    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
                    iaa.ContrastNormalization(
                        (0.5, 2.0),
                        per_channel=0.5),  # improve or worsen the contrast
                    iaa.Grayscale(alpha=(0.0, 1.0)),
                ],
                random_order=True)
        ],
        random_order=True)
示例#14
0
def DA_Emboss(inputimg):
    alpha = np.arange(0.0, 1.0, 0.125)
    strength = np.arange(0.3, 1.3, 0.125)
    assert inputimg.ndim in [2, 3], "input invalid! Please check input"
    ret = []
    for i in np.arange(len(alpha)):
        for j in np.arange(len(strength)):
            Name = "SharpenAL_" + str(alpha[i]) + "_" + str(strength[j])
            VALUE = str(alpha[i]) + "_" + str(strength[j])
            aug_img = iaa.Emboss(alpha=alpha[i], strength=strength[j]).augment_image(inputimg)
            ret.append((Name, VALUE, aug_img))
    assert len(ret) == 81, "DA_Sharpen output size not match!"
    return ret
示例#15
0
def pattern_augment(img):
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.2),  # vertically flip 20% of all images
           iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),  # sharpen images
           iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),  # emboss images
           ],
           random_order=True
           )
    return seq.augment_image(img)
 def __init__(self):
     self.seq = iaa.Sequential([
         iaa.SomeOf(2, [
             iaa.Superpixels(p_replace=0.5, n_segments=100),
             iaa.GaussianBlur(0, 2.0),
             iaa.BilateralBlur(5, sigma_color=250, sigma_space=250),
             iaa.Sharpen(alpha=1),
             iaa.Emboss(alpha=1),
             iaa.AdditiveGaussianNoise(scale=0.1 * 255),
             iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25),
             iaa.ContrastNormalization((0.5, 2.0))
         ],
                    random_order=True)
     ])
示例#17
0
def augment(batch):
    batch = preprocess_input(batch)
    sometimes = lambda aug: iaa.Sometimes(.5, aug)
    seq = iaa.Sequential([
        iaa.Fliplr(.5),
        sometimes(iaa.Affine(rotate=(-20, 20))),
        sometimes(iaa.AddToHueAndSaturation((-20, 20))),
        sometimes(iaa.GaussianBlur((0, 2.))),
        sometimes(iaa.ContrastNormalization((.5, 1.5), per_channel=True)),
        sometimes(iaa.Sharpen(alpha=(0, 1.), lightness=(.75, 1.5))),
        sometimes(iaa.Emboss(alpha=(0, 1.), strength=(0, 2.))),
        sometimes(iaa.Crop(px=(5, 15))),
    ])
    return seq.augment_images(batch)
示例#18
0
def augmentation():
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = 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 20% of all images

            # crop images by -10% to 10% of their height/width
            sometimes(
                iaa.CropAndPad(
                    percent=(-0.1, 0.1), pad_mode=ia.ALL, pad_cval=0)),
            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
                    order=[
                        0,
                        1
                    ],  # use nearest neighbour or bilinear interpolation (fast)
                    cval=0,  # if mode is constant, use a cval between 0 and 255
                    mode=ia.
                    ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf(
                (0, 4),
                [
                    iaa.Sharpen(alpha=(0, 1.0),
                                lightness=(0.9, 1.1)),  # sharpen images
                    iaa.Emboss(alpha=(0, 1.0),
                               strength=(0, 2.0)),  # emboss images
                    iaa.AdditiveGaussianNoise(
                        scale=(0, 0.05 * 255)),  # add gaussian noise to images
                    iaa.AddToHueAndSaturation(
                        (-1, 1))  # change hue and saturation
                ],
                random_order=True)
        ],
        random_order=True)
    return seq
def draw_single_sequential_images():
    image = misc.imresize(
        ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))

    st = lambda aug: iaa.Sometimes(0.3, aug)

    seq = iaa.Sequential([
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
        st(iaa.Crop(percent=(0, 0.1))),
        st(iaa.GaussianBlur((0, 3.0))),
        st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),
        st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))),
        st(
            iaa.Sometimes(
                0.5,
                iaa.EdgeDetect(alpha=(0, 0.7)),
                iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
            )),
        st(
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)),
        st(iaa.Dropout((0.0, 0.1), per_channel=0.5)),
        st(iaa.Invert(0.25, per_channel=True)),
        st(iaa.Add((-10, 10), per_channel=0.5)),
        st(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)),
        st(iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale")),
        st(
            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=[0, 1],
                       cval=(0, 255),
                       mode=ia.ALL)),
        st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25))
    ],
                         random_order=True)

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
示例#20
0
def chapter_augmenters_emboss():
    aug = iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5))
    run_and_save_augseq("emboss.jpg",
                        aug, [ia.quokka(size=(64, 64)) for _ in range(16)],
                        cols=8,
                        rows=2)

    #alphas = [1/8*i for i in range(8)]
    alphas = np.linspace(0, 1.0, num=8)
    run_and_save_augseq(
        "emboss_vary_alpha.jpg",
        [iaa.Emboss(alpha=alpha, strength=1.0)
         for alpha in alphas], [ia.quokka(size=(64, 64)) for _ in range(8)],
        cols=8,
        rows=1)

    #strength = [0.5+(0.5/8)*i for i in range(8)]
    strength = np.linspace(0.5, 1.5, num=8)
    run_and_save_augseq(
        "emboss_vary_strength.jpg",
        [iaa.Emboss(alpha=1.0, strength=strength) for strength in strength],
        [ia.quokka(size=(64, 64)) for _ in range(8)],
        cols=8,
        rows=1)
示例#21
0
    def __init__(self, p=1.0, alpha=(0, 9, 1.0), strength=(1.5, 1.6)):
        """

        Parameters
        ----------
        p:
        alpha:
            see imgaug `doc`_
        strength:
            see imgaug `doc`_


        .. _doc: https://imgaug.readthedocs.io/en/latest/source/api_augmenters_convolutional.html#imgaug.augmenters.convolutional.Emboss
        """
        super().__init__(p, iaa.Emboss(alpha=alpha, strength=strength))
示例#22
0
def augmentation(images, labels, n_transform=60):
    original_images = images.copy()
    result = list()

    transformations = []
    for i in range(n_transform):
        # include all possible changes
        transform = iaa.SomeOf(
            (1, 3),
            [
                iaa.Affine(rotate=(-35, 0), mode=ia.ALL, cval=(0, 255)),
                iaa.Affine(rotate=(0, 35), mode=ia.ALL, cval=(0, 255)),
                iaa.Affine(translate_percent={
                    "x": (-0.1, 0.1),
                    "y": (-0.2, 0.2)
                },
                           mode=ia.ALL,
                           cval=(0, 255)),
                iaa.Affine(scale={
                    "x": (0.7, 1.4),
                    "y": (0.7, 1.4)
                },
                           mode=ia.ALL,
                           cval=(0, 255)),
                iaa.Affine(shear=(-30, 30), mode=ia.ALL, cval=(0, 255)),
                # Blur
                iaa.GaussianBlur(sigma=(0.0, 2.0)),
                iaa.AverageBlur(k=3),
                iaa.MedianBlur(k=3),
                iaa.AdditiveGaussianNoise(scale=0.05 * 255),
                iaa.ElasticTransformation(alpha=(0, 3.0), sigma=0.4),
                # Others
                iaa.CropAndPad(percent=(-0.15, 0.15)),
                iaa.Dropout(p=(0.01, 0.02)),
                iaa.PiecewiseAffine(scale=(0.03, 0.05)),
                iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
                iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
            ],
            random_order=True)
        transformations.append(transform)

    for transform in transformations:
        aug_images = transform.augment_images(original_images)
        result.extend(aug_images)

    result = images + result
    labels = labels * (len(result) // len(labels))
    return result, labels
示例#23
0
文件: aug.py 项目: SoulDuck/EveryNN
def aug_lv3(images):
    seq = iaa.Sequential(
        [
            # Blur
            iaa.OneOf([
                iaa.GaussianBlur(sigma=(0, 0.5)),
                iaa.AverageBlur(k=(2, 7)),
                iaa.MedianBlur((3, 11))
            ]),
            iaa.OneOf([
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                iaa.Dropout((0.01, 0.1), per_channel=0.5),
                iaa.CoarseDropout(
                    (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
            ]),
            iaa.OneOf([
                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 , 0.2))
            ]),
            iaa.SomeOf(2, [
                iaa.Add((-40, 40), per_channel=0.5),
                iaa.AddElementwise((-40, 40), per_channel=0.5),
                iaa.Multiply((0.5, 1.5)),
                iaa.Multiply((0.5, 1.5), per_channel=0.5),
            ],
                       random_order=True),
            iaa.SomeOf((0, None), [
                iaa.OneOf([
                    iaa.ContrastNormalization((0.5, 1.5)),
                    iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
                ]),
            ]),
            iaa.Affine(scale=(0.8, 1.2),
                       translate_percent={
                           "x": (-0.1, 0.1),
                           "y": (-0.1, 0.1)
                       },
                       rotate=(-30, 30)),
            iaa.OneOf([
                iaa.Affine(shear=(0.01, 0.05)),
                iaa.PiecewiseAffine((0.01, 0.05))
            ]),
        ],
        random_order=True)
    augimgs = seq.augment_images(images)
    return augimgs
    def __init__(self):
        #Define imgaug.augmenters Sequential transforms
        sometimes = lambda aug: iaa.Sometimes(0.4, aug) # applies the given augmenter in 50% of all cases

        img_seq = iaa.Sequential([
             sometimes(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03*255), per_channel=0.5)),
             sometimes(iaa.ContrastNormalization((0.5, 2.0), per_channel=1),),
             sometimes(iaa.Sharpen(alpha=(0, 0.2), lightness=(0.1, 0.4))), # sharpen images
             sometimes(iaa.Emboss(alpha=(0, 0.3), strength=(0, 0.5))), # emboss images
            ],random_order=True)
        
        self.aug_pipeline = Compose([
            RandomBrightness(16), #make image brighter or darker
            RandomContrast(0.9, 1.1), #strengthen or weaken the contrast in each image
            ImgAugSequence(img_seq),
        ])
 def __call__(self, sample):
     image, mask = sample
     if np.random.uniform(0, 1) > 0.5:
         seq = iaa.Sequential(
             [
                 iaa.SomeOf(
                     (1, None),
                     [
                         iaa.AdditiveGaussianNoise(scale=(0, 0.2 * 255)),  # 添加高斯噪音
                         iaa.SomeOf(
                             (1, None),
                             [
                                 iaa.Sharpen(alpha=(0.1, 0.3), lightness=(0.7, 1.3)),  # 锐化
                                 iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),  # 浮雕效果
                                 iaa.Invert(0.05, per_channel=True),  # 5%的概率反转像素的强度,即原来的强度为v那么现在的就是255-v
                             ],
                             random_order=True  # 随机的顺序把这些操作用在图像上
                         ),
                         # 用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法
                         iaa.OneOf(
                             [
                                 iaa.GaussianBlur(sigma=(0, 1.0)),  # 高斯模糊
                                 iaa.MedianBlur(k=(3, 11)),  # 中值模糊
                                 iaa.AverageBlur(k=(2, 7)),  # 均值模糊。
                             ]
                         ),
                         iaa.OneOf(
                             [
                                 iaa.Multiply((0.5, 1.5), per_channel=0.5),  # 像素乘上0.5或者1.5之间的数字.
                                 iaa.Add((-10, 10), per_channel=0.5),  # 每个像素随机加减-10到10之间的数
                             ]
                         ),
                         sometimes(
                             iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)),  # 超像素的表示
                         ),
                         sometimes(
                             # 把像素移动到周围的地方。这个方法在mnist数据集增强中有见到
                             iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
                         ),
                         iaa.Grayscale(alpha=(0.0, 1.0)),  # 将RGB变成灰度图然后乘alpha加在原图上
                     ],
                     random_order=True  # 随机的顺序把这些操作用在图像上
                 ),
             ]
         )
         image = seq.augment_image(image)
     return image, mask
示例#26
0
    def _augment_image(self, image):
        """
            Augment a single image.
            Uses https://github.com/aleju/imgaug library
            Included in requirements (Do not attempt to manually install, use pip install - requirements.txt)

            Args
                image (np array): 3-dimensional np image array

            Returns
                Augmented image as 3-dimensional np image array
        """
        # Add label noise
        rand_int = random.randint(-5, 5)
        value = 0 if rand_int < 0 else rand_int

        seq = iaa.Sequential([
            iaa.SomeOf((0, 2)),
            iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.75)),  # emboss images
            iaa.OneOf([
                iaa.GaussianBlur(
                    (0, 2.0)),  # blur images with a sigma between 0 and 3.0
                iaa.AverageBlur(
                    k=(5, 7)
                ),  # blur image using local means with kernel sizes between 2 and 7
                iaa.MedianBlur(
                    k=(3, 11)
                ),  # blur image using local medians with kernel sizes between 2 and 7
            ]),
            iaa.OneOf([
                # either change the brightness of the whole image (sometimes
                # per channel) or change the brightness of subareas
                iaa.Multiply((0.8, 1.2), per_channel=0.5),
                iaa.AdditiveGaussianNoise(loc=0,
                                          scale=(0.0, 0.05 * 255),
                                          per_channel=0.5),
                # add gaussian noise to images
            ]),
            iaa.OneOf([
                iaa.Dropout(p=0.05, per_channel=True),
                iaa.Crop(px=(
                    0, value
                )),  # crop images from each side by 0 to 4px (randomly chosen)
            ]),
        ])

        return seq.augment_image(np.asarray(image))
示例#27
0
def augumentor(image):
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)  # 建立lambda表达式,
    seq = iaa.Sequential(
        [
            iaa.SomeOf(
                (0, 5),
                [
                    sometimes(iaa.GaussianBlur(sigma=(0, 0.5))),

                    # 锐化处理
                    sometimes(
                        iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),
                    iaa.Affine(rotate=(-1.5, 1.5)),

                    # 加入高斯噪声
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),

                    # 每个像素随机加减-10到10之间的数
                    iaa.Add((-10, 10)),

                    # 像素乘上0.5或者1.5之间的数字.
                    iaa.Multiply((0.75, 1.25)),

                    # 将整个图像的对比度变为原来的一半或者二倍
                    iaa.ContrastNormalization((0.6, 1.5)),

                    #                            改变某一通道的值
                    iaa.WithChannels(1, iaa.Add((10, 50))),

                    #                            灰度
                    iaa.Grayscale(alpha=(0.0, 1.0)),

                    #                            加钢印
                    iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),

                    #                            扭曲图像的局部区域
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03)))
                ],
                random_order=True  # 随机的顺序把这些操作用在图像上
            )
        ],
        random_order=True  # 随机的顺序把这些操作用在图像上
    )

    image_aug = seq.augment_image(image)
    return image_aug
示例#28
0
 def __init__(self,with_mask=True):
     self.with_mask = with_mask
     self.seq = iaa.Sequential(
         [
         iaa.SomeOf((0, 5),
             [
                 sometimes(iaa.Superpixels(p_replace=(0, 0.5), n_segments=(100, 200))), # convert images into their superpixel representation
                 iaa.OneOf([
                     iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                     iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                     iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                 ]),
                 iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                 iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                 # search either for all edges or for directed edges,
                 # blend the result with the original image using a blobby mask
                 iaa.SimplexNoiseAlpha(iaa.OneOf([
                     iaa.EdgeDetect(alpha=(0.5, 1.0)),
                     iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                 ])),
                 iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                 #iaa.OneOf([
                 #    iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                 #    iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                 #]),
                 iaa.Invert(0.05, per_channel=True), # invert color channels
                 iaa.Add((-5, 5), per_channel=0.5), # change brightness of images
                 iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                 # either change the brightness of the whole image (sometimes
                 # per channel) or change the brightness of subareas
                 iaa.OneOf([
                     iaa.Multiply((0.5, 1.5), per_channel=0.5),
                     iaa.FrequencyNoiseAlpha(
                         exponent=(-4, 0),
                         first=iaa.Multiply((0.5, 1.5), per_channel=True),
                         second=iaa.LinearContrast((0.5, 2.0))
                     )
                 ]),
                 iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                 iaa.Grayscale(alpha=(0.0, 1.0))
             ],
             random_order=True
         )
     ],
     random_order=True
 )
def train(model, dataset_dir, subset):
    """Train the model."""
    # Training dataset.
    dataset_train = myKidneyDataset()
    dataset_train.load_glomerulus(dataset_dir, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = myKidneyDataset()
    dataset_val.load_glomerulus(dataset_dir, "val")
    dataset_val.prepare()

    # Image augmentation
    # http://imgaug.readthedocs.io/en/latest/source/augmenters.html
    augmentation = iaa.SomeOf((0, 4), [
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.OneOf([iaa.Affine(rotate=90),
                   iaa.Affine(rotate=180),
                   iaa.Affine(rotate=270)]),
        iaa.Multiply((0.8, 1.5)),
        iaa.GaussianBlur(sigma=(0.0, 5.0)),
        iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
        iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))
    ])

    # *** This training schedule is an example. Update to your needs ***

    # If starting from imagenet, train heads only for a bit
    # since they have random weights
    print("Train network head")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=5,
                augmentation=augmentation,
                layers='heads')

    print("Train all layers")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=1000,
                augmentation=augmentation,
                layers='all')
def distortion(images_trans_aug):
    #ia.seed(1)
    seq = iaa.Sequential(
        [  #   iaa.Superpixels(p_replace=superpixel_p, n_segments=superpixel_n),
            iaa.GaussianBlur(sigma=gau_blur_size),
            iaa.AverageBlur(k=avg_blur_size),
            iaa.MedianBlur(k=med_blur_size),
            iaa.Sharpen(alpha=sharpen_alpha, lightness=sharpen_lightness),
            iaa.Emboss(alpha=emboss_alpha, strength=emboss_strength),
            #   iaa.Add(add_size, per_channel= add_per_channel_size),
            iaa.AdditiveGaussianNoise(scale=gaussian_noise, per_channel=0.2),
            #   iaa.Multiply(multiply_scale, per_channel=0.2),
            #   iaa.Dropout(p=dropout_p, per_channel=0.2),
            #   iaa.CoarseDropout(0.1, size_percent=coarse_dropout, per_channel=0.2)
        ],
        random_order=True)
    images_aug = seq(images=images_trans_aug)
    #    ia.imshow(ia.draw_grid(images_aug, cols=sample/8, rows=8))
    return images_aug