Ejemplo n.º 1
0
    def test_augment_images__deterministic(self):
        rs = np.random.RandomState(1)
        images = rs.randint(0, 255, size=(32, 4, 4, 3), dtype=np.uint8)

        for deterministic in [False, True]:
            aug = iaa.MultiplyHueAndSaturation(mul=(0.1, 5.0),
                                               deterministic=deterministic)
            images_aug1 = aug.augment_images(images)
            images_aug2 = aug.augment_images(images)
            equal = np.array_equal(images_aug1, images_aug2)
            if deterministic:
                assert equal
            else:
                assert not equal

            aug = iaa.MultiplyHueAndSaturation(mul_hue=(0.1, 5.0),
                                               mul_saturation=(0.1, 5.0),
                                               deterministic=deterministic)
            images_aug1 = aug.augment_images(images)
            images_aug2 = aug.augment_images(images)
            equal = np.array_equal(images_aug1, images_aug2)
            if deterministic:
                assert equal
            else:
                assert not equal
def main():
    image = ia.quokka_square((128, 128))
    images_aug = []

    for mul in np.linspace(0.0, 2.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    for mul_hue in np.linspace(0.0, 5.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul_hue=mul_hue)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    for mul_saturation in np.linspace(0.0, 5.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul_saturation=mul_saturation)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    ia.imshow(ia.draw_grid(images_aug, rows=3))

    images_aug = []
    images_aug.extend(iaa.MultiplyHue().augment_images([image] * 10))
    images_aug.extend(iaa.MultiplySaturation().augment_images([image] * 10))
    ia.imshow(ia.draw_grid(images_aug, rows=2))
Ejemplo n.º 3
0
    def __init__(self,
                 coco_dir,
                 set_name='val2014',
                 img_size=416,
                 multiscale=False,
                 phase='Test'):
        super().__init__(coco_dir, set_name, img_size)

        self.coco_dir = coco_dir
        self.set_name = set_name
        self.img_size = img_size
        self.max_objects = 100
        self.min_size = img_size - 3 * 32
        self.max_size = img_size + 3 * 32

        self.multiscale = multiscale
        self.batch_count = 0
        self.augmentation = False

        if phase == 'Train':
            self.augmentation = True

            self.aug_seq = iaa.Sequential([
                iaa.Affine(
                    scale=(0.8, 1.2),
                    rotate=(-10, 10),
                    shear=(-15, 15),
                    translate_percent=(-0.05, 0.05),
                ),
                iaa.Fliplr(p=0.5),
                iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True),
            ])

        self.mosaic = self.augmentation
 def __init__(self):
     self.seq = iaa.Sequential(
         [
             # iaa.Fliplr(0.5), # horizontal flips
             # Small gaussian blur with random sigma between 0 and 0.5.
             # But we only blur about 50% of all images.
             iaa.GaussianBlur(sigma=(0, 0.5)),
             iaa.MotionBlur(k=[5, 12], angle=[-45, 45]),
             # Strengthen or weaken the contrast in each image.
             iaa.Alpha([0.25, 0.35, 0.55],
                       iaa.Sequential([
                           iaa.GaussianBlur(sigma=(60, 100)),
                           iaa.LinearContrast((1, 3)),
                           iaa.Add((0, 30))
                       ])),
             #iaa.Lambda(radial_blur),
             # Add gaussian noise.
             # For 50% of all images, we sample the noise once per pixel.
             # For the other 50% of all images, we sample the noise per pixel AND
             # channel. This can change the color (not only brightness) of the
             # pixels.
             iaa.LinearContrast((0.5, 1.0)),
             iaa.MultiplyHueAndSaturation((0.5, 1.5))
             # iaa.Alpha([0.25, 0.35], iaa.Clouds()),
         ],
         random_order=False)
Ejemplo n.º 5
0
    def __init__(self, data_root, image_shape, plus1, mode, \
                 use_subset=False, no_flow=False, precomputed_val=None, \
                 sample_length=5):
        self.no_flow = no_flow
        self.mode = mode
        self.precomputed_val = precomputed_val
        self.sample_length = sample_length
        assert self.mode in ['train', 'val']
        if self.precomputed_val is not None:
            assert self.mode == 'val'
        self.data_root = data_root
        if plus1:
            self.image_shape = [image_shape[0] + 1, image_shape[1] + 1]
        else:
            self.image_shape = list(image_shape)
        setname = '{}_videos_subset.txt' if use_subset else '{}_videos.txt'
        setname = setname.format(self.mode)

        with open(os.path.join(self.data_root, 'frame_corr.json'), 'r') as f:
            self.frame_corr = json.load(f)
        with open(os.path.join(self.data_root, setname), 'r') as f:
            self.samples = self.parse(f)
        #self.samples = self.samples[:240]
        self.dataset_length = len(self.samples)

        # apply to fg, bg
        self.pixel_aug = iaa.Sequential([
            iaa.MultiplyHueAndSaturation(mul=iap.TruncatedNormal(
                1.0, 0.2, 0.5, 1.5)),  # mean, std, low, high
            iaa.GammaContrast(gamma=iap.TruncatedNormal(1.0, 0.2, 0.5, 1.5)),
            iaa.AddToHue(value=iap.TruncatedNormal(0.0, 0.1 * 100, -0.2 *
                                                   255, 0.2 * 255)),
        ])
        self.jpeg_aug = iaa.Sometimes(
            0.6, iaa.JpegCompression(compression=(70, 99)))
Ejemplo n.º 6
0
def YOLO():
    """
    Data augmentation model for YOLOv3 training
    """
    return iaa.Sequential([
        iaa.KeepSizeByResize(
            iaa.Affine(
                scale=iap.Normal(1, 0.125),
                translate_percent=0.1,
                cval=128,
            )),
        iaa.Fliplr(0.5),
        iaa.Resize({
            "height": iap.Normal(1, 0.1),
            "width": iap.Normal(1, 0.1)
        }),
        iaa.Resize({
            "longer-side": 416,
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(416, 416, pad_cval=128),
        iaa.MultiplyHueAndSaturation(mul_hue=iap.Uniform(0, 2),
                                     mul_saturation=iap.Uniform(1 / 1.5, 1.5)),
        iaa.AssertShape((None, 416, 416, 3)),
    ])
Ejemplo n.º 7
0
    def __init__(self, data_dir, batch_size, image_shape, training=True):
        """
        Keras Sequence object to train a model on larger-than-memory data.
            @:param: data_dir: directory in which we have got the kitti images and the corresponding masks
            @:param: batch_size: define the number of training samples to be propagated.
            @:param: image_shape: shape of the input image
        """

        self.batch_size = batch_size
        self.image_shape = image_shape
        self.training = training
        self.image_paths = glob(os.path.join(data_dir, 'image_2', '*.png'))
        #print(self.image_paths)       
        print("*****************[DATA INFO]*****************")
        if (training):
            print("Found " + str(len(self.image_paths)) + " training images")
        else:
            print("Found " + str(len(self.image_paths)) + " validation images")
        print("*********************************************")
        
        if (training):
            self.label_paths = {re.sub(r'_(lane|road)_', '_', os.path.basename(path)): path
                for path in glob(os.path.join(data_dir, 'gt_image_2', '*_road_*.png'))}
            #glob(os.path.join(data_dir, 'gt_image_2', '*.png'))
        else:
            self.label_paths = {os.path.basename(path): path
                for path in glob(os.path.join(data_dir, 'gt_image_2', '*.png'))}
                    
        #print(self.label_paths)        
        self.sometimes = lambda aug: iaa.Sometimes(0.5, aug)

        self.aug_pipe = iaa.Sequential(
            [
                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.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                               iaa.OneOf([
                                   iaa.Dropout((0.01, 0.1), per_channel=0.5),  # randomly remove up to 10% of the pixels
                               ]),
                               iaa.Add((-10, 10), per_channel=0.5),
                               # change brightness of images (by -10 to 10 of original value)
                               iaa.Multiply((0.5, 1.5), per_channel=0.5),
                               iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),  # improve or worsen the contrast
                               iaa.MultiplyHueAndSaturation((0.5, 1.5)),
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
Ejemplo n.º 8
0
    def hue_transform1(self, image):
        z = image.shape[0]
        para = random.randrange(6, 14, 1)
        hue_m = para / 10
        aug = iaa.MultiplyHueAndSaturation((hue_m, hue_m))
        for stack in range(z):
            image[stack] = aug.augment_image(image[stack])

        return image
Ejemplo n.º 9
0
def chapter_augmenters_multiplyhueandsaturation():
    fn_start = "color/multiplyhueandsaturation"

    aug = iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)

    aug = iaa.MultiplyHueAndSaturation(mul_hue=(0.5, 1.5))
    run_and_save_augseq(fn_start + "_mul_hue.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)

    aug = iaa.MultiplyHueAndSaturation(mul_saturation=(0.5, 1.5))
    run_and_save_augseq(fn_start + "_mul_saturation.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
Ejemplo n.º 10
0
 def test_returns_correct_objects__mul(self):
     aug = iaa.MultiplyHueAndSaturation((0.9, 1.1), per_channel=True)
     assert isinstance(aug, iaa.WithHueAndSaturation)
     assert isinstance(aug.children, iaa.Sequential)
     assert len(aug.children) == 1
     assert isinstance(aug.children[0], iaa.Multiply)
     assert isinstance(aug.children[0].mul, iap.Uniform)
     assert np.isclose(aug.children[0].mul.a.value, 0.9)
     assert np.isclose(aug.children[0].mul.b.value, 1.1)
     assert isinstance(aug.children[0].per_channel, iap.Deterministic)
     assert aug.children[0].per_channel.value == 1
Ejemplo n.º 11
0
 def test_returns_correct_objects__mul_saturation(self):
     aug = iaa.MultiplyHueAndSaturation(mul_saturation=(0.9, 1.1))
     assert isinstance(aug, iaa.WithHueAndSaturation)
     assert isinstance(aug.children, iaa.Sequential)
     assert len(aug.children) == 1
     assert isinstance(aug.children[0], iaa.WithChannels)
     assert aug.children[0].channels == [1]
     assert len(aug.children[0].children) == 1
     assert isinstance(aug.children[0].children[0], iaa.Multiply)
     assert isinstance(aug.children[0].children[0].mul, iap.Uniform)
     assert np.isclose(aug.children[0].children[0].mul.a.value, 0.9)
     assert np.isclose(aug.children[0].children[0].mul.b.value, 1.1)
def ol_aug(image, mask):
    # ia.seed(seed)

    # Example batch of images.
    # The array has shape (32, 64, 64, 3) and dtype uint8.
    images = image  # B,H,W,C
    masks = mask  # B,H,W,C

    # print('In Aug',images.shape,masks.shape)
    combo = np.concatenate((images, masks), axis=3)
    # print('COMBO: ',combo.shape)

    seq_all = iaa.Sequential([
        iaa.Fliplr(0.5),  # horizontal flips
        # iaa.PadToFixedSize(width=crop_size[0], height=crop_size[1]),
        # iaa.CropToFixedSize(width=crop_size[0], height=crop_size[1]),
        iaa.Affine(
            scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
            # scale images to 90-110% of their size, individually per axis
            translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
            # translate by -10 to +10 percent (per axis)
            rotate=(-5, 5),  # rotate by -5 to +5 degrees
            shear=(-3, 3),  # shear by -3 to +3 degrees
        ),
        # iaa.Cutout(nb_iterations=(1, 5), size=0.2, cval=0, squared=False),
    ], random_order=False)  # apply augmenters in random order

    seq_f = iaa.Sequential([
        iaa.Sometimes(0.5,
                      iaa.OneOf([
                          iaa.GaussianBlur((0.0, 3.0)),
                          iaa.MotionBlur(k=(3, 20)),
                      ]),
                      ),
        iaa.Sometimes(0.5,
                      iaa.OneOf([
                          iaa.Multiply((0.8, 1.2), per_channel=0.2),
                          iaa.MultiplyBrightness((0.5, 1.5)),
                          iaa.LinearContrast((0.5, 2.0), per_channel=0.2),
                          iaa.BlendAlpha((0., 1.), iaa.HistogramEqualization()),
                          iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=0.2),
                      ]),
                      ),
    ], random_order=False)

    combo_aug = np.array(seq_all.augment_images(images=combo))
    # print('combo_au: ', combo_aug.shape)
    images_aug = combo_aug[:, :, :, :3]
    masks_aug = combo_aug[:, :, :, 3:]
    images_aug = seq_f.augment_images(images=images_aug)

    return images_aug, masks_aug
Ejemplo n.º 13
0
def data_augmentation(path):
    ia.seed(2)

    seq = iaa.Sequential([
        iaa.Sometimes(0.5, iaa.Grayscale(alpha=(0.1, 0.5))),
        iaa.Sometimes(0.5, iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        iaa.Sometimes(0.5,
                      iaa.MultiplyHueAndSaturation(mul_saturation=(0.5, 1.5))),
        iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 2.0))),
        iaa.Sometimes(0.8, iaa.MultiplyBrightness((0.5, 1.5))),
        iaa.AddToBrightness((-30, 30)),
        iaa.Sometimes(0.6,
                      iaa.MultiplyHueAndSaturation(mul_saturation=(0.5, 1.5)))
    ],
                         random_order=True)

    i = 0
    for fname in os.listdir(path):

        try:
            img = imageio.imread(os.path.join(path, fname), pilmode="RGB")
            print(i)
            if i % 5 == 0:
                img_aug = seq.augment_image(img)
                imageio.imwrite(
                    os.path.join(path, fname.replace(".jpg", "_imgaug.jpg")),
                    img_aug)
                fname_txt = fname.replace('.jpg', '.txt')
                print(
                    os.path.join(path,
                                 fname_txt.replace(".txt", "_imgaug.txt")))
                shutil.copyfile(
                    os.path.join(path, fname_txt),
                    os.path.join(path,
                                 fname_txt.replace(".txt", "_imgaug.txt")))

        except:
            print('Error reading img')
        i += 1
def augmentor(images):
    'Apply data augmentation'
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.9),  # horizontally flip 50% of all images
            iaa.Flipud(0.9),  # vertically flip 20% of all images
            iaa.ElasticTransformation(alpha=(10, 20), sigma=6),
            iaa.GammaContrast((0.8, 1.2)),
            iaa.MultiplyHueAndSaturation(mul_hue=(0.6, 1.4)),
            iaa.GaussianBlur((0.0, 0.8)),
        ],
        random_order=True)
    return seq.augment_images(images)
Ejemplo n.º 15
0
    def __init__(self, key_source='image', key_target=None):
        super(RandomColorJitter, self).__init__(key_source=key_source,
                                                key_target=key_target)

        self.sequence = iaa.Sequential([
            iaa.Sometimes(
                0.8,
                iaa.Sequential([
                    iaa.MultiplyBrightness((0.8, 1.25)),
                    iaa.MultiplyHueAndSaturation(mul_hue=(0.8, 1.25),
                                                 mul_saturation=(0.8, 1.25))
                ])),
            iaa.Sometimes(0.2, iaa.Grayscale())
        ])
Ejemplo n.º 16
0
    def test_augment_images__mul_hue_and_mul_saturation(self):
        # this is almost identical to test_augment_images__mul
        # only
        #     aug = ...
        # and
        #     image_hsv[...] *= 1.2
        # have been changed

        aug = iaa.MultiplyHueAndSaturation(mul_hue=1.5,
                                           mul_saturation=1.6)  # changed

        # example image
        image = np.arange(0, 255).reshape((1, 255, 1)).astype(np.uint8)
        image = np.tile(image, (1, 1, 3))
        image[..., 0] += 0
        image[..., 1] += 5
        image[..., 2] += 10

        # compute expected output
        image_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
        image_hsv = image_hsv.astype(np.int16)  # simulate WithHueAndSaturation
        image_hsv[..., 0] = ((image_hsv[..., 0].astype(np.float32) / 180) *
                             255).astype(np.int16)
        image_hsv = image_hsv.astype(np.float32)  # simulate Multiply

        image_hsv[..., 0] *= 1.5
        image_hsv[..., 1] *= 1.6  # changed over __mul
        image_hsv = np.round(image_hsv).astype(np.int16)

        image_hsv[..., 0] = np.mod(image_hsv[..., 0], 255)
        image_hsv[..., 0] = ((image_hsv[..., 0].astype(np.float32) / 255) *
                             180).astype(np.int16)
        image_hsv[..., 1] = np.clip(image_hsv[..., 1], 0, 255)

        image_hsv = image_hsv.astype(np.uint8)
        image_expected = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2RGB)
        assert not np.array_equal(image_expected, image)

        # augment and verify
        images_aug = aug.augment_images(np.stack([image, image], axis=0))
        assert ia.is_np_array(images_aug)
        for image_aug in images_aug:
            assert image_aug.shape == (1, 255, 3)
            diff = np.abs(image_aug.astype(np.int16) - image_expected)
            assert np.all(diff <= 1)
Ejemplo n.º 17
0
def _load_augmentation_aug_non_geometric():
    return iaa.Sequential([
        iaa.Sometimes(0.3, iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))),
        iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0, 3.0))),
        iaa.Sometimes(0.2, iaa.MotionBlur(k=15, angle=[-45, 45])),
        iaa.Sometimes(0.2, iaa.MultiplyHue((0.5, 1.5))),
        iaa.Sometimes(0.2, iaa.MultiplySaturation((0.5, 1.5))),
        iaa.Sometimes(
            0.34, iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)),
        iaa.Sometimes(0.34, iaa.Grayscale(alpha=(0.0, 1.0))),
        iaa.Sometimes(0.2, iaa.ChangeColorTemperature((1100, 10000))),
        iaa.Sometimes(0.1, iaa.GammaContrast((0.5, 2.0))),
        iaa.Sometimes(0.2, iaa.SigmoidContrast(gain=(3, 10),
                                               cutoff=(0.4, 0.6))),
        iaa.Sometimes(0.1, iaa.CLAHE()),
        iaa.Sometimes(0.1, iaa.HistogramEqualization()),
        iaa.Sometimes(0.2, iaa.LinearContrast((0.5, 2.0), per_channel=0.5)),
        iaa.Sometimes(0.1, iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)))
    ])
Ejemplo n.º 18
0
 def load_augmentation_aug_non_geometric():
     return iaa.Sequential([
         iaa.Sometimes(
             0.5,
             iaa.AdditiveGaussianNoise(loc=0,
                                       scale=(0.0, 0.05 * 255),
                                       per_channel=0.5)),
         iaa.Sometimes(
             0.5,
             iaa.OneOf([
                 iaa.GaussianBlur(sigma=(0.0, 3.0)),
                 iaa.GaussianBlur(sigma=(0.0, 5.0))
             ])),
         iaa.Sometimes(0.5, iaa.MultiplyAndAddToBrightness(mul=(0.4, 1.7))),
         iaa.Sometimes(0.5, iaa.GammaContrast((0.4, 1.7))),
         iaa.Sometimes(0.5, iaa.Multiply((0.4, 1.7), per_channel=0.5)),
         iaa.Sometimes(0.5, iaa.MultiplyHue((0.4, 1.7))),
         iaa.Sometimes(
             0.5, iaa.MultiplyHueAndSaturation((0.4, 1.7),
                                               per_channel=True)),
         iaa.Sometimes(0.5, iaa.LinearContrast((0.4, 1.7), per_channel=0.5))
     ])
Ejemplo n.º 19
0
    def __init__(self, da):
        assert check_data_aug(da), 'Parameters for custom data augmentation missing. Should have: {}'.format(
                                    keys_data_aug)

        aug_geometric = [iaa.Affine(scale=(da['scalem'], da['scaleM']),
                                    translate_percent={'x': (-da['trans'], da['trans']),
                                                       'y': (-da['trans'], da['trans'])},
                                    rotate=(-da['rot'], da['rot']),
                                    shear=(-da['shear'], da['shear']),
                                    cval=FILL_COLOR),
                         iaa.PerspectiveTransform(scale=(0, da['pers']),
                                                  cval=FILL_COLOR, keep_size=True)]

        aug_camera = [iaa.GaussianBlur(sigma=(0, da['sigma'])),
                      iaa.MotionBlur(k=(da['mot_km'], da['mot_kM']),
                                     angle=(-da['mot_an'], da['mot_an']),
                                     direction=(da['mot_dm'], da['mot_dM'])),
                      iaa.JpegCompression(compression=(da['jpegm'], da['jpegM'])),
                      iaa.LinearContrast(alpha=(da['con_alpham'], da['con_alphaM']),
                                         per_channel=da['con_chan']),
                      iaa.MultiplyHueAndSaturation(mul=(da['col_mulm'], da['col_mulM']),
                                                   per_channel=da['col_chan'],
                                                   from_colorspace='BGR'),
                      iaa.AddToHueAndSaturation((da['col_addm'], da['col_addM']),
                                                per_channel=da['col_chan'])
                      ]

        cutout = iaa.Cutout(nb_iterations=(0, da['co_num']),
                            size=(da['co_sm'], da['co_sM']),
                            squared=False,
                            cval=FILL_COLOR)

        # Create a mix of all others
        self.augmenter = iaa.Sequential([iaa.SomeOf((0, 1), aug_geometric),  # none or 1
                                         iaa.SomeOf((0, len(aug_camera) - 2), aug_camera),  # from none to all-2
                                         cutout], random_order=True)  # mix the apply order
Ejemplo n.º 20
0
    .loc[lambda df: df.label == "ASSIETTE_26"]
    .pipe(RandomAssignment("tray_name"))
)

train_set = all_annotations.loc[lambda df: df.random_split == "train"]
support_set = train_set.assign(crop_coordinates=lambda df: df[["x1", "y1", "x2", "y2"]].agg(list, axis=1))
val_set = all_annotations.loc[lambda df: df.random_split == "val"]

#%% Init training
query_preprocessing = iaa.Sequential(
    [
        iaa.Resize({"longer-side": 416, "shorter-side": "keep-aspect-ratio"}),
        iaa.PadToFixedSize(416, 416),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.MultiplyHueAndSaturation(mul_hue=iap.Uniform(0, 2), mul_saturation=iap.Uniform(1 / 1.5, 1.5)),
        iaa.AssertShape((None, 416, 416, 3)),
    ]
)

support_preprocessing = iaa.Sequential(
    [
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(rotate=(-180, 180)),
        iaa.Resize({"longer-side": 128, "shorter-side": "keep-aspect-ratio"}),
        iaa.PadToFixedSize(128, 128, pad_mode="symmetric"),
        iaa.MultiplyHueAndSaturation(mul_hue=iap.Uniform(0, 2), mul_saturation=iap.Uniform(1 / 1.5, 1.5)),
        iaa.AssertShape((None, 128, 128, 3)),
    ]
)
Ejemplo n.º 21
0
##augmentation functions used in sequences pipeline
seq1 = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=(0, 0.3), per_channel=True),
    iaa.Add((-25, 60)),
    iaa.JpegCompression(compression=(30, 87)),
    iaa.MedianBlur(k=(1, 3)),
    iaa.PiecewiseAffine(scale=(0.01, 0.05)),
    iaa.Fliplr(0.5),
    iaa.Flipud(0.5),
    iaa.CoarseDropout((0.0, 0.09), size_percent=(0.02, 0.15))
],
                      random_order=True)

seq2 = iaa.Sequential([
    iaa.GaussianBlur(sigma=(0, 3)),
    iaa.MultiplyHueAndSaturation((0.5, 1.5)),
    iaa.JpegCompression(compression=(45, 87)),
    iaa.PiecewiseAffine(scale=(0.01, 0.05)),
    iaa.SaltAndPepper(p=(0.1, 0.15), per_channel=True),
    iaa.Fliplr(0.5),
    iaa.Flipud(0.5),
    iaa.CoarseDropout((0.0, 0.09), size_percent=(0.02, 0.15))
],
                      random_order=True)

##divide the dataset into two batches
first_image_list = []
second_image_list = []
first_segmap_list = []
second_segmap_list = []
j = len(image_name_list)
Ejemplo n.º 22
0
path = 'idcard/'

sqe_list = [
    iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
    iaa.WithChannels(0, iaa.Add((-50, 50))),
    iaa.WithChannels(1, iaa.Add((-50, 50))),
    iaa.WithChannels(2, iaa.Add((-50, 50))),
    iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB"),
    iaa.Add((-80, 80), per_channel=0.5),
    iaa.Multiply((0.5, 1.5), per_channel=0.5),
    iaa.AverageBlur(k=((5), (1, 3))),
    iaa.AveragePooling(2),
    iaa.AddElementwise((-20, -5)),
    iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
    iaa.JpegCompression(compression=(50, 99)),
    iaa.MultiplyHueAndSaturation(mul_hue=(0.5, 1.5)),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50))),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50)),
                               to_colorspace=[iaa.CSPACE_Lab, iaa.CSPACE_HSV]),
    iaa.MaxPooling(2),
    iaa.MinPooling((1, 2)),
    # iaa.Superpixels(p_replace=(0.1, 0.2), n_segments=(16, 128)),
    iaa.Clouds(),
    iaa.Fog(),
    iaa.AdditiveGaussianNoise(scale=0.1 * 255, per_channel=True),
    iaa.Dropout(p=(0, 0.2)),

    # iaa.WithChannels(0, iaa.Affine(rotate=(0, 0))),
    iaa.ChannelShuffle(0.35),
    iaa.WithColorspace(to_colorspace="HSV",
                       from_colorspace="RGB",
Ejemplo n.º 23
0
                                                per_channel=0.5),

                   ]),
                   # Dropout some pixels
                   iaa.OneOf([
                       iaa.Dropout((0.01, 0.1), per_channel=0.5),
                       iaa.CoarseDropout((0.01, 0.1),
                                         size_percent=(0.01, 0.5),
                                         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 = lambda aug: iaa.Sometimes(0.5, aug)

AUGMENTATIONS = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Flipud(0.5),
    sometimes(iaa.Affine(
        scale=(0.8, 1.2),
        rotate=(90),
        mode=ia.ALL)),
    sometimes(iaa.ElasticTransformation(alpha=(0.8, 1.2),\
                                        sigma=(9.0, 11.0))),
    sometimes(iaa.AdditiveGaussianNoise(scale=(0, 0.1))),
    sometimes(iaa.GaussianBlur((0, 0.1))),
    sometimes(iaa.MultiplyBrightness((0.65, 1.35))),
    sometimes(iaa.LinearContrast((0.5, 1.5))),
    sometimes(iaa.MultiplyHueAndSaturation((-1, 1)))
    ], random_order=True)

# instantiate datagen objects
train_datagen = ImageDataAugmentor(
    #     featurewise_center=True,
    #     featurewise_std_normalization=True,
    augment=AUGMENTATIONS,
    rescale=1. / 255,
    preprocess_input=None)

val_datagen = ImageDataAugmentor(rescale=1. / 255)

# define the ImageNet mean subtraction (in RGB order)
mean = np.array([123.68, 116.779, 103.939], dtype="float32")
# set the mean subtraction value for each of the data augmentation objects
Ejemplo n.º 25
0
    def __getitem__(self, index):
        mosaic_pro = random.random()
        if mosaic_pro > 0:
            img_id = self.images[index]
            img, labels = self.load_mosaic(index)
            all_ann = []
            for da_label in labels:
                da_label = da_label.tolist()
                for da_l in da_label:
                    all_ann.append(da_l)
            num_objs = min(len(all_ann), self.max_objs)
        else:
            positive_aug = random.random()
            if positive_aug > 2:
                index1 = random.randint(0, self.num_samples - 1)
                # chartlet_dir = "/home/raid5/daming/HandDataMix/TrainImg/AnnImgMix"
                img_id = self.images[index]
                img_id1 = self.images[index1]

                file_name = self.coco.loadImgs(ids=[img_id])[0]['file_name']
                file_name1 = self.coco.loadImgs(ids=[img_id1])[0]['file_name']

                path_num = random.random()
                img_path = os.path.join(self.img_dir, file_name)
                img_path1 = os.path.join(self.img_dir, file_name1)
                # if path_num > 0.5:
                #   img_path = os.path.join(chartlet_dir, file_name)

                ann_ids = self.coco.getAnnIds(imgIds=[img_id])
                ann_ids1 = self.coco.getAnnIds(imgIds=[img_id1])

                anns = self.coco.loadAnns(ids=ann_ids)
                anns1 = self.coco.loadAnns(ids=ann_ids1)

                img = cv2.imread(img_path)
                img1 = cv2.imread(img_path1)
                hand_num = len(anns1)
                if hand_num > 0:
                    for ann1 in anns1:
                        ran_id = random.randint(0, 26000)
                        hand_x = ann1['bbox'][0]
                        hand_y = ann1['bbox'][1]
                        hand_w = ann1['bbox'][2]
                        hand_h = ann1['bbox'][3]
                        temp = img1[hand_y:hand_y + hand_h,
                                    hand_x:hand_x + hand_w]
                        temp_h, temp_w, c = temp.shape
                        src_h, src_w, src_c = img.shape
                        for n in range(100):
                            min_src = min(src_w, src_h)
                            max_temp = max(temp_h, temp_w)
                            if (max_temp > 0.5 * min_src):
                                break
                            if (src_w < temp_w or src_h < temp_h):
                                break
                            x_tmp = random.randint(0, src_w - temp_w)
                            y_tmp = random.randint(0, src_h - temp_h)
                            src_rect = [
                                x_tmp, y_tmp, x_tmp + temp_w, y_tmp + temp_h
                            ]
                            iou_all = 0
                            for gt in anns:
                                gt = [
                                    gt['bbox'][0], gt['bbox'][1],
                                    gt['bbox'][0] + gt['bbox'][2],
                                    gt['bbox'][1] + gt['bbox'][3]
                                ]
                                iou = self.compute_iou(gt, src_rect)
                                iou_all = iou_all + iou
                                # print(iou_all)
                                if iou_all == 0:
                                    img[y_tmp:y_tmp + temp_h,
                                        x_tmp:x_tmp + temp_w] = temp
                                    a = {
                                        'bbox': [x_tmp, y_tmp, temp_w, temp_h],
                                        'category_id': 1
                                    }
                                    anns.append(a)
                                    break
                    num_objs = min(len(anns), self.max_objs)
            else:
                img_id = self.images[index]
                file_name = self.coco.loadImgs(ids=[img_id])[0]['file_name']
                # daming_dir = "/home/raid5/daming/HandDataMix/TrainImg/AnnImgMix"
                img_path = os.path.join(self.img_dir, file_name)
                # img_path1 = os.path.join(daming_dir, file_name)
                ann_ids = self.coco.getAnnIds(imgIds=[img_id])
                anns = self.coco.loadAnns(ids=ann_ids)
                num_objs = min(len(anns), self.max_objs)
                img = cv2.imread(img_path)
                # daming_num = random.random()
                # if daming_num > 0.5:
                #   img = cv2.imread(img_path)
                # else:
                #   img = cv2.imread(img_path1)

        gray_pro = random.random()
        if gray_pro > 2:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        if self.opt.keep_res:
            input_h = (height | self.opt.pad) + 1
            input_w = (width | self.opt.pad) + 1
            s = np.array([input_w, input_h], dtype=np.float32)
        else:
            s = max(img.shape[0], img.shape[1]) * 1.0
            input_h, input_w = self.opt.input_h, self.opt.input_w

        flipped = False
        if self.split == 'train':
            if not self.opt.not_rand_crop:
                s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                # s = s * np.random.choice(np.arange(0.3, 1.2, 0.1))
                w_border = self._get_border(128, img.shape[1])
                h_border = self._get_border(128, img.shape[0])
                c[0] = np.random.randint(low=w_border,
                                         high=img.shape[1] - w_border)
                c[1] = np.random.randint(low=h_border,
                                         high=img.shape[0] - h_border)
            else:
                sf = self.opt.scale
                cf = self.opt.shift
                c[0] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                c[1] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)

            if np.random.random() < self.opt.flip:
                flipped = True
                img = img[:, ::-1, :]
                c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)
        iaa_pro = random.random()
        if iaa_pro > 2:
            aug_seq = iaa.Sequential(
                [iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)])
            #   aug_seq = iaa.Sequential([
            #     iaa.Sometimes(
            #         0.5,
            #         iaa.GaussianBlur(sigma=(0, 0.5))
            #     ),
            #     iaa.LinearContrast((0.75, 1.5)),
            #     iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
            #     iaa.Multiply((0.8, 1.2), per_channel=0.2),
            # ], random_order=True)
            inp, _ = aug_seq(image=inp, bounding_boxes=None)
        inp = (inp.astype(np.float32) / 255.)
        if self.split == 'train' and not self.opt.no_color_aug:
            color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)
        inp = (inp - self.mean) / self.std
        inp = inp.transpose(2, 0, 1)

        output_h = input_h // self.opt.down_ratio
        output_w = input_w // self.opt.down_ratio
        num_classes = self.num_classes
        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])

        hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32)
        # ind is the center index, reg is the offset of center point in extracted feature maps
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        cat_spec_wh = np.zeros((self.max_objs, num_classes * 2),
                               dtype=np.float32)
        cat_spec_mask = np.zeros((self.max_objs, num_classes * 2),
                                 dtype=np.uint8)

        draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \
            draw_umich_gaussian

        gt_det = []
        for k in range(num_objs):
            if mosaic_pro > 0:
                ann = all_ann[k]
                bbox = np.array([
                    float(ann[0]),
                    float(ann[1]),
                    float(ann[2]),
                    float(ann[3])
                ],
                                dtype=np.float32)
            else:
                ann = anns[k]
                bbox = self._coco_box_to_bbox(ann['bbox'])
            cls_id = 0
            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1
            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1)
            bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1)
            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]
            if h > 0 and w > 0:
                # print("- h : ", h," - w : ", w)
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                radius = self.opt.hm_gauss if self.opt.mse_loss else radius
                ct = np.array([(bbox[0] + bbox[2]) / 2,
                               (bbox[1] + bbox[3]) / 2],
                              dtype=np.float32)
                ct_int = ct.astype(np.int32)
                draw_gaussian(hm[cls_id], ct_int, radius)
                wh[k] = 1. * w, 1. * h
                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1
                cat_spec_wh[k, cls_id * 2:cls_id * 2 + 2] = wh[k]
                cat_spec_mask[k, cls_id * 2:cls_id * 2 + 2] = 1
                if self.opt.dense_wh:
                    draw_dense_reg(dense_wh, hm.max(axis=0), ct_int, wh[k],
                                   radius)
                gt_det.append([
                    ct[0] - w / 2, ct[1] - h / 2, ct[0] + w / 2, ct[1] + h / 2,
                    1, cls_id
                ])
        ret = {
            'input': inp,
            'hm': hm,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh
        }
        if self.opt.dense_wh:
            hm_a = hm.max(axis=0, keepdims=True)
            dense_wh_mask = np.concatenate([hm_a, hm_a], axis=0)
            ret.update({'dense_wh': dense_wh, 'dense_wh_mask': dense_wh_mask})
            del ret['wh']
        elif self.opt.cat_spec_wh:
            ret.update({
                'cat_spec_wh': cat_spec_wh,
                'cat_spec_mask': cat_spec_mask
            })
            del ret['wh']
        if self.opt.reg_offset:
            ret.update({'reg': reg})
        if self.opt.debug > 0 or not self.split == 'train':
            gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
                np.zeros((1, 6), dtype=np.float32)
            meta = {'c': c, 's': s, 'gt_det': gt_det, 'img_id': img_id}
            ret['meta'] = meta

        return ret
Ejemplo n.º 26
0
import imgaug.augmenters as iaa
import random

import numpy as np
import cv2
from PIL import Image

aug_transform = iaa.SomeOf((0, None), [
    iaa.OneOf([
        iaa.MultiplyAndAddToBrightness(mul=(0.3, 1.6), add=(-50, 50)),
        iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True),
        iaa.ChannelShuffle(0.5),
        iaa.RemoveSaturation(),
        iaa.Grayscale(alpha=(0.0, 1.0)),
        iaa.ChangeColorTemperature((1100, 35000)),
    ]),
    iaa.OneOf([
        iaa.MedianBlur(k=(3, 7)),
        iaa.BilateralBlur(
            d=(3, 10), sigma_color=(10, 250), sigma_space=(10, 250)),
        iaa.MotionBlur(k=(3, 9), angle=[-45, 45]),
        iaa.MeanShiftBlur(spatial_radius=(5.0, 10.0),
                          color_radius=(5.0, 10.0)),
        iaa.AllChannelsCLAHE(clip_limit=(1, 10)),
        iaa.AllChannelsHistogramEqualization(),
        iaa.GammaContrast((0.5, 1.5), per_channel=True),
        iaa.GammaContrast((0.5, 1.5)),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
        iaa.HistogramEqualization(),
        iaa.Sharpen(alpha=0.5)
Ejemplo n.º 27
0
color_augmentation = args.color
geometric_augmentation = args.geometric
all_aumenters = args.all


augmenters = [
    iaa.Dropout(p=(0, 0.1)),
    iaa.CoarseDropout((0.01, 0.05), size_percent=0.1),
    iaa.Multiply((0.5, 1.3), per_channel=(0.2)),
    iaa.GaussianBlur(sigma=(0, 5)),
    iaa.AdditiveGaussianNoise(scale=((0, 0.2*255))),
    iaa.ContrastNormalization((0.5, 1.5)),
    iaa.Grayscale(alpha=((0.1, 1))),
    iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25),
    iaa.PerspectiveTransform(scale=(0.15)),
    iaa.MultiplyHueAndSaturation((0.7)),

    iaa.Affine(scale=((0.6, 1.2))),
    iaa.Affine(translate_percent=(-0.3, 0.3)),
    iaa.Affine(shear=(-25, 25)),
    iaa.Affine(translate_percent={"x": (-0.3, 0.3), "y": (-0.2, 0.2)}),
    iaa.Fliplr(1),
    iaa.Affine(scale={"x": (0.6, 1.4), "y": (0.6, 1.4)})
]
if all_aumenters > 0:
    some_of_all = iaa.SomeOf(all_aumenters, augmenters)


# pixel manipulation
color_augmenters = [
    iaa.Dropout(p=(0, 0.1)),
Ejemplo n.º 28
0
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
Ejemplo n.º 29
0
def simple_imgaug_example():
    image_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1'
    label_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1'
    images, labels = prepare_dataset(image_dir_path, label_dir_path)

    image_width, image_height = 200, 200

    # FIXME [decide] >> Before or after random transformation?
    # Preprocessing (normalization, standardization, etc).
    images_pp = images.astype(np.float)
    #images_pp /= 255.0
    images_pp = standardize_samplewise(images_pp)
    #images_pp = standardize_featurewise(images_pp)

    if True:
        augmenter = iaa.SomeOf(
            (1, 2),
            [
                iaa.OneOf([
                    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.1, 0.1),
                            'y': (-0.1, 0.1)
                        },  # Translate by -10 to +10 percent (per axis).
                        rotate=(-10, 10),  # Rotate by -10 to +10 degrees.
                        shear=(-5, 5),  # Shear by -5 to +5 degrees.
                        #order=[0, 1],  # Use nearest neighbour or bilinear interpolation (fast).
                        order=
                        0,  # Use nearest neighbour or bilinear interpolation (fast).
                        #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).
                        #mode='edge'  # Use any of scikit-image's warping modes (see 2nd image from the top for examples).
                    ),
                    #iaa.PiecewiseAffine(scale=(0.01, 0.05)),  # Move parts of the image around. Slow.
                    iaa.PerspectiveTransform(scale=(0.01, 0.1)),
                    iaa.ElasticTransformation(
                        alpha=(20.0, 50.0), sigma=(6.5, 8.5)
                    ),  # Move pixels locally around (with random strengths).
                ]),
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(
                        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.MotionBlur(k=(5, 11),
                                   angle=(0, 360),
                                   direction=(-1.0, 1.0),
                                   order=1),
                ]),
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.1 * 255, 0.5 * 255),
                        per_channel=False),  # Add Gaussian noise to images.
                    iaa.AdditiveLaplaceNoise(loc=0,
                                             scale=(0.1 * 255, 0.4 * 255),
                                             per_channel=False),
                    iaa.AdditivePoissonNoise(lam=(32, 96), per_channel=False),
                    iaa.CoarseSaltAndPepper(p=(0.1, 0.3),
                                            size_percent=(0.2, 0.9),
                                            per_channel=False),
                    iaa.CoarseSalt(p=(0.1, 0.3),
                                   size_percent=(0.2, 0.9),
                                   per_channel=False),
                    iaa.CoarsePepper(p=(0.1, 0.3),
                                     size_percent=(0.2, 0.9),
                                     per_channel=False),
                    iaa.CoarseDropout(p=(0.1, 0.3),
                                      size_percent=(0.05, 0.3),
                                      per_channel=False),
                ]),
                iaa.OneOf([
                    iaa.MultiplyHueAndSaturation(mul=(-10, 10),
                                                 per_channel=False),
                    iaa.AddToHueAndSaturation(value=(-255, 255),
                                              per_channel=False),
                    iaa.LinearContrast(
                        alpha=(0.5, 1.5),
                        per_channel=False),  # Improve or worsen the contrast.
                    iaa.Invert(p=1,
                               per_channel=False),  # Invert color channels.
                    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)
    elif False:
        augmenter = 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.
                # Crop images by -5% to 10% of their height/width.
                iaa.Sometimes(
                    0.5,
                    iaa.CropAndPad(percent=(-0.05, 0.1),
                                   pad_mode=ia.ALL,
                                   pad_cval=(0, 255))),
                iaa.Sometimes(
                    0.5,
                    iaa.Affine(
                        scale={
                            'x': (0.8, 1.2),
                            'y': (0.8, 1.2)
                        },  # Scale images to 80-120% of their size, individually per axis.
                        translate_percent={
                            'x': (-0.2, 0.2),
                            'y': (-0.2, 0.2)
                        },  # Translate by -20 to +20 percent (per axis).
                        rotate=(-45, 45),  # Rotate by -45 to +45 degrees.
                        shear=(-16, 16),  # Shear by -16 to +16 degrees.
                        order=[
                            0,
                            1
                        ],  # Use nearest neighbour or bilinear interpolation (fast).
                        cval=(
                            0, 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).
                    )),
                # 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.Sometimes(
                            0.5,
                            iaa.Superpixels(p_replace=(0, 1.0),
                                            n_segments=(20, 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(
                            (-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.
                        iaa.Grayscale(alpha=(0.0, 1.0)),
                        iaa.Sometimes(
                            0.5,
                            iaa.ElasticTransformation(alpha=(0.5, 3.5),
                                                      sigma=0.25)
                        ),  # Move pixels locally around (with random strengths).
                        iaa.Sometimes(
                            0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))
                        ),  # Sometimes move parts of the image around.
                        iaa.Sometimes(
                            0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                    ],
                    random_order=True)
            ],
            random_order=True)
    else:
        augmenter = iaa.Sequential([
            iaa.SomeOf(
                1,
                [
                    #iaa.Sometimes(0.5, iaa.Crop(px=(0, 100))),  # Crop images from each side by 0 to 16px (randomly chosen).
                    iaa.Sometimes(0.5, iaa.Crop(percent=(
                        0,
                        0.1))),  # Crop images by 0-10% of their height/width.
                    iaa.Fliplr(0.5),  # Horizontally flip 50% of the images.
                    iaa.Flipud(0.5),  # Vertically flip 50% of the images.
                    iaa.Sometimes(
                        0.5,
                        iaa.Affine(
                            scale={
                                'x': (0.8, 1.2),
                                'y': (0.8, 1.2)
                            },  # Scale images to 80-120% of their size, individually per axis.
                            translate_percent={
                                'x': (-0.2, 0.2),
                                'y': (-0.2, 0.2)
                            },  # Translate by -20 to +20 percent (per axis).
                            rotate=(-45, 45),  # Rotate by -45 to +45 degrees.
                            shear=(-16, 16),  # Shear by -16 to +16 degrees.
                            #order=[0, 1],  # Use nearest neighbour or bilinear interpolation (fast).
                            order=
                            0,  # Use nearest neighbour or bilinear interpolation (fast).
                            #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).
                            #mode='edge'  # Use any of scikit-image's warping modes (see 2nd image from the top for examples).
                        )),
                    iaa.Sometimes(0.5, iaa.GaussianBlur(
                        sigma=(0,
                               3.0)))  # Blur images with a sigma of 0 to 3.0.
                ]),
            iaa.Scale(size={
                'height': image_height,
                'width': image_width
            })  # Resize.
        ])

    for idx in range(images.shape[0]):
        images_pp[idx] = (images_pp[idx] - np.min(images_pp[idx])) / (
            np.max(images_pp[idx]) - np.min(images_pp[idx])) * 255
    images_pp = images_pp.astype(np.uint8)

    # Test 1 (good).
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug1 = augmenter_det.augment_images(images)
    images_aug1 = augmenter_det.augment_images(images_pp)
    labels_aug1 = augmenter_det.augment_images(labels)
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug2 = augmenter_det.augment_images(images)
    images_aug2 = augmenter_det.augment_images(images_pp)
    labels_aug2 = augmenter_det.augment_images(labels)

    #export_images(images, labels, './augmented1/img', '')
    export_images(images_pp, labels, './augmented1/img', '')
    export_images(images_aug1, labels_aug1, './augmented1/img', '_aug1')
    export_images(images_aug2, labels_aug2, './augmented1/img', '_aug2')

    # Test 2 (bad).
    augmenter_det = augmenter.to_deterministic(
    )  # Call this for each batch again, NOT only once at the start.
    #images_aug1 = augmenter_det.augment_images(images)
    images_aug1 = augmenter_det.augment_images(images_pp)
    labels_aug1 = augmenter_det.augment_images(labels)
    #images_aug2 = augmenter_det.augment_images(images)
    images_aug2 = augmenter_det.augment_images(images_pp)
    labels_aug2 = augmenter_det.augment_images(labels)

    #export_images(images, labels, './augmented2/img', '')
    export_images(images_pp, labels, './augmented2/img', '')
    export_images(images_aug1, labels_aug1, './augmented2/img', '_aug1')
    export_images(images_aug2, labels_aug2, './augmented2/img', '_aug2')

    print('*********************************', images_pp.dtype)
Ejemplo n.º 30
0
                rotate=(-20, 20),  # rotate by -45 to +45 degrees
                shear=(-16, 16),  # shear by -16 to +16 degrees
                order=[
                    0, 1
                ],  # use nearest neighbour or bilinear interpolation (fast)
                mode=
                "edge"  # use any of scikit-image"s warping modes (see 2nd image from the top for examples)
            )),
        sometimes(iaa.PiecewiseAffine(
            scale=(0.01, 0.05))),  # sometimes move parts of the image around
        iaa.PerspectiveTransform(scale=(0.01, 0.1)),
        sometimes(
            iaa.OneOf([
                iaa.SigmoidContrast(gain=5),
                iaa.Multiply(mul=1.0),
                iaa.MultiplyHueAndSaturation(mul=1.25)
            ])),
    ],
    random_order=True)


def apply_batch(images, file_counter_start, aug_amount):
    i = file_counter_start
    batches = []
    for _ in range(aug_amount):
        batches.append(iaa.UnnormalizedBatch(images=images))

    for batch in seq.augment_batches(batches=batches, background=True):
        for images in batch.images_aug:
            cv2.imwrite(f"augmented/{category}/{category}_{i}.png", images)
            i += 1