Example #1
0
    def randomDataAugument(self, num_trans):
        # 以下で定義する変換処理の内ランダムに幾つかの処理を選択
        seq = iaa.SomeOf(num_trans, [
            iaa.Affine(rotate=(-90, 90), order=1, mode="edge"),
            iaa.Fliplr(1.0),
            iaa.OneOf([
                # 同じ系統の変換はどれか1つが起きるように 1つにまとめる
                iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge"),
                iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge")
            ]),
            iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge"),
            iaa.OneOf([
                iaa.AdditiveGaussianNoise(scale=[0.05 * 255, 0.2 * 255]),
                iaa.AdditiveLaplaceNoise(scale=[0.05 * 255, 0.2 * 255]),
                iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True)
            ]),
            iaa.OneOf([
                iaa.LogContrast((0.5, 1.5)),
                iaa.LinearContrast((0.5, 2.0))
            ]),
            iaa.OneOf([
                iaa.GaussianBlur(sigma=(0.5, 1.0)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))
            ]),
            iaa.Invert(1.0)
        ], random_order=True)

        return seq
Example #2
0
 def __augmentation_operations(self):
     self.aug_ops = iaa.Sequential([
         self.__sometimes(iaa.Fliplr(1), 0.5),
         self.__sometimes(
             iaa.Affine(scale=iap.Uniform(1.0, 1.2).draw_samples(1)), 0.3),
         self.__sometimes(iaa.AdditiveGaussianNoise(scale=0.05 * 255), 0.2),
         self.__sometimes(
             iaa.OneOf([
                 iaa.CropAndPad(percent=(iap.Uniform(
                     0.0, 0.20).draw_samples(1)[0], iap.Uniform(
                         0.0, 0.20).draw_samples(1)[0]),
                                pad_mode=["constant"],
                                pad_cval=(0, 128)),
                 iaa.Crop(
                     percent=(iap.Uniform(0.0, 0.15).draw_samples(1)[0],
                              iap.Uniform(0.0, 0.15).draw_samples(1)[0]))
             ])),
         self.__sometimes(
             iaa.OneOf([
                 iaa.LogContrast(
                     gain=iap.Uniform(0.9, 1.2).draw_samples(1)),
                 iaa.GammaContrast(
                     gamma=iap.Uniform(1.5, 2.5).draw_samples(1))
             ]))
     ],
                                   random_order=True)
     return None
Example #3
0
def get_training_augmentation(**kwargs):
    seq = iaa.Sequential([
        iaa.Resize({
            'height': kwargs['crop_sz'],
            'width': kwargs['crop_sz']
        }),
        iaa.flip.Fliplr(p=0.5),
        iaa.OneOf(
            [iaa.GaussianBlur(sigma=(0.0, 1.0)),
             iaa.MotionBlur(k=(3, 5))]),
        iaa.OneOf([
            iaa.GammaContrast((0.8, 1.0)),
            iaa.LinearContrast((0.75, 1.5)),
            iaa.LogContrast((0.8, 1.0))
        ]),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        iaa.Crop(px=(0, 2 * (kwargs['crop_sz'] - kwargs['inp_sz']))),
        iaa.Resize({
            'height': kwargs['inp_sz'],
            'width': kwargs['inp_sz']
        })
    ])
    return seq
def chapter_augmenters_logcontrast():
    fn_start = "contrast/logcontrast"

    aug = iaa.LogContrast(gain=(0.4, 1.6))
    run_and_save_augseq(fn_start + ".jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.LogContrast(gain=(0.4, 1.6), per_channel=True)
    run_and_save_augseq(fn_start + "_per_channel.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)
Example #5
0
def data_aug(images):

    seq = iaa.Sometimes(
        0.5, iaa.Identity(),
        iaa.Sometimes(
            0.5,
            iaa.Sequential([
                iaa.Sometimes(
                    0.5,
                    iaa.OneOf([
                        iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                        iaa.AdditiveLaplaceNoise(scale=(0, 0.1 * 255)),
                        iaa.ReplaceElementwise(0.03, [0, 255]),
                        iaa.GaussianBlur(sigma=(0.0, 3.0)),
                        iaa.BilateralBlur(d=(3, 10),
                                          sigma_color=(10, 250),
                                          sigma_space=(10, 250))
                    ])),
                iaa.OneOf([
                    iaa.Add((-40, 40)),
                    iaa.AddElementwise((-20, 20)),
                    iaa.pillike.EnhanceBrightness()
                ]),
                iaa.OneOf([
                    iaa.GammaContrast((0.2, 2.0)),
                    iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                    iaa.LogContrast(gain=(0.6, 1.4)),
                    iaa.AllChannelsCLAHE(),
                    iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
                ])
            ])))

    images = seq(images=images)

    return images
Example #6
0
def data_gen(train_data: str, seg_data: str, batch_size: int,
             n_classes: int) -> (np.ndarray, np.ndarray):
    inputs = np.array(os.listdir(train_data))
    batch = len(inputs) // batch_size
    identity = np.identity(n_classes, dtype=np.int16)

    while True:
        shuffle = np.random.permutation(len(inputs))
        for b in np.array_split(inputs[shuffle], batch):
            imgs = []
            segs = []
            for img_file in b:
                img = Image.open(os.path.join(train_data,
                                              img_file)).convert("RGB")
                img = img.resize((224, 224))
                img = np.asarray(img)

                seq = iaa.Sequential([
                    iaa.GaussianBlur(sigma=(0, 3.0)),
                    iaa.LogContrast(gain=(0.5, 1.0)),
                    iaa.ChannelShuffle(p=1.0),
                ])
                img = seq.augment_image(img)
                img = img / 255.0
                imgs.append(img)

                seg = Image.open(
                    os.path.join(seg_data,
                                 img_file.split(".")[0] + "-seg.png"))
                seg = seg.resize((224, 224))
                seg = np.asarray(seg)
                seg = identity[seg].astype(np.float32)
                segs.append(seg)

            yield np.array(imgs), np.array(segs)
def imgaugRGB(img):

    print(img.shape)
    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((0, 2), [
                iaa.GaussianBlur((0.0, 2.0)),
                iaa.AverageBlur(k=(3, 7)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.BilateralBlur(d=(1, 7)),
                iaa.MotionBlur(k=(3, 7))
            ]),
            #color
            iaa.SomeOf(
                (0, 2),
                [
                    #iaa.WithColorspace(),
                    iaa.AddToHueAndSaturation((-20, 20)),
                    #iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                    iaa.Grayscale(alpha=(0.0, 0.2))
                ]),
            #brightness
            iaa.OneOf([
                iaa.Sequential([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.5, 1.5), per_channel=0.5)
                ]),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.Multiply((0.5, 1.5), per_channel=0.5),
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Multiply(
                                            (0.5, 1.5), per_channel=0.5),
                                        second=iaa.ContrastNormalization(
                                            (0.5, 2.0), per_channel=0.5))
            ]),
            #contrast
            iaa.SomeOf((0, 2), [
                iaa.GammaContrast((0.5, 1.5), per_channel=0.5),
                iaa.SigmoidContrast(
                    gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
            ]),
            #arithmetic
            iaa.SomeOf((0, 3), [
                iaa.AdditiveGaussianNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditiveLaplaceNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditivePoissonNoise(lam=(0, 8), per_channel=0.5),
                iaa.Dropout(p=(0, 0.05), per_channel=0.5),
                iaa.ImpulseNoise(p=(0, 0.05)),
                iaa.SaltAndPepper(p=(0, 0.05)),
                iaa.Salt(p=(0, 0.05)),
                iaa.Pepper(p=(0, 0.05))
            ]),
            #iaa.Sometimes(p=0.5, iaa.JpegCompression((0, 30)), None),
        ],
        random_order=True)
    return seq.augment_image(img)
def contrast():
    return iaa.OneOf([
        iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
        iaa.GammaContrast((0.5, 1.5), per_channel=0.5),
        iaa.HistogramEqualization(),
        iaa.LinearContrast((0.5, 1.5), per_channel=0.5),
        iaa.LogContrast((0.5, 1.5), per_channel=0.5),
        iaa.SigmoidContrast((5, 20), (0.25, 0.75), per_channel=0.5),
    ])
Example #9
0
def load_augmentations(flip=0.5,
                       blur=0.2,
                       crop=0.5,
                       contrast=0.3,
                       elastic=0.2,
                       affine=0.5):
    """
    Loads and configures data augmenter object

    Arguements:
    flip (float) -- probality of horizontal flip
    crop (float) -- probability of random crop
    blur (float) -- probability of gaussian blur
    contrast (float) -- probability of pixelwise color transformation
    elastic (float) -- probability of elastic distortion
    affine (float) -- probability of affine transform
    noise (float) -- probability of one of noises
    """
    aug = iaa.Sequential([
        iaa.Fliplr(flip),
        iaa.Sometimes(crop, iaa.Crop(px=(0, 20))),
        iaa.Sometimes(blur, iaa.GaussianBlur(sigma=(0.5, 5))),
        iaa.Sometimes(
            contrast,
            iaa.SomeOf((1, 5), [
                iaa.GammaContrast(per_channel=True, gamma=(0.25, 1.75)),
                iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=True),
                iaa.HistogramEqualization(to_colorspace="HLS"),
                iaa.LogContrast(gain=(0.5, 1.0)),
                iaa.CLAHE(clip_limit=(1, 10))
            ]),
        ),
        iaa.Sometimes(
            elastic,
            iaa.OneOf([
                iaa.ElasticTransformation(alpha=20, sigma=1),
                iaa.ElasticTransformation(alpha=200, sigma=20)
            ])),
        iaa.Sometimes(
            affine,
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       rotate=(-30, 30),
                       order=[0, 1]))
    ])
    return aug
Example #10
0
def get_common_augmentation(inp_hw):
    seq = iaa.Sequential([
        iaa.GaussianBlur(sigma=(0.0, 0.5)),
        iaa.OneOf([
            iaa.GammaContrast((0.8, 1.0)),
            iaa.LinearContrast((0.75, 1.5)),
            iaa.LogContrast((0.8, 1.0))
        ]),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.02 * 255),
                                  per_channel=0.5),
        iaa.Resize({
            'height': inp_hw[0],
            'width': inp_hw[1]
        }),
    ])
    return seq
Example #11
0
def main():
    parser = argparse.ArgumentParser(description="Contrast check script")
    parser.add_argument("--per_channel", dest="per_channel", action="store_true")
    args = parser.parse_args()

    augs = []
    for p in [0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("GammaContrast " + str(p), iaa.GammaContrast(p, per_channel=args.per_channel)))

    for cutoff in [0.25, 0.5, 0.75]:
        for gain in [5, 10, 15, 20, 25]:
            augs.append(("SigmoidContrast " + str(cutoff) + " " + str(gain), iaa.SigmoidContrast(gain, cutoff, per_channel=args.per_channel)))

    for gain in [0.0, 0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LogContrast " + str(gain), iaa.LogContrast(gain, per_channel=args.per_channel)))

    for alpha in [-1.0, 0.5, 0, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LinearContrast " + str(alpha), iaa.LinearContrast(alpha, per_channel=args.per_channel)))

    augs.append(("AllChannelsHistogramEqualization", iaa.AllChannelsHistogramEqualization()))
    augs.append(("HistogramEqualization (Lab)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.Lab)))
    augs.append(("HistogramEqualization (HSV)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HSV)))
    augs.append(("HistogramEqualization (HLS)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HLS)))

    for clip_limit in [0.1, 1, 5, 10]:
        for tile_grid_size_px in [3, 7]:
            augs.append(("AllChannelsCLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px),
                         iaa.AllChannelsCLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px,
                                              per_channel=args.per_channel)))

    for clip_limit in [1, 5, 10, 100, 200]:
        for tile_grid_size_px in [3, 7, 15]:
            augs.append(("CLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px),
                         iaa.CLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px)))

    images = [data.astronaut()] * 16
    images = ia.imresize_many_images(np.uint8(images), (128, 128))
    for name, aug in augs:
        print("-----------")
        print(name)
        print("-----------")
        images_aug = aug.augment_images(images)
        images_aug[0] = images[0]
        grid = ia.draw_grid(images_aug, rows=4, cols=4)
        ia.imshow(grid)
Example #12
0
    def randomTestAugument(self, NUM_TRANS):
        # 本番環境の危機から取得したデータにノイズが乗っていた状態を想定して変換
        seq = iaa.SomeOf(
            NUM_TRANS,
            [
                iaa.Affine(rotate=(-90, 90), order=1, mode="edge"),
                iaa.Fliplr(1.0),
                iaa.OneOf([
                    # 同じ系統の変換はどれか1つが起きるように 1つにまとめる
                    iaa.Affine(translate_percent={"x": (-0.125, 0.125)},
                               order=1,
                               mode="edge"),
                    iaa.Affine(translate_percent={"y": (-0.125, 0.125)},
                               order=1,
                               mode="edge")
                ]),
                iaa.Affine(scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
                           order=1,
                           mode="edge"),
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(scale=[0.05 * 255, 0.2 * 255]),
                    iaa.AdditiveLaplaceNoise(scale=[0.05 * 255, 0.2 * 255]),
                    iaa.AdditivePoissonNoise(lam=(16.0, 48.0),
                                             per_channel=True)
                ]),
                iaa.OneOf([
                    iaa.LogContrast((0.5, 1.5)),
                    iaa.LinearContrast((0.5, 2.0))
                ]),
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0.5, 1.0)),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))
                ]),
                # iaa.Invert(1.0)  # 色反転は故障しすぎでしょう..
            ],
            random_order=True)

        return seq
Example #13
0
def augmentRGB_V3(img):

    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((1, 2), [
                iaa.Sometimes(0.5, iaa.GaussianBlur(1.5)),
                iaa.Sometimes(0.25, iaa.AverageBlur(k=(3, 7))),
                iaa.Sometimes(0.25, iaa.MedianBlur(k=(3, 7))),
                iaa.Sometimes(0.25, iaa.BilateralBlur(d=(1, 7))),
                iaa.Sometimes(0.25, iaa.MotionBlur(k=(3, 7))),
            ]),
            iaa.Sometimes(0.25, iaa.Add((-25, 25), per_channel=0.3)),
            iaa.Sometimes(0.25, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
            iaa.Sometimes(
                0.25, iaa.ContrastNormalization((0.4, 2.3), per_channel=0.3)),

            #iaa.Sometimes(0.25, iaa.AddToHueAndSaturation((-15, 15))),
            #iaa.Sometimes(0.25, iaa.Grayscale(alpha=(0.0, 0.2))),
            iaa.Sometimes(
                0.25,
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Add(
                                            (-25, 25), per_channel=0.3),
                                        second=iaa.Multiply(
                                            (0.6, 1.4), per_channel=0.3)),
                iaa.SomeOf((0, 1), [
                    iaa.GammaContrast((0.75, 1.25), per_channel=0.5),
                    iaa.SigmoidContrast(
                        gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                    iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                    iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
                ]),
            ),
        ],
        random_order=True)
    return seq.augment_image(img)
Example #14
0
def main():
    parser = argparse.ArgumentParser(description="Contrast check script")
    parser.add_argument("--per_channel",
                        dest="per_channel",
                        action="store_true")
    args = parser.parse_args()

    augs = []
    for p in [0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("GammaContrast " + str(p),
                     iaa.GammaContrast(p, per_channel=args.per_channel)))

    for cutoff in [0.25, 0.5, 0.75]:
        for gain in [5, 10, 15, 20, 25]:
            augs.append(("SigmoidContrast " + str(cutoff) + " " + str(gain),
                         iaa.SigmoidContrast(gain,
                                             cutoff,
                                             per_channel=args.per_channel)))

    for gain in [-1.0, 0.0, 0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LogContrast " + str(gain),
                     iaa.LogContrast(gain, per_channel=args.per_channel)))

    for alpha in [-1.0, 0.5, 0, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LinearContrast " + str(alpha),
                     iaa.LinearContrast(alpha, per_channel=args.per_channel)))

    images = [data.astronaut()] * 16
    images = ia.imresize_many_images(np.uint8(images), (128, 128))
    for name, aug in augs:
        print("-----------")
        print(name)
        print("-----------")
        images_aug = aug.augment_images(images)
        grid = ia.draw_grid(images_aug, rows=4, cols=4)
        ia.imshow(grid)
Example #15
0
def test_LogContrast():
    reseed()

    img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    img = np.uint8(img)
    img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # check basic functionality with gain=1 or 2 (deterministic) and per_chanenl on/off (makes
    # no difference due to deterministic gain)
    for per_channel in [False, 0, 0.0, True, 1, 1.0]:
        for gain in [1, 2]:
            aug = iaa.LogContrast(gain=iap.Deterministic(gain),
                                  per_channel=per_channel)
            img_aug = aug.augment_image(img)
            img3d_aug = aug.augment_image(img3d)
            assert img_aug.dtype.type == np.uint8
            assert img3d_aug.dtype.type == np.uint8
            assert np.array_equal(img_aug,
                                  skimage.exposure.adjust_log(img, gain=gain))
            assert np.array_equal(
                img3d_aug, skimage.exposure.adjust_log(img3d, gain=gain))

    # check that tuple to uniform works
    aug = iaa.LogContrast((1, 2))
    assert isinstance(aug.params1d[0], iap.Uniform)
    assert isinstance(aug.params1d[0].a, iap.Deterministic)
    assert isinstance(aug.params1d[0].b, iap.Deterministic)
    assert aug.params1d[0].a.value == 1
    assert aug.params1d[0].b.value == 2

    # check that list to choice works
    aug = iaa.LogContrast([1, 2])
    assert isinstance(aug.params1d[0], iap.Choice)
    assert all([val in aug.params1d[0].a for val in [1, 2]])

    # check that per_channel at 50% prob works
    aug = iaa.LogContrast((0.5, 2.0), per_channel=0.5)
    seen = [False, False]
    img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128
    for _ in sm.xrange(100):
        img_aug = aug.augment_image(img1000d)
        assert img_aug.dtype.type == np.uint8
        l = len(set(img_aug.flatten().tolist()))
        if l == 1:
            seen[0] = True
        else:
            seen[1] = True
        if all(seen):
            break
    assert all(seen)

    # check that keypoints are not changed
    kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3))
    kpsoi_aug = iaa.LogContrast(gain=2).augment_keypoints([kpsoi])
    assert keypoints_equal([kpsoi], kpsoi_aug)

    # check that heatmaps are not changed
    heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5,
                                  shape=(3, 3, 3))
    heatmaps_aug = iaa.LogContrast(gain=2).augment_heatmaps([heatmaps])[0]
    assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)

    ###################
    # test other dtypes
    ###################
    # uint, int
    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16,
            np.int32, np.int64
    ]:
        min_value, center_value, max_value = meta.get_value_range_of_dtype(
            dtype)

        gains = [0.5, 0.75, 1.0, 1.1]
        values = [0, 100, int(center_value + 0.1 * max_value)]
        tmax = 1e-8 * max_value if dtype in [np.uint64, np.int64] else 0
        tolerances = [0, tmax, tmax]

        for gain in gains:
            aug = iaa.LogContrast(gain)
            for value, tolerance in zip(values, tolerances):
                image = np.full((3, 3), value, dtype=dtype)
                expected = gain * np.log2(1 + (image.astype(np.float128) /
                                               max_value))
                expected = (expected * max_value).astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert len(np.unique(image_aug)) == 1
                value_aug = int(image_aug[0, 0])
                value_expected = int(expected[0, 0])
                diff = abs(value_aug - value_expected)
                assert diff <= tolerance

    # float
    for dtype in [np.float16, np.float32, np.float64]:

        def _allclose(a, b):
            atol = 1e-2 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        gains = [0.5, 0.75, 1.0, 1.1]
        isize = np.dtype(dtype).itemsize
        values = [0, 1.0, 50.0, 100**(isize - 1)]

        for gain in gains:
            aug = iaa.LogContrast(gain)
            for value in values:
                image = np.full((3, 3), value, dtype=dtype)
                expected = gain * np.log2(1 + image.astype(np.float128))
                expected = expected.astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert _allclose(image_aug, expected)
        train_label[os.path.splitext(image)[0] +
                    "_sig_contrast.jpeg"] = train_label[image]

gammma_contrast = iaa.GammaContrast((1.1, 2.0))
for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        img = cv2.imread(os.path.join(train_img_dir, image))
        img_aug = gammma_contrast(image=img)
        cv2.imwrite(
            os.path.join(save_dir,
                         os.path.splitext(image)[0] + "_gamma_contrast.jpeg"),
            img_aug)
        train_label[os.path.splitext(image)[0] +
                    "_gamma_contrast.jpeg"] = train_label[image]

log_contrast = iaa.LogContrast(gain=(0.6, 1.4))
for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        img = cv2.imread(os.path.join(train_img_dir, image))
        img_aug = log_contrast(image=img)
        cv2.imwrite(
            os.path.join(save_dir,
                         os.path.splitext(image)[0] + "_log_contrast.jpeg"),
            img_aug)
        train_label[os.path.splitext(image)[0] +
                    "_log_contrast.jpeg"] = train_label[image]

linear_contrast = iaa.LinearContrast((1.1, 1.6))
for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        img = cv2.imread(os.path.join(train_img_dir, image))
Example #17
0
args = sys.argv
if len(args) != 2:
    print("Need option target directory")
    sys.exit()

target = args[1]
print("Augment target Directory :", target)

# Augmentation definition
seq0 = iaa.Sequential([
    iaa.Sometimes(0.8, iaa.Affine(rotate=(-90, 90))),
])
seq1 = iaa.Sequential([
    iaa.Sometimes(0.8, iaa.GaussianBlur(sigma=(0.0, 2.0))),
    iaa.Sometimes(0.8, iaa.LogContrast(gain=(0.8, 1.4))),
    iaa.Sometimes(0.4, iaa.AdditiveGaussianNoise(scale=(0, 20)))
])
seq2 = iaa.Sequential([
    iaa.Sometimes(0.9,
                  iaa.CoarseDropout((0.01, 0.04), size_percent=(0.01, 0.05))),
])

DATA_DIR = os.path.join(ROOT_DIR, target)
ids = 0
for curDir, dirs, files in os.walk(DATA_DIR):
    if not curDir.endswith("augmented") and os.path.isfile(curDir +
                                                           "/label.json"):
        if not os.path.isdir(curDir + "/augmented"):
            os.makedirs(curDir + "/augmented")
Example #18
0
    # Augmenter that changes the contrast of images using a unique formula (using gamma).
    # Multiplier for gamma function is between lo and hi,, sampled randomly per image (higher values darken image)
    # For percent of all images values are sampled independently per channel.
    "Gamma_Contrast": lambda lo, hi, percent: iaa.GammaContrast((lo, hi), per_channel=percent),

    # Augmenter that changes the contrast of images using a unique formula (linear).
    # Multiplier for linear function is between lo and hi, sampled randomly per image
    # For percent of all images values are sampled independently per channel.
    "Linear_Contrast": lambda lo, hi, percent: iaa.LinearContrast((lo, hi), per_channel=percent),

    # Augmenter that changes the contrast of images using a unique formula (using log).
    # Multiplier for log function is between lo and hi, sampled randomly per image.
    # For percent of all images values are sampled independently per channel.
    # Values around 1.0 lead to a contrast-adjusted images. Values above 1.0 quickly lead to partially broken
    # images due to exceeding the datatype’s value range.
    "Log_Contrast": lambda lo, hi, percent: iaa.LogContrast((lo, hi), per_channel=percent),

    # Augmenter that changes the contrast of images using a unique formula (sigmoid).
    # Multiplier for sigmoid function is between lo and hi, sampled randomly per image. c_lo and c_hi decide the
    # cutoff value that shifts the sigmoid function in horizontal direction (Higher values mean that the switch
    # from dark to light pixels happens later, i.e. the pixels will remain darker).
    # For percent of all images values are sampled independently per channel:
    "Sigmoid_Contrast": lambda lo, hi, c_lo, c_hi, percent:
    iaa.SigmoidContrast((lo, hi), (c_lo, c_hi), per_channel=percent),

    # Augmenter that calls a custom (lambda) function for each batch of input image.
    # Extracts Canny Edges from images (refer to description in CO)
    # Good default values for min and max are 100 and 200
    'Custom_Canny_Edges': lambda min_val, max_val: iaa.Lambda(
        func_images=CO.Edges(min_value=min_val, max_value=max_val)),
    model.load_state_dict(trained_weight)

    return model

augList = [iaa.Add(20),
            iaa.Add(-20),
            iaa.Multiply(0.8),
            iaa.Multiply(1.3),
            iaa.Cutout(fill_mode="constant", cval=(0, 255), fill_per_channel=1),
            iaa.SaltAndPepper(0.05),
            iaa.GaussianBlur(1.5),
            iaa.MotionBlur(k=15, angle=60, direction=1),
            iaa.MotionBlur(k=5, angle=60, direction=-1),
            iaa.Grayscale(0.5),
            iaa.SigmoidContrast(gain=10, cutoff=0.3),
            iaa.LogContrast(0.7),
            iaa.LogContrast(1.3),
            iaa.Sharpen(alpha=0.2, lightness=0.9),
            iaa.Sharpen(alpha=0.2, lightness=1.2),
            iaa.Fliplr(1),
            iaa.Flipud(1),
            iaa.Rotate(15),
            iaa.Rotate(-15),
            iaa.ShearX(-10),
            iaa.ShearX(10),
            iaa.ShearY(-10),
            iaa.ShearY(10),
            iaa.ScaleX(0.7),
            iaa.ScaleX(1.3),
            iaa.ScaleY(0.7),
            iaa.ScaleY(1.3),
Example #20
0
    def __init__(self,
                 dataset_type,
                 dataset_path,
                 real_path,
                 mesh_path,
                 mesh_info,
                 object_id,
                 batch_size,
                 img_res=(224, 224, 3),
                 is_testing=False):
        self.data_type = dataset_type
        self.img_res = img_res
        self.dataset_path = dataset_path
        self.real_path = [
            os.path.join(real_path, x) for x in os.listdir(real_path)
        ]
        self.batch_size = batch_size
        self.is_testing = is_testing
        self.ply_path = mesh_path
        self.obj_id = int(object_id)

        # annotate
        self.train_info = os.path.join(self.dataset_path, 'annotations',
                                       'instances_' + 'train' + '.json')
        self.val_info = os.path.join(self.dataset_path, 'annotations',
                                     'instances_' + 'val' + '.json')
        # self.mesh_info = os.path.join(self.dataset_path, 'annotations', 'models_info' + '.yml')
        self.mesh_info = mesh_info
        with open(self.train_info, 'r') as js:
            data = json.load(js)
        image_ann = data["images"]
        anno_ann = data["annotations"]
        self.image_ids = []
        self.Anns = []

        # init renderer
        # < 11 ms;
        self.ren = bop_renderer.Renderer()
        self.ren.init(640, 480)
        self.ren.add_object(self.obj_id, self.ply_path)

        stream = open(self.mesh_info, 'r')
        for key, value in yaml.load(stream).items():
            # for key, value in yaml.load(open(self.mesh_info)).items():
            if int(key) == self.obj_id + 1:
                self.model_dia = value['diameter']

        for ann in anno_ann:
            y_mean = (ann['bbox'][0] + ann['bbox'][2] * 0.5)
            x_mean = (ann['bbox'][1] + ann['bbox'][3] * 0.5)
            max_side = np.max(ann['bbox'][2:])
            x_min = int(x_mean - max_side * 0.75)
            x_max = int(x_mean + max_side * 0.75)
            y_min = int(y_mean - max_side * 0.75)
            y_max = int(y_mean + max_side * 0.75)
            if ann['category_id'] != 2 or ann[
                    'feature_visibility'] < 0.5 or x_min < 0 or x_max > 639 or y_min < 0 or y_max > 479:
                continue
            else:
                self.Anns.append(ann)
                # for img_info in image_ann:
                # print(img_info)
                #    if img_info['id'] == ann['id']:
                #        self.image_ids.append(img_info['file_name'])
                #        print(img_info['file_name'])
                template_name = '00000000000'
                id = str(ann['image_id'])
                # print(ann['id'])
                name = template_name[:-len(id)] + id + '_rgb.png'
                img_path = os.path.join(self.dataset_path, 'images',
                                        self.data_type, name)
                # print(name)
                self.image_ids.append(img_path)

        self.fx = image_ann[0]["fx"]
        self.fy = image_ann[0]["fy"]
        self.cx = image_ann[0]["cx"]
        self.cy = image_ann[0]["cy"]

        #self.image_idxs = range(len(self.image_ids))
        c = list(zip(self.Anns, self.image_ids))  #, self.image_idxs))
        np.random.shuffle(c)
        self.Anns, self.image_ids = zip(*c)

        self.img_seq = iaa.Sequential(
            [
                # blur
                iaa.SomeOf((0, 2), [
                    iaa.GaussianBlur((0.0, 2.0)),
                    iaa.AverageBlur(k=(3, 7)),
                    iaa.MedianBlur(k=(3, 7)),
                    iaa.BilateralBlur(d=(1, 7)),
                    iaa.MotionBlur(k=(3, 7))
                ]),
                # color
                iaa.SomeOf(
                    (0, 2),
                    [
                        # iaa.WithColorspace(),
                        iaa.AddToHueAndSaturation((-15, 15)),
                        # iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                        iaa.Grayscale(alpha=(0.0, 0.2))
                    ]),
                # brightness
                iaa.OneOf([
                    iaa.Sequential([
                        iaa.Add((-10, 10), per_channel=0.5),
                        iaa.Multiply((0.75, 1.25), per_channel=0.5)
                    ]),
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.75, 1.25), per_channel=0.5),
                    iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                            first=iaa.Multiply(
                                                (0.75, 1.25), per_channel=0.5),
                                            second=iaa.LinearContrast(
                                                (0.7, 1.3), per_channel=0.5))
                ]),
                # contrast
                iaa.SomeOf((0, 2), [
                    iaa.GammaContrast((0.75, 1.25), per_channel=0.5),
                    iaa.SigmoidContrast(
                        gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                    iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                    iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
                ]),
            ],
            random_order=True)

        self.n_batches = int(np.floor(len(self.image_ids) / self.batch_size))
        self.on_epoch_end()
        self.dataset_length = len(self.image_ids)
Example #21
0
def apply_transform(matrix, image, params):

    # rgb
    # seq describes an object for rgb image augmentation using aleju/imgaug
    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((0, 2), [
                iaa.GaussianBlur((0.0, 2.0)),
                iaa.AverageBlur(k=(3, 7)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.BilateralBlur(d=(1, 7)),
                iaa.MotionBlur(k=(3, 7))
            ]),
            # color
            iaa.SomeOf(
                (0, 2),
                [
                    # iaa.WithColorspace(),
                    iaa.AddToHueAndSaturation((-15, 15)),
                    # iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                    iaa.Grayscale(alpha=(0.0, 0.2))
                ]),
            # brightness
            iaa.OneOf([
                iaa.Sequential([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.75, 1.25), per_channel=0.5)
                ]),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.Multiply((0.75, 1.25), per_channel=0.5),
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Multiply(
                                            (0.75, 1.25), per_channel=0.5),
                                        second=iaa.LinearContrast(
                                            (0.7, 1.3), per_channel=0.5))
            ]),
            # contrast
            iaa.SomeOf((0, 2), [
                iaa.GammaContrast((0.75, 1.25), per_channel=0.5),
                iaa.SigmoidContrast(
                    gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
            ]),
        ],
        random_order=True)
    image = seq.augment_image(image)
    '''
    seq = iaa.Sequential([
                        iaa.Sometimes(0.5, iaa.CoarseDropout(p=0.2, size_percent=(0.1, 0.25))),
                        iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())),
                        iaa.Sometimes(0.5, iaa.Add((-25, 25), per_channel=0.3)),
                        iaa.Sometimes(0.5, iaa.Invert(0.2, per_channel=True)),
                        iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
                        iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))),
                        iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))
                ], random_order=False)
    image = seq.augment_image(image)
    '''
    image = cv2.warpAffine(
        image,
        matrix[:2, :],
        dsize=(image.shape[1], image.shape[0]),
        flags=params.cvInterpolation(),
        borderMode=params.cvBorderMode(),
        borderValue=params.cval,
    )

    return image
    def build_augmentation_pipeline(self, apply_prob=0.5):
        cfg = self.cfg

        sometimes = lambda aug: iaa.Sometimes(apply_prob, aug)
        pipeline = iaa.Sequential(random_order=False)

        pre_resize = cfg.get("pre_resize")
        crop_sampling = cfg.get("crop_sampling", "hybrid")
        if pre_resize:
            width, height = pre_resize
            pipeline.add(iaa.Resize({"height": height, "width": width}))
            if crop_sampling == "none":
                self.default_size = width, height

        if crop_sampling != "none":
            # Add smart, keypoint-aware image cropping
            pipeline.add(iaa.PadToFixedSize(*self.default_size))
            pipeline.add(
                augmentation.KeypointAwareCropToFixedSize(
                    *self.default_size,
                    cfg.get("max_shift", 0.4),
                    crop_sampling,
                ))

        if cfg.get("fliplr", False):
            opt = cfg.get("fliplr", False)
            if type(opt) == int:
                pipeline.add(sometimes(iaa.Fliplr(opt)))
            else:
                pipeline.add(sometimes(iaa.Fliplr(0.5)))
        if cfg.get("rotation", False):
            opt = cfg.get("rotation", False)
            if type(opt) == int:
                pipeline.add(sometimes(iaa.Affine(rotate=(-opt, opt))))
            else:
                pipeline.add(sometimes(iaa.Affine(rotate=(-10, 10))))
        if cfg.get("hist_eq", False):
            pipeline.add(sometimes(iaa.AllChannelsHistogramEqualization()))
        if cfg.get("motion_blur", False):
            opts = cfg.get("motion_blur", False)
            if type(opts) == list:
                opts = dict(opts)
                pipeline.add(sometimes(iaa.MotionBlur(**opts)))
            else:
                pipeline.add(sometimes(iaa.MotionBlur(k=7, angle=(-90, 90))))
        if cfg.get("covering", False):
            pipeline.add(
                sometimes(
                    iaa.CoarseDropout(
                        (0, 0.02),
                        size_percent=(0.01, 0.05))))  # , per_channel=0.5)))
        if cfg.get("elastic_transform", False):
            pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5)))
        if cfg.get("gaussian_noise", False):
            opt = cfg.get("gaussian_noise", False)
            if type(opt) == int or type(opt) == float:
                pipeline.add(
                    sometimes(
                        iaa.AdditiveGaussianNoise(loc=0,
                                                  scale=(0.0, opt),
                                                  per_channel=0.5)))
            else:
                pipeline.add(
                    sometimes(
                        iaa.AdditiveGaussianNoise(loc=0,
                                                  scale=(0.0, 0.05 * 255),
                                                  per_channel=0.5)))
        if cfg.get("grayscale", False):
            pipeline.add(sometimes(iaa.Grayscale(alpha=(0.5, 1.0))))

        def get_aug_param(cfg_value):
            if isinstance(cfg_value, dict):
                opt = cfg_value
            else:
                opt = {}
            return opt

        cfg_cnt = cfg.get("contrast", {})
        cfg_cnv = cfg.get("convolution", {})

        contrast_aug = ["histeq", "clahe", "gamma", "sigmoid", "log", "linear"]
        for aug in contrast_aug:
            aug_val = cfg_cnt.get(aug, False)
            cfg_cnt[aug] = aug_val
            if aug_val:
                cfg_cnt[aug + "ratio"] = cfg_cnt.get(aug + "ratio", 0.1)

        convolution_aug = ["sharpen", "emboss", "edge"]
        for aug in convolution_aug:
            aug_val = cfg_cnv.get(aug, False)
            cfg_cnv[aug] = aug_val
            if aug_val:
                cfg_cnv[aug + "ratio"] = cfg_cnv.get(aug + "ratio", 0.1)

        if cfg_cnt["histeq"]:
            opt = get_aug_param(cfg_cnt["histeq"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["histeqratio"],
                              iaa.AllChannelsHistogramEqualization(**opt)))

        if cfg_cnt["clahe"]:
            opt = get_aug_param(cfg_cnt["clahe"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["claheratio"],
                              iaa.AllChannelsCLAHE(**opt)))

        if cfg_cnt["log"]:
            opt = get_aug_param(cfg_cnt["log"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["logratio"], iaa.LogContrast(**opt)))

        if cfg_cnt["linear"]:
            opt = get_aug_param(cfg_cnt["linear"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["linearratio"],
                              iaa.LinearContrast(**opt)))

        if cfg_cnt["sigmoid"]:
            opt = get_aug_param(cfg_cnt["sigmoid"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["sigmoidratio"],
                              iaa.SigmoidContrast(**opt)))

        if cfg_cnt["gamma"]:
            opt = get_aug_param(cfg_cnt["gamma"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["gammaratio"], iaa.GammaContrast(**opt)))

        if cfg_cnv["sharpen"]:
            opt = get_aug_param(cfg_cnv["sharpen"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["sharpenratio"], iaa.Sharpen(**opt)))

        if cfg_cnv["emboss"]:
            opt = get_aug_param(cfg_cnv["emboss"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["embossratio"], iaa.Emboss(**opt)))

        if cfg_cnv["edge"]:
            opt = get_aug_param(cfg_cnv["edge"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["edgeratio"], iaa.EdgeDetect(**opt)))

        return pipeline
Example #23
0
def test_LogContrast():
    reseed()

    img = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    img = np.uint8(img)
    img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # check basic functionality with gain=1 or 2 (deterministic) and per_chanenl on/off (makes
    # no difference due to deterministic gain)
    for per_channel in [False, 0, 0.0, True, 1, 1.0]:
        for gain in [1, 2]:
            aug = iaa.LogContrast(gain=iap.Deterministic(gain), per_channel=per_channel)
            img_aug = aug.augment_image(img)
            img3d_aug = aug.augment_image(img3d)
            assert img_aug.dtype.type == np.uint8
            assert img3d_aug.dtype.type == np.uint8
            assert np.array_equal(img_aug, skimage.exposure.adjust_log(img, gain=gain))
            assert np.array_equal(img3d_aug, skimage.exposure.adjust_log(img3d, gain=gain))

    # check that tuple to uniform works
    aug = iaa.LogContrast((1, 2))
    assert isinstance(aug.params1d[0], iap.Uniform)
    assert isinstance(aug.params1d[0].a, iap.Deterministic)
    assert isinstance(aug.params1d[0].b, iap.Deterministic)
    assert aug.params1d[0].a.value == 1
    assert aug.params1d[0].b.value == 2

    # check that list to choice works
    aug = iaa.LogContrast([1, 2])
    assert isinstance(aug.params1d[0], iap.Choice)
    assert all([val in aug.params1d[0].a for val in [1, 2]])

    # check that per_channel at 50% prob works
    aug = iaa.LogContrast((0.5, 2.0), per_channel=0.5)
    seen = [False, False]
    img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128
    for _ in sm.xrange(100):
        img_aug = aug.augment_image(img1000d)
        assert img_aug.dtype.type == np.uint8
        l = len(set(img_aug.flatten().tolist()))
        if l == 1:
            seen[0] = True
        else:
            seen[1] = True
        if all(seen):
            break
    assert all(seen)

    # check that keypoints are not changed
    kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3))
    kpsoi_aug = iaa.LogContrast(gain=2).augment_keypoints([kpsoi])
    assert keypoints_equal([kpsoi], kpsoi_aug)

    # check that heatmaps are not changed
    heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5, shape=(3, 3, 3))
    heatmaps_aug = iaa.LogContrast(gain=2).augment_heatmaps([heatmaps])[0]
    assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)
def create_augmenters(height, width, height_augmentable, width_augmentable,
                      only_augmenters):
    def lambda_func_images(images, random_state, parents, hooks):
        return images

    def lambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return heatmaps

    def lambda_func_keypoints(keypoints, random_state, parents, hooks):
        return keypoints

    def assertlambda_func_images(images, random_state, parents, hooks):
        return True

    def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks):
        return True

    def assertlambda_func_keypoints(keypoints, random_state, parents, hooks):
        return True

    augmenters_meta = [
        iaa.Sequential([iaa.Noop(), iaa.Noop()],
                       random_order=False,
                       name="Sequential_2xNoop"),
        iaa.Sequential([iaa.Noop(), iaa.Noop()],
                       random_order=True,
                       name="Sequential_2xNoop_random_order"),
        iaa.SomeOf((1, 3),
                   [iaa.Noop(), iaa.Noop(), iaa.Noop()],
                   random_order=False,
                   name="SomeOf_3xNoop"),
        iaa.SomeOf((1, 3),
                   [iaa.Noop(), iaa.Noop(), iaa.Noop()],
                   random_order=True,
                   name="SomeOf_3xNoop_random_order"),
        iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"),
        iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"),
        iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"),
        iaa.Noop(name="Noop"),
        iaa.Lambda(func_images=lambda_func_images,
                   func_heatmaps=lambda_func_heatmaps,
                   func_keypoints=lambda_func_keypoints,
                   name="Lambda"),
        iaa.AssertLambda(func_images=assertlambda_func_images,
                         func_heatmaps=assertlambda_func_heatmaps,
                         func_keypoints=assertlambda_func_keypoints,
                         name="AssertLambda"),
        iaa.AssertShape((None, height_augmentable, width_augmentable, None),
                        name="AssertShape"),
        iaa.ChannelShuffle(0.5, name="ChannelShuffle")
    ]
    augmenters_arithmetic = [
        iaa.Add((-10, 10), name="Add"),
        iaa.AddElementwise((-10, 10), name="AddElementwise"),
        #iaa.AddElementwise((-500, 500), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"),
        iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"),
        iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Dropout((0.01, 0.05), name="Dropout"),
        iaa.CoarseDropout((0.01, 0.05),
                          size_percent=(0.01, 0.1),
                          name="CoarseDropout"),
        iaa.ReplaceElementwise((0.01, 0.05), (0, 255),
                               name="ReplaceElementwise"),
        #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"),
        iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"),
        iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"),
        iaa.CoarseSaltAndPepper((0.01, 0.05),
                                size_percent=(0.01, 0.1),
                                name="CoarseSaltAndPepper"),
        iaa.Salt((0.01, 0.05), name="Salt"),
        iaa.CoarseSalt((0.01, 0.05),
                       size_percent=(0.01, 0.1),
                       name="CoarseSalt"),
        iaa.Pepper((0.01, 0.05), name="Pepper"),
        iaa.CoarsePepper((0.01, 0.05),
                         size_percent=(0.01, 0.1),
                         name="CoarsePepper"),
        iaa.Invert(0.1, name="Invert"),
        # ContrastNormalization
        iaa.JpegCompression((50, 99), name="JpegCompression")
    ]
    augmenters_blend = [
        iaa.Alpha((0.01, 0.99), iaa.Noop(), name="Alpha"),
        iaa.AlphaElementwise((0.01, 0.99), iaa.Noop(),
                             name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha((-2.0, 2.0),
                                iaa.Noop(),
                                name="FrequencyNoiseAlpha")
    ]
    augmenters_blur = [
        iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"),
        iaa.MotionBlur(k=(3, 11), name="MotionBlur")
    ]
    augmenters_color = [
        # InColorspace (deprecated)
        iaa.WithColorspace(to_colorspace="HSV",
                           children=iaa.Noop(),
                           name="WithColorspace"),
        iaa.WithHueAndSaturation(children=iaa.Noop(),
                                 name="WithHueAndSaturation"),
        iaa.MultiplyHueAndSaturation((0.8, 1.2),
                                     name="MultiplyHueAndSaturation"),
        iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"),
        iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"),
        iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"),
        iaa.AddToHue((-10, 10), name="AddToHue"),
        iaa.AddToSaturation((-10, 10), name="AddToSaturation"),
        iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"),
        iaa.Grayscale((0.01, 0.99), name="Grayscale"),
        iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"),
        iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization")
    ]
    augmenters_contrast = [
        iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"),
        iaa.SigmoidContrast(gain=(5, 20),
                            cutoff=(0.25, 0.75),
                            name="SigmoidContrast"),
        iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"),
        iaa.LinearContrast((0.5, 1.5), name="LinearContrast"),
        iaa.AllChannelsCLAHE(clip_limit=(2, 10),
                             tile_grid_size_px=(3, 11),
                             name="AllChannelsCLAHE"),
        iaa.CLAHE(clip_limit=(2, 10),
                  tile_grid_size_px=(3, 11),
                  to_colorspace="HSV",
                  name="CLAHE"),
        iaa.AllChannelsHistogramEqualization(
            name="AllChannelsHistogramEqualization"),
        iaa.HistogramEqualization(to_colorspace="HSV",
                                  name="HistogramEqualization"),
    ]
    augmenters_convolutional = [
        iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]),
                     name="Convolve_3x3"),
        iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"),
        iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect")
    ]
    augmenters_edges = [iaa.Canny(alpha=(0.01, 0.99), name="Canny")]
    augmenters_flip = [
        iaa.Fliplr(1.0, name="Fliplr"),
        iaa.Flipud(1.0, name="Flipud")
    ]
    augmenters_geometric = [
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=0,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_0_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_1_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=3,
                   mode="constant",
                   cval=(0, 255),
                   name="Affine_order_3_constant"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="edge",
                   cval=(0, 255),
                   name="Affine_order_1_edge"),
        iaa.Affine(scale=(0.9, 1.1),
                   translate_percent={
                       "x": (-0.05, 0.05),
                       "y": (-0.05, 0.05)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10),
                   order=1,
                   mode="constant",
                   cval=(0, 255),
                   backend="skimage",
                   name="Affine_order_1_constant_skimage"),
        # TODO AffineCv2
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=1,
                            mode="constant",
                            name="PiecewiseAffine_4x4_order_1_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=0,
                            mode="constant",
                            name="PiecewiseAffine_4x4_order_0_constant"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=4,
                            nb_cols=4,
                            order=1,
                            mode="edge",
                            name="PiecewiseAffine_4x4_order_1_edge"),
        iaa.PiecewiseAffine(scale=(0.01, 0.05),
                            nb_rows=8,
                            nb_cols=8,
                            order=1,
                            mode="constant",
                            name="PiecewiseAffine_8x8_order_1_constant"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                 keep_size=False,
                                 name="PerspectiveTransform"),
        iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                 keep_size=True,
                                 name="PerspectiveTransform_keep_size"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=0,
            mode="constant",
            cval=0,
            name="ElasticTransformation_order_0_constant"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="constant",
            cval=0,
            name="ElasticTransformation_order_1_constant"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="nearest",
            cval=0,
            name="ElasticTransformation_order_1_nearest"),
        iaa.ElasticTransformation(
            alpha=(1, 10),
            sigma=(0.5, 1.5),
            order=1,
            mode="reflect",
            cval=0,
            name="ElasticTransformation_order_1_reflect"),
        iaa.Rot90((1, 3), keep_size=False, name="Rot90"),
        iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size")
    ]
    augmenters_pooling = [
        iaa.AveragePooling(kernel_size=(1, 16),
                           keep_size=False,
                           name="AveragePooling"),
        iaa.AveragePooling(kernel_size=(1, 16),
                           keep_size=True,
                           name="AveragePooling_keep_size"),
        iaa.MaxPooling(kernel_size=(1, 16), keep_size=False,
                       name="MaxPooling"),
        iaa.MaxPooling(kernel_size=(1, 16),
                       keep_size=True,
                       name="MaxPooling_keep_size"),
        iaa.MinPooling(kernel_size=(1, 16), keep_size=False,
                       name="MinPooling"),
        iaa.MinPooling(kernel_size=(1, 16),
                       keep_size=True,
                       name="MinPooling_keep_size"),
        iaa.MedianPooling(kernel_size=(1, 16),
                          keep_size=False,
                          name="MedianPooling"),
        iaa.MedianPooling(kernel_size=(1, 16),
                          keep_size=True,
                          name="MedianPooling_keep_size")
    ]
    augmenters_segmentation = [
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=64,
                        interpolation="cubic",
                        name="Superpixels_max_size_64_cubic"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=64,
                        interpolation="linear",
                        name="Superpixels_max_size_64_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=128,
                        interpolation="linear",
                        name="Superpixels_max_size_128_linear"),
        iaa.Superpixels(p_replace=(0.05, 1.0),
                        n_segments=(10, 100),
                        max_size=224,
                        interpolation="linear",
                        name="Superpixels_max_size_224_linear"),
        iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"),
        iaa.RegularGridVoronoi(n_rows=(16, 31),
                               n_cols=(16, 31),
                               name="RegularGridVoronoi"),
        iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14),
                                       n_cols_frac=(0.07, 0.14),
                                       name="RelativeRegularGridVoronoi"),
    ]
    augmenters_size = [
        iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"),
        iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"),
        iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="constant",
                       pad_cval=(0, 255),
                       keep_size=False,
                       name="CropAndPad"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="edge",
                       pad_cval=(0, 255),
                       keep_size=False,
                       name="CropAndPad_edge"),
        iaa.CropAndPad(percent=(-0.2, 0.2),
                       pad_mode="constant",
                       pad_cval=(0, 255),
                       name="CropAndPad_keep_size"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="constant",
                pad_cval=(0, 255),
                keep_size=False,
                name="Pad"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="edge",
                pad_cval=(0, 255),
                keep_size=False,
                name="Pad_edge"),
        iaa.Pad(percent=(0.05, 0.2),
                pad_mode="constant",
                pad_cval=(0, 255),
                name="Pad_keep_size"),
        iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"),
        iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"),
        iaa.PadToFixedSize(width=width + 10,
                           height=height + 10,
                           pad_mode="constant",
                           pad_cval=(0, 255),
                           name="PadToFixedSize"),
        iaa.CropToFixedSize(width=width - 10,
                            height=height - 10,
                            name="CropToFixedSize"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="nearest",
                             name="KeepSizeByResize_CropToFixedSize_nearest"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="linear",
                             name="KeepSizeByResize_CropToFixedSize_linear"),
        iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10,
                                                 width=width - 10),
                             interpolation="cubic",
                             name="KeepSizeByResize_CropToFixedSize_cubic"),
    ]
    augmenters_weather = [
        iaa.FastSnowyLandscape(lightness_threshold=(100, 255),
                               lightness_multiplier=(1.0, 4.0),
                               name="FastSnowyLandscape"),
        iaa.Clouds(name="Clouds"),
        iaa.Fog(name="Fog"),
        iaa.CloudLayer(intensity_mean=(196, 255),
                       intensity_freq_exponent=(-2.5, -2.0),
                       intensity_coarse_scale=10,
                       alpha_min=0,
                       alpha_multiplier=(0.25, 0.75),
                       alpha_size_px_max=(2, 8),
                       alpha_freq_exponent=(-2.5, -2.0),
                       sparsity=(0.8, 1.0),
                       density_multiplier=(0.5, 1.0),
                       name="CloudLayer"),
        iaa.Snowflakes(name="Snowflakes"),
        iaa.SnowflakesLayer(density=(0.005, 0.075),
                            density_uniformity=(0.3, 0.9),
                            flake_size=(0.2, 0.7),
                            flake_size_uniformity=(0.4, 0.8),
                            angle=(-30, 30),
                            speed=(0.007, 0.03),
                            blur_sigma_fraction=(0.0001, 0.001),
                            name="SnowflakesLayer")
    ]

    augmenters = (augmenters_meta + augmenters_arithmetic + augmenters_blend +
                  augmenters_blur + augmenters_color + augmenters_contrast +
                  augmenters_convolutional + augmenters_edges +
                  augmenters_flip + augmenters_geometric + augmenters_pooling +
                  augmenters_segmentation + augmenters_size +
                  augmenters_weather)

    if only_augmenters is not None:
        augmenters_reduced = []
        for augmenter in augmenters:
            if any([
                    re.search(pattern, augmenter.name)
                    for pattern in only_augmenters
            ]):
                augmenters_reduced.append(augmenter)
        augmenters = augmenters_reduced

    return augmenters
Example #25
0
critical_4 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.Add(20)
]

critical_5 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.Multiply(1.3)
]

critical_6 =[
    iaa.SaltAndPepper(0.05),
    iaa.Sharpen(alpha=0.2, lightness=0.9),
    iaa.LogContrast(1.3)
]

critical_cases = [critical_1, critical_2, critical_3, critical_4, critical_5, critical_6]

# Our Dataset Classes
classes = ('airplane', 'cat', 'dog', 'motorbike', 'person')

def ImgTransform(images, TransformList):
    
    seq = iaa.Sequential([
        TransformList[0],
        TransformList[1],
        TransformList[2],
    ])
Example #26
0
    def build_augmentation_pipeline(self,
                                    height=None,
                                    width=None,
                                    apply_prob=0.5):
        sometimes = lambda aug: iaa.Sometimes(apply_prob, aug)
        pipeline = iaa.Sequential(random_order=False)

        cfg = self.cfg
        if cfg["mirror"]:
            opt = cfg["mirror"]  # fliplr
            if type(opt) == int:
                pipeline.add(sometimes(iaa.Fliplr(opt)))
            else:
                pipeline.add(sometimes(iaa.Fliplr(0.5)))

        if cfg["rotation"] > 0:
            pipeline.add(
                iaa.Sometimes(
                    cfg["rotratio"],
                    iaa.Affine(rotate=(-cfg["rotation"], cfg["rotation"])),
                ))

        if cfg["motion_blur"]:
            opts = cfg["motion_blur_params"]
            pipeline.add(sometimes(iaa.MotionBlur(**opts)))

        if cfg["covering"]:
            pipeline.add(
                sometimes(
                    iaa.CoarseDropout(0.02, size_percent=0.3,
                                      per_channel=0.5)))

        if cfg["elastic_transform"]:
            pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5)))

        if cfg.get("gaussian_noise", False):
            opt = cfg.get("gaussian_noise", False)
            if type(opt) == int or type(opt) == float:
                pipeline.add(
                    sometimes(
                        iaa.AdditiveGaussianNoise(loc=0,
                                                  scale=(0.0, opt),
                                                  per_channel=0.5)))
            else:
                pipeline.add(
                    sometimes(
                        iaa.AdditiveGaussianNoise(loc=0,
                                                  scale=(0.0, 0.05 * 255),
                                                  per_channel=0.5)))
        if cfg.get("grayscale", False):
            pipeline.add(sometimes(iaa.Grayscale(alpha=(0.5, 1.0))))

        def get_aug_param(cfg_value):
            if isinstance(cfg_value, dict):
                opt = cfg_value
            else:
                opt = {}
            return opt

        cfg_cnt = cfg.get("contrast", {})
        cfg_cnv = cfg.get("convolution", {})

        contrast_aug = ["histeq", "clahe", "gamma", "sigmoid", "log", "linear"]
        for aug in contrast_aug:
            aug_val = cfg_cnt.get(aug, False)
            cfg_cnt[aug] = aug_val
            if aug_val:
                cfg_cnt[aug + "ratio"] = cfg_cnt.get(aug + "ratio", 0.1)

        convolution_aug = ["sharpen", "emboss", "edge"]
        for aug in convolution_aug:
            aug_val = cfg_cnv.get(aug, False)
            cfg_cnv[aug] = aug_val
            if aug_val:
                cfg_cnv[aug + "ratio"] = cfg_cnv.get(aug + "ratio", 0.1)

        if cfg_cnt["histeq"]:
            opt = get_aug_param(cfg_cnt["histeq"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["histeqratio"],
                              iaa.AllChannelsHistogramEqualization(**opt)))

        if cfg_cnt["clahe"]:
            opt = get_aug_param(cfg_cnt["clahe"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["claheratio"],
                              iaa.AllChannelsCLAHE(**opt)))

        if cfg_cnt["log"]:
            opt = get_aug_param(cfg_cnt["log"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["logratio"], iaa.LogContrast(**opt)))

        if cfg_cnt["linear"]:
            opt = get_aug_param(cfg_cnt["linear"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["linearratio"],
                              iaa.LinearContrast(**opt)))

        if cfg_cnt["sigmoid"]:
            opt = get_aug_param(cfg_cnt["sigmoid"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["sigmoidratio"],
                              iaa.SigmoidContrast(**opt)))

        if cfg_cnt["gamma"]:
            opt = get_aug_param(cfg_cnt["gamma"])
            pipeline.add(
                iaa.Sometimes(cfg_cnt["gammaratio"], iaa.GammaContrast(**opt)))

        if cfg_cnv["sharpen"]:
            opt = get_aug_param(cfg_cnv["sharpen"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["sharpenratio"], iaa.Sharpen(**opt)))

        if cfg_cnv["emboss"]:
            opt = get_aug_param(cfg_cnv["emboss"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["embossratio"], iaa.Emboss(**opt)))

        if cfg_cnv["edge"]:
            opt = get_aug_param(cfg_cnv["edge"])
            pipeline.add(
                iaa.Sometimes(cfg_cnv["edgeratio"], iaa.EdgeDetect(**opt)))

        if height is not None and width is not None:
            if not cfg.get("crop_by", False):
                crop_by = 0.15
            else:
                crop_by = cfg.get("crop_by", False)
            pipeline.add(
                iaa.Sometimes(
                    cfg.get("cropratio", 0.4),
                    iaa.CropAndPad(percent=(-crop_by, crop_by),
                                   keep_size=False),
                ))
            pipeline.add(iaa.Resize({"height": height, "width": width}))
        return pipeline
Example #27
0
    def imgaug_augment(self, target_dir='default', mode='native'):

        if target_dir == 'default':
            data, label = self.npzLoader(self.train_file)
        else:
            data, label = self.getStackedData(target_dir=target_dir)


        if mode == 'native':
            return data, label
        elif mode == 'rotation':
            imgaug_aug = iaa.Affine(rotate=(-90, 90), order=1, mode="edge")  # 90度回転
            # keras と仕様が異なることに注意
            #   keras は変化量 / imgaug は 変化の最大角を指定している
            #   開いた部分の穴埋めができない..?? mode="edge" にするとそれなり..
        elif mode == 'hflip':
            imgaug_aug = iaa.Fliplr(1.0)  # 左右反転
        elif mode == 'width_shift':
            imgaug_aug = iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge")  # 1/8 平行移動(左右)
        elif mode == 'height_shift':
            imgaug_aug = iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge")  # 1/8 平行移動(上下)
            # imgaug_aug = iaa.Crop(px=(0, 40))  <= 平行移動ではなく、切り抜き
        elif mode == 'zoom':
            imgaug_aug = iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge")  # 80~120% ズーム
            # これも keras と仕様が違って、縦横独立に拡大・縮小されるようである。
        elif mode == 'logcon':
            imgaug_aug = iaa.LogContrast(gain=(5, 15))
        elif mode == 'linecon':
            imgaug_aug = iaa.LinearContrast((0.5, 2.0))  # 明度変換
        elif mode == 'gnoise':
            imgaug_aug = iaa.AdditiveGaussianNoise(scale=[0.05*255, 0.2*255])  # Gaussian Noise
        elif mode == 'lnoise':
            imgaug_aug = iaa.AdditiveLaplaceNoise(scale=[0.05*255, 0.2*255])  # LaplaceNoise
        elif mode == 'pnoise':
            imgaug_aug = iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True)  # PoissonNoise
        elif mode == 'flatten':
            imgaug_aug = iaa.GaussianBlur(sigma=(0.5, 1.0))  # blur: ぼかし (平滑化)
        elif mode == 'sharpen':
            imgaug_aug = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) # sharpen images (鮮鋭化)
        elif mode == 'emboss':
            imgaug_aug = iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))  # Edge 強調
        elif mode == 'invert':
            #imgaug_aug = iaa.Invert(1.0)  # 色反転 <= これがうまく行かないので自分で作った。
            aug_data = []
            for b in range(data.shape[0]):
                aug_data.append(255-data[b])
            return np.array(aug_data), label
        elif mode == 'someof':  # 上記のうちのどれか1つ
            imgaug_aug = iaa.SomeOf(1, [
                iaa.Affine(rotate=(-90, 90), order=1, mode="edge"),
                iaa.Fliplr(1.0),
                iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge"),
                iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge"),
                iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge"),
                iaa.LogContrast((0.5, 1.5)),
                iaa.LinearContrast((0.5, 2.0)),
                iaa.AdditiveGaussianNoise(scale=[0.05*255, 0.25*255]),
                iaa.AdditiveLaplaceNoise(scale=[0.05*255, 0.25*255]),
                iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True),
                iaa.GaussianBlur(sigma=(0.5, 1.0)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
                iaa.Invert(1.0)  # 14
            ])
        #elif mode == 'plural':  # 異なる系統の変換を複数(1つの変換あとに画素値がマイナスになるとError..)
        #    imgaug_aug = self.randomDataAugument(2)
        else:
            print("現在 imgaug で選択できる DA のモードは以下の通りです。")
            print(self.imgaug_mode_list, "\n")
            raise ValueError("予期されないモードが選択されています。")

        aug_data = imgaug_aug.augment_images(data)
        aug_data = np.clip(aug_data, 0, 255)

        # 注意: 戻り値の範囲は [0, 255] です。
        return aug_data, label
Example #28
0
    def __init__(self, img_dir, mask_dir, img_format):
        self.img_dir = img_dir
        self.mask_dir = mask_dir
        self.img_type = img_format
        self.augment_geometric = [
            iaa.OneOf([iaa.Affine(rotate=40)]),  #0
            iaa.OneOf([iaa.Affine(rotate=80)]),
            iaa.OneOf([iaa.Affine(rotate=120)]),
            iaa.OneOf([iaa.Affine(rotate=160)]),
            iaa.OneOf([iaa.Affine(rotate=200)]),
            iaa.OneOf([iaa.Affine(rotate=240)]),  #5
            iaa.OneOf([iaa.Affine(rotate=280)]),
            iaa.OneOf([iaa.Affine(rotate=320)]),
            iaa.OneOf([iaa.Affine(scale={
                "x": (0.8),
                "y": (0.8)
            })]),
            iaa.OneOf([iaa.Affine(scale={
                "x": (1.2),
                "y": (1.2)
            })]),  #10
            iaa.OneOf([iaa.Flipud(1)]),
            iaa.OneOf([iaa.Fliplr(1)])
        ]  #13

        self.augment_spectral = [
            iaa.OneOf([iaa.GaussianBlur(sigma=(0.75))]),  #0
            iaa.OneOf([iaa.AddElementwise((-60, -10))]),
            iaa.OneOf([iaa.AddElementwise((10, 60))]),
            iaa.OneOf([iaa.LogContrast(gain=(0.6, 1.4))]),
            iaa.OneOf([iaa.LogContrast(gain=(0.6, 1.4), per_channel=True)]),
            iaa.OneOf([iaa.Add((-60, -40))]),  #5
            iaa.OneOf([iaa.Add((40, 80))]),
            iaa.OneOf([iaa.Add((-40, 0), per_channel=True)]),
            iaa.OneOf([iaa.Add((0, 40), per_channel=True)])
        ]  #8

        self.augmentations = [
            iaa.OneOf([iaa.Affine(rotate=40)]),  #0
            iaa.OneOf([iaa.Affine(rotate=80)]),
            iaa.OneOf([iaa.Affine(rotate=120)]),
            iaa.OneOf([iaa.Affine(rotate=160)]),
            iaa.OneOf([iaa.Affine(rotate=200)]),
            iaa.OneOf([iaa.Affine(rotate=240)]),
            iaa.OneOf([iaa.Affine(rotate=280)]),
            iaa.OneOf([iaa.Affine(rotate=320)]),
            iaa.OneOf([iaa.Affine(scale={
                "x": (0.8),
                "y": (0.8)
            })]),
            iaa.OneOf([iaa.Affine(scale={
                "x": (1.2),
                "y": (1.2)
            })]),
            iaa.OneOf([iaa.Flipud(1)]),
            iaa.OneOf([iaa.Fliplr(1)]),
            iaa.OneOf([iaa.AddElementwise((-60, -10))]),  #12
            iaa.OneOf([iaa.AddElementwise((10, 60))]),
            iaa.OneOf([iaa.GaussianBlur(sigma=(0.75))]),
            iaa.OneOf([iaa.GaussianBlur(sigma=(1.50))]),
            iaa.OneOf([iaa.LogContrast(gain=(0.6, 1.4))]),
            iaa.OneOf([iaa.LogContrast(gain=(0.6, 1.4), per_channel=True)]),
            iaa.OneOf([iaa.Add((-60, -40))]),
            iaa.OneOf([iaa.Add((40, 80))]),
            iaa.OneOf([iaa.Add((-40, 0), per_channel=True)]),  #20
            iaa.OneOf([iaa.Add((0, 40), per_channel=True)])  #21
        ]
Example #29
0
                                      per_channel=0.5),
                    iaa.SaltAndPepper((0.01, 0.3))
                ]),
                # Play with the colors of the image
                iaa.OneOf([
                    iaa.Invert(0.01, per_channel=0.5),
                    iaa.AddToHueAndSaturation((-1, 1)),
                    iaa.MultiplyHueAndSaturation((-1, 1))
                ]),
                # Change brightness and contrast
                iaa.OneOf([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),
                    iaa.GammaContrast(gamma=(0.5, 1.75), per_channel=0.5),
                    iaa.SigmoidContrast(cutoff=(0, 1), per_channel=0.5),
                    iaa.LogContrast(gain=(0.5, 1), per_channel=0.5),
                    iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=0.5),
                    iaa.HistogramEqualization()
                ]),
                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.2))),
                iaa.JpegCompression((0.1, 1))
            ]
            ),
 # With 10 % probability apply one the of the weather conditions
 iaa.Sometimes(0.2, iaa.OneOf([
     iaa.Clouds(),
Example #30
0
    def imgaug_augment(self,
                       TARGET_DIR,
                       INPUT_SIZE,
                       NORMALIZE=False,
                       AUGMENTATION='native'):

        data, label = self.img2array(TARGET_DIR, INPUT_SIZE, NORMALIZE)

        if AUGMENTATION == 'native':
            return data, label
        elif AUGMENTATION == 'rotation':
            imgaug_aug = iaa.Affine(rotate=(-90, 90), order=1,
                                    mode="edge")  # 90度 "まで" 回転
        elif AUGMENTATION == 'hflip':
            imgaug_aug = iaa.Fliplr(1.0)  # 左右反転
        elif AUGMENTATION == 'width_shift':
            imgaug_aug = iaa.Affine(translate_percent={"x": (-0.125, 0.125)},
                                    order=1,
                                    mode="edge")  # 1/8 平行移動(左右)
        elif AUGMENTATION == 'height_shift':
            imgaug_aug = iaa.Affine(translate_percent={"y": (-0.125, 0.125)},
                                    order=1,
                                    mode="edge")  # 1/8 平行移動(上下)
            # imgaug_aug = iaa.Crop(px=(0, 40))  <= 平行移動ではなく、切り抜き
        elif AUGMENTATION == 'zoom':
            imgaug_aug = iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                                    order=1,
                                    mode="edge")  # 80~120% ズーム
            # これも keras と仕様が違って、縦横独立に拡大・縮小されるようである。
        elif AUGMENTATION == 'logcon':
            imgaug_aug = iaa.LogContrast((0.5, 1.5))
        elif AUGMENTATION == 'linecon':
            imgaug_aug = iaa.LinearContrast((0.5, 2.0))  # 明度変換
        elif AUGMENTATION == 'gnoise':
            imgaug_aug = iaa.AdditiveGaussianNoise(
                scale=[0.05 * 255, 0.2 * 255])  # Gaussian Noise
        elif AUGMENTATION == 'lnoise':
            imgaug_aug = iaa.AdditiveLaplaceNoise(
                scale=[0.05 * 255, 0.2 * 255])  # LaplaceNoise
        elif AUGMENTATION == 'pnoise':
            imgaug_aug = iaa.AdditivePoissonNoise(
                lam=(16.0, 48.0), per_channel=True)  # PoissonNoise
        elif AUGMENTATION == 'flatten':
            imgaug_aug = iaa.GaussianBlur(sigma=(0.5, 1.0))  # blur: ぼかし (平滑化)
        elif AUGMENTATION == 'sharpen':
            imgaug_aug = iaa.Sharpen(alpha=(0, 1.0),
                                     lightness=(0.75,
                                                1.5))  # sharpen images (鮮鋭化)
        elif AUGMENTATION == 'emboss':
            imgaug_aug = iaa.Emboss(alpha=(0, 1.0),
                                    strength=(0, 2.0))  # Edge 強調
        elif AUGMENTATION == 'invert':
            imgaug_aug = iaa.Invert(1.0)  # 色反転 <= これがうまく行かないので自分で作った。
        elif AUGMENTATION == 'someof':  # 上記のうちのどれか1つ
            imgaug_aug = iaa.SomeOf(
                1,
                [
                    iaa.Affine(rotate=(-90, 90), order=1, mode="edge"),
                    iaa.Fliplr(1.0),
                    iaa.Affine(translate_percent={"x": (-0.125, 0.125)},
                               order=1,
                               mode="edge"),
                    iaa.Affine(translate_percent={"y": (-0.125, 0.125)},
                               order=1,
                               mode="edge"),
                    iaa.Affine(scale={
                        "x": (0.8, 1.2),
                        "y": (0.8, 1.2)
                    },
                               order=1,
                               mode="edge"),
                    iaa.LogContrast((0.5, 1.5)),
                    iaa.LinearContrast((0.5, 2.0)),
                    iaa.AdditiveGaussianNoise(scale=[0.05 * 255, 0.25 * 255]),
                    iaa.AdditiveLaplaceNoise(scale=[0.05 * 255, 0.25 * 255]),
                    iaa.AdditivePoissonNoise(lam=(16.0, 48.0),
                                             per_channel=True),
                    iaa.GaussianBlur(sigma=(0.5, 1.0)),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
                    iaa.Invert(1.0)  # 14
                ])
        elif AUGMENTATION == 'plural':  # 異なる系統の変換を複数(1つの変換あとに画素値がマイナスになるとError..)
            imgaug_aug = self.randomDataAugument(2)
        elif AUGMENTATION == 'fortest':  # plural - invert (色反転) (test 用)
            imgaug_aug = self.randomTestAugument(2)
        else:
            print("現在 imgaug で選択できる DA のモードは以下の通りです。")
            print(self.imgaug_aug_list, "\n")
            raise ValueError("予期されないモードが選択されています。")

        aug_data = imgaug_aug.augment_images(data)
        aug_data = np.clip(aug_data, 0, 255)

        return aug_data, label