Exemple #1
0
def _lane_argue(*, image, lane_src):
    lines_tuple = [[(float(pt['x']), float(pt['y'])) for pt in line_spec] for line_spec in lane_src['Lines']]
    lss = [ia_LineString(line_tuple_spec) for line_tuple_spec in lines_tuple]

    lsoi = LineStringsOnImage(lss, shape=image.shape)
    color_shift = iaa.OneOf([
        iaa.GaussianBlur(sigma=(0.5, 1.5)),
        iaa.LinearContrast((1.5, 1.5), per_channel=False),
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(0, iaa.Multiply((0.7, 1.3)))),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(1, iaa.Multiply((0.1, 2)))),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(2, iaa.Multiply((0.5, 1.5)))),
    ])
    posion_shift = iaa.SomeOf(4, [
        iaa.Fliplr(),
        iaa.Crop(percent=([0, 0.2], [0, 0.15], [0, 0], [0, 0.15]), keep_size=True),
        iaa.TranslateX(px=(-16, 16)),
        iaa.ShearX(shear=(-15, 15)),
        iaa.Rotate(rotate=(-15, 15))
    ])
    aug = iaa.Sequential([
        iaa.Sometimes(p=0.6, then_list=color_shift),
        iaa.Sometimes(p=0.6, then_list=posion_shift)
    ], random_order=True)
    batch = ia.Batch(images=[image], line_strings=[lsoi])
    batch_aug = list(aug.augment_batches([batch]))[0]  # augment_batches returns a generator
    image_aug = batch_aug.images_aug[0]
    lsoi_aug = batch_aug.line_strings_aug[0]
    lane_aug = [[dict(x=kpt.x, y=kpt.y) for kpt in shapely_line.to_keypoints()] for shapely_line in lsoi_aug]
    return image_aug, dict(Lines=lane_aug)
Exemple #2
0
    def transform(self, in_data):
        augmenter = iaa.Sequential([
            iaa.LinearContrast(alpha=(0.8, 1.2)),
            iaa.WithColorspace(
                to_colorspace="HSV",
                from_colorspace="RGB",
                children=iaa.Sequential([
                    # SV
                    iaa.WithChannels(
                        (1, 2),
                        iaa.Multiply(mul=(0.8, 1.2), per_channel=True),
                    ),
                    # H
                    iaa.WithChannels(
                        (0, ),
                        iaa.Multiply(mul=(0.95, 1.05), per_channel=True),
                    ),
                ]),
            ),
            iaa.GaussianBlur(sigma=(0, 1.0)),
            iaa.KeepSizeByResize(children=iaa.Resize((0.25, 1.0))),
        ])

        augmenter = augmenter.to_deterministic()
        for index in self._indices:
            in_data[index] = augmenter.augment_image(in_data[index])
Exemple #3
0
def create_augmentation_configuration(some_of=None,
                                      flip_lr=True,
                                      flip_ud=True,
                                      gblur=None,
                                      avgblur=None,
                                      gnoise=None,
                                      scale=None,
                                      rotate=None,
                                      bright=None,
                                      colour_shift=None,
                                      cval=0):
    """
        More info at https://imgaug.readthedocs.io/en/latest/source/augmenters.html
        Args:
            some_of: (int) The maximum amount of transforms to apply. Randoms selects 0->some_of transforms to apply.
            flip_lr: (bool) Randomly flip the images from left to right
            flip_ud: (bool) randomly flip the images from left to right
            gblur: (tuple) Apply gaussian blur. This is equal to sigma. gblur=(0.0,3.0)
            avgblur: (tuple) Apply average blur. This is equal to kernel size e.g. avgblur=(2,11)
            gnoise: (tuple) Apply guassian noise. This parameter is equal to scale range. e.g. gnoise=(0,0.05*255)
            scale: (tuple) Apply scale transformations. This parameter is equal to scale. e.g. scale=(0.5,1.5) to scale between 50% and 150% of image size
            rotate: (tuple) Apply rotation transformations. This parameter is equal to rotation degrees. e.g. scale=(-45,45)
            bright: (tuple) Brighten the image by multiplying. This parameter is equal to the range of brightnesses, e.g. bright=(0.9,1.1)
            colour_shift: (tuple) Apply a color slight colour shift to some of the channels in the image. This parameter is equal to the multiplying factor on each channel. E.g. colour_shift=(0.9,1.1)
            cval : (int) defines a constant value with which to fill in newly created pixels.
        """
    aug_list = []
    if flip_lr:
        aug_list.append(iaa.Fliplr(0.5))
    if flip_ud:
        aug_list.append(iaa.Flipud(0.5))

    sometimes_list = []
    if gblur:
        sometimes_list.append(iaa.GaussianBlur(sigma=gblur))
    if avgblur and not gblur:
        # Only use avgblur if gblur is not being used
        sometimes_list.append(iaa.AverageBlur(k=avgblur))
    if gnoise:
        sometimes_list.append(iaa.AdditiveGaussianNoise(scale=gnoise))
    if scale:
        sometimes_list.append(
            iaa.Affine(scale=scale, mode='constant', cval=cval, order=0))
    if rotate:
        sometimes_list.append(
            iaa.Affine(rotate=rotate, mode='constant', cval=cval, order=0))
    if bright:
        sometimes_list.append(iaa.Multiply(bright))
    if colour_shift:
        colours = iaa.SomeOf((0, None), [
            iaa.WithChannels(0, iaa.Multiply(mul=colour_shift)),
            iaa.WithChannels(1, iaa.Multiply(mul=colour_shift)),
            iaa.WithChannels(2, iaa.Multiply(mul=colour_shift))
        ])
        sometimes_list.append(colours)

    aug_list.append(iaa.SomeOf((0, some_of), sometimes_list))

    return aug_list
Exemple #4
0
    def _augment(img, depth, label):
        st = lambda x: iaa.Sometimes(0.5, x)  # NOQA
        augmentations = [
            st(iaa.WithChannels([0, 1], iaa.Multiply([1, 1.5]))),
            st(
                iaa.InColorspace(
                    'HSV',
                    children=iaa.WithChannels([1, 2], iaa.Multiply([0.5, 2])),
                )),
            (iaa.GaussianBlur(sigma=[0, 1])),
            iaa.Sometimes(0.9, iaa.Dropout(p=(0, 0.4), name='dropout')),
            # iaa.CoarseDropout(p=(0, 0.1), size_percent=0.5, name='dropout'),
            iaa.Sometimes(
                0.9,
                iaa.Affine(
                    order=1,
                    cval=0,
                    scale=1,
                    translate_px=(-96, 96),
                    rotate=(-180, 180),
                    mode='constant',
                )),
        ]
        aug = iaa.Sequential(augmentations, random_order=True)

        def activator_imgs(images, augmenter, parents, default):
            if isinstance(augmenter, iaa.Affine):
                augmenter.order = Deterministic(1)
                augmenter.cval = Deterministic(0)
            return True

        def activator_depths(images, augmenter, parents, default):
            white_lists = (iaa.Affine, iaa.Sequential, iaa.Sometimes)
            if not isinstance(augmenter, white_lists):
                return False
            if isinstance(augmenter, iaa.Affine):
                augmenter.order = Deterministic(1)
                augmenter.cval = Deterministic(0)
            return True

        def activator_lbls(images, augmenter, parents, default):
            white_lists = (iaa.Affine, iaa.Sequential, iaa.Sometimes)
            if not isinstance(augmenter, white_lists):
                return False
            if isinstance(augmenter, iaa.Affine):
                augmenter.order = Deterministic(0)
                augmenter.cval = Deterministic(-1)
            return True

        aug = aug.to_deterministic()
        img = aug.augment_image(img,
                                hooks=ia.HooksImages(activator=activator_imgs))
        depth = aug.augment_image(
            depth, hooks=ia.HooksImages(activator=activator_depths))
        label = aug.augment_image(
            label, hooks=ia.HooksImages(activator=activator_lbls))

        return img, depth, label
 def augment():
     with open(CONFIG, "r") as file:
         config = json.loads(file.read())
     if config[ImageAugmentation.AUGMENT_DATA]:
         matrix = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])
         return iaa.Sometimes(ImageAugmentation.AUG_PERCENTAGE, [
             iaa.GaussianBlur(sigma=2.0),
             iaa.Sequential([iaa.Affine(rotate=45),
                             iaa.Sharpen(alpha=1.0)]),
             iaa.WithColorspace(to_colorspace="HSV",
                                from_colorspace="RGB",
                                children=iaa.WithChannels(
                                    0, iaa.Add((10, 50)))),
             iaa.AdditiveGaussianNoise(scale=0.2 * 255),
             iaa.Add(50, per_channel=True),
             iaa.Sharpen(alpha=0.5),
             iaa.WithChannels(0, iaa.Add((10, 100))),
             iaa.WithChannels(0, iaa.Affine(rotate=(0, 45))),
             iaa.Noop(),
             iaa.Superpixels(p_replace=0.5, n_segments=64),
             iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128)),
             iaa.ChangeColorspace(from_colorspace="RGB",
                                  to_colorspace="HSV"),
             iaa.WithChannels(0, iaa.Add((50, 100))),
             iaa.ChangeColorspace(from_colorspace="HSV",
                                  to_colorspace="RGB"),
             iaa.Grayscale(alpha=(0.0, 1.0)),
             iaa.GaussianBlur(sigma=(0.0, 3.0)),
             iaa.AverageBlur(k=(2, 11)),
             iaa.AverageBlur(k=((5, 11), (1, 3))),
             iaa.MedianBlur(k=(3, 11)),
             iaa.Convolve(matrix=matrix),
             iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
             iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
             iaa.EdgeDetect(alpha=(0.0, 1.0)),
             iaa.DirectedEdgeDetect(alpha=(0.0, 1.0), direction=(0.0, 1.0)),
             iaa.Add((-40, 40)),
             iaa.Add((-40, 40), per_channel=0.5),
             iaa.AddElementwise((-40, 40)),
             iaa.AddElementwise((-40, 40), per_channel=0.5),
             iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
             iaa.AdditiveGaussianNoise(scale=0.05 * 255),
             iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5),
             iaa.Multiply((0.5, 1.5), per_channel=0.5),
             iaa.Dropout(p=(0, 0.2)),
             iaa.Dropout(p=(0, 0.2), per_channel=0.5),
             iaa.CoarseDropout(0.02, size_percent=0.5),
             iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
             iaa.CoarseDropout(0.02, size_percent=0.15, per_channel=0.5),
             iaa.Invert(0.25, per_channel=0.5),
             iaa.Invert(0.5),
             iaa.ContrastNormalization((0.5, 1.5)),
             iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
             iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25)
         ])
     else:
         return None
Exemple #6
0
def iaa_hsv_aug(hue=0, saturation=1, exposure=1):
    h_l, h_t = 179 * -hue, 179 * hue
    s_l, s_t = 1 / saturation if saturation else 0, saturation
    v_l, v_t = 1 / exposure if exposure else 0, exposure
    
    return iaa.Sequential([iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                           iaa.WithChannels([0], iaa.Add((h_l, h_t))),
                           iaa.WithChannels([1], iaa.Multiply((s_l, s_t))),
                           iaa.WithChannels([2], iaa.Multiply((v_l, v_t))),
                           iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")])
    def get_ill_seq(self):
        light_change = 50
        seq = iaa.Sequential([
            # 全局调整,含有颜色空间调整
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.WithColorspace(
                        to_colorspace="HSV",
                        from_colorspace="RGB",
                        children=iaa.OneOf([
                            iaa.WithChannels(0, iaa.Add((-5, 5))),
                            iaa.WithChannels(1, iaa.Add((-20, 20))),
                            iaa.WithChannels(
                                2, iaa.Add((-light_change, light_change))),
                        ])),
                    iaa.Grayscale((0.2, 0.6)),
                    iaa.ChannelShuffle(1),
                    iaa.Add((-light_change, light_change)),
                    iaa.Multiply((0.5, 1.5)),
                ])),

            # # dropout阴影模仿,暂时不使用,转而使用了自定义的阴影模仿
            # iaa.Sometimes(0.5, iaa.OneOf([
            #     iaa.Alpha((0.2, 0.7), iaa.CoarseDropout(p=0.2, size_percent=(0.02, 0.005)))
            # ])),

            # 椒盐噪声
            iaa.Sometimes(
                0.5,
                iaa.OneOf(
                    [iaa.Alpha((0.2, 0.6), iaa.SaltAndPepper((0.01, 0.03)))])),

            # 图像反转
            iaa.Sometimes(0.5, iaa.OneOf([
                iaa.Invert(1),
            ])),

            # 对比度调整
            iaa.Sometimes(0.5,
                          iaa.OneOf([
                              iaa.ContrastNormalization((0.5, 1.5)),
                          ])),
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(0, (3, 6)),
                    iaa.AdditivePoissonNoise((3, 6)),
                    iaa.JpegCompression((30, 60)),
                    iaa.GaussianBlur(sigma=1),
                    iaa.AverageBlur((1, 3)),
                    iaa.MedianBlur((1, 3)),
                ])),
        ])
        return seq
Exemple #8
0
 def __init__(self):
     self.contrast = iaa.ContrastNormalization((0.5, 1.5), per_channel=0.3)
     self.mulaug = iaa.Multiply((0.8, 1.2), per_channel=0.3)
     self.grayaug = iaa.Grayscale(alpha=(0.0, 1.0))
     self.sharpen = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))
     self.coloraug = iaa.Sequential([
         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")
     ])
Exemple #9
0
def chapter_augmenters_withchannels():
    aug = iaa.WithChannels(0, iaa.Add((10, 100)))
    run_and_save_augseq("meta/withchannels.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)

    aug = iaa.WithChannels(0, iaa.Affine(rotate=(0, 45)))
    run_and_save_augseq("meta/withchannels_affine.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
Exemple #10
0
def _main_(args) :
    
    number_of_data_augmentation = int(args.number_of_dataset_augmentation)
    last_gen = int(args.number_of_the_last_dataset_augmentation)

    aug = iaa.SomeOf(3, [    
        #FIRST GEN OF DATA AUGMENTATION
        iaa.Affine(scale=(0.8, 1.2)),
        iaa.Affine(rotate=(-30, 30)),
        iaa.Affine(translate_percent={"x":(-0.2, 0.2),"y":(-0.2, 0.2)}),
        iaa.Fliplr(1),

        #SECOND GEN OF DATA AUGMENTATION
        iaa.SaltAndPepper(0.1, per_channel=True),
        iaa.Add((-40, 40), per_channel=0.5),
        iaa.AdditiveGaussianNoise(scale=(0, 0.2*255)),
        iaa.Multiply((0.5, 1.5), per_channel=0.5),
        iaa.AverageBlur(k=((5, 11), (1, 3))),
        iaa.WithColorspace(to_colorspace="HSV",from_colorspace="RGB",children=iaa.WithChannels(0,iaa.Add((0, 50)))),
        iaa.AddToHueAndSaturation((-50, 50), per_channel=True),

        #iaa.RandAugment(n=(0, 3)), # ==> DON'T WORK WITH BOUNDING BOX 
        #iaa.BlendAlphaCheckerboard(nb_rows=2, nb_cols=(1, 4),foreground=iaa.AddToHue((-100, 100))),
        #iaa.BlendAlphaHorizontalLinearGradient(iaa.TotalDropout(1.0),min_value=0.2, max_value=0.8),
        #iaa.BlendAlphaSimplexNoise(iaa.EdgeDetect(1.0)),
        iaa.Solarize(0.5, threshold=(32, 128)), 
        iaa.WithHueAndSaturation(iaa.WithChannels(0, iaa.Add((0, 50))))
    ])

    labels_df = xml_to_csv('vanilla_dataset_annot/')
    labels_df.to_csv(('labels.csv'), index=None)

    for i in range(number_of_data_augmentation):

        prefix = "aug{}_".format(i+last_gen+1)
        augmented_images_df = image_aug(labels_df, 'vanilla_dataset_img/', 'aug_images/', prefix, aug)
        csv_to_xml(augmented_images_df, 'aug_images/')

        # Concat resized_images_df and augmented_images_df together and save in a new all_labels.csv file
        if(i==0):
            all_labels_df = pd.concat([labels_df, augmented_images_df])
        else:
            all_labels_df = pd.concat([all_labels_df, augmented_images_df])

    all_labels_df.to_csv('all_labels.csv', index=False)
    
    del_unique_file()

    # Lastly we can copy all our augmented images in the same folder as original resized images
    for file in os.listdir('aug_images/'):
        shutil.copy('aug_images/'+file, 'train_image_folder/'+file)
    for file in os.listdir("aug_annot/"):
        shutil.copy('aug_annot/'+file, 'train_annot_folder/'+file)
Exemple #11
0
    def __init__(self, sometimes_prob=0.5, someof_range=(0, 3)):
        def sometimes(aug):
            return iaa.Sometimes(sometimes_prob, aug)

        # define the sequence of augmentation strageties

        self._pipeline = sometimes(
            iaa.SomeOf(
                someof_range,
                [
                    # converts to HSV
                    # alters Hue in range -50,50°
                    # multiplies saturation
                    # converts back
                    iaa.WithHueAndSaturation([
                        iaa.WithChannels(0, iaa.Add((-50, 50))),
                        iaa.WithChannels(1, [
                            iaa.Multiply((0.5, 1.5)),
                        ]),
                    ]),
                    # Sharpen each image, overlay the result with the original
                    # image using an alpha between 0 (no sharpening) and 1
                    # (full sharpening effect).
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),

                    # Improve or worsen the contrast of images.
                    iaa.LinearContrast((0.5, 1.5), per_channel=0.5),

                    # Either drop randomly 1 to 10% of all pixels (i.e. set
                    # them to black) or drop them on an image with 2-5% percent
                    # of the original size, leading to large dropped
                    # rectangles.
                    # otherwise apply gaussian blur
                    iaa.OneOf([
                        iaa.Dropout((0.01, 0.1), per_channel=0.5),
                        iaa.CoarseDropout((0.03, 0.15),
                                          size_percent=(0.02, 0.05),
                                          per_channel=0.2),
                        # gaussian blur (sigma between 0 and 3.0),
                        iaa.GaussianBlur((0, 3.0)),
                    ]),

                    # Add a value of -10 to 10 to each pixel.
                    iaa.Add((-10, 10), per_channel=0.5),

                    # Convert each image to grayscale and then overlay the
                    # result with the original with random alpha. I.e. remove
                    # colors with varying strengths.
                    sometimes(iaa.Grayscale(alpha=(0.0, 1.0))),
                ],
                # do all of the above augmentations in random order
                random_order=True))
def main():
    image = ia.quokka_square(size=(128, 128))
    images = []

    for i in range(15):
        aug = iaa.WithHueAndSaturation(iaa.WithChannels(0, iaa.Add(i * 20)))
        images.append(aug.augment_image(image))

    for i in range(15):
        aug = iaa.WithHueAndSaturation(iaa.WithChannels(1, iaa.Add(i * 20)))
        images.append(aug.augment_image(image))

    ia.imshow(ia.draw_grid(images, rows=2))
def get_augmentation(noise=False,
                     crop=False,
                     flip=False,
                     contrast=False,
                     add=False,
                     saturation=False,
                     value=False):
    augmenters = []
    if noise:
        std = 0.075
        augmenters.append(iaa.AdditiveGaussianNoise(scale=(0, std)))
    if crop:
        augmenters.append(iaa.CropAndPad(
            percent=(0, 0.2),
            pad_mode='edge',
        ))
    if flip:
        augmenters.append(iaa.Fliplr(0.5))
    if contrast:
        augmenters.append(iaa.contrast.LinearContrast((0.7, 1.3)))
    if add:
        augmenters.append(iaa.Add((-0.075, 0.075)))
    if saturation:
        augmenters.append(
            iaa.Sequential([
                UInt8FromFloat32(),
                iaa.ChangeColorspace(from_colorspace='RGB',
                                     to_colorspace='HSV'),
                iaa.WithChannels(1, iaa.Add((-0.15, 0.15))),  # HSV
                iaa.ChangeColorspace(from_colorspace='HSV',
                                     to_colorspace='RGB'),
                Float32FromUInt8(),
            ]))
    if value:
        augmenters.append(
            iaa.Sequential([
                UInt8FromFloat32(),
                iaa.ChangeColorspace(from_colorspace='RGB',
                                     to_colorspace='HSV'),
                iaa.WithChannels(2, iaa.Add((-0.15, 0.15))),  # HSV
                iaa.ChangeColorspace(from_colorspace='HSV',
                                     to_colorspace='RGB'),
                Float32FromUInt8(),
            ]))

    return iaa.Sequential([
        iaa.SomeOf(max(1,
                       len(augmenters) // 2), augmenters),
        Clip(),
    ])
Exemple #14
0
def f1():
    '''
        Increase each pixel’s R-value (redness) by 3 to 50:
    '''
    aug = iaa.WithChannels(random.sample([0, 1, 2], 1)[0], iaa.Add((3, 50)))
    desc = 'AddRandColor'
    return aug, desc
def augmentation(X, y, masks, itterations):
    ys = np.concatenate(
        (y[:, :, np.newaxis], y[:, :, np.newaxis], y[:, :, np.newaxis]),
        axis=2)
    masks = np.concatenate(
        (masks[:, :, np.newaxis], masks[:, :, np.newaxis], masks[:, :,
                                                                 np.newaxis]),
        axis=2)
    B = np.concatenate((ys, X, masks), axis=2).astype(np.uint8)
    A = np.zeros((itterations, np.shape(B)[0], np.shape(B)[1], np.shape(B)[2]))

    for num in range(itterations):
        aug1 = iaa.Affine(scale={"x": (1, 1.5), "y": (1, 1.5)})
        aug2 = iaa.Fliplr(1)
        aug3 = iaa.Flipud(1)
        aug4 = iaa.ShearX((-20, 20), mode='reflect')
        aug5 = iaa.ShearY((-20, 20), mode='reflect')
        aug6 = iaa.Rotate((-45, 45), mode='reflect')
        aug7 = iaa.WithChannels((3, 4, 5), iaa.Multiply((0.5, 1.5)))
        aug_list = [aug1, aug2, aug3, aug4, aug5, aug6, aug7]
        ID = np.random.randint(0, len(aug_list))
        A[num, :, :, :] = aug_list[ID](image=B)
    aug_y = A[:, :, :, 0]
    aug_X = A[:, :, :, 3:6]
    aug_masks = A[:, :, :, 8]

    return aug_X, aug_y, aug_masks
Exemple #16
0
def iaa_hsv_aug(hue=0, saturation=1, exposure=1):
    dhue = rand_uniform(-hue, hue) * 179
    dsat = rand_scale(saturation)
    dexp = rand_scale(exposure)

    # h_l, h_t = 179 * -hue, 179 * hue
    # s_l, s_t = 1 / saturation if saturation else 0, saturation
    # v_l, v_t = 1 / exposure if exposure else 0, exposure

    return iaa.Sequential([
        iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
        iaa.WithChannels([0], iaa.Add((dhue, dhue))),
        iaa.WithChannels([1], iaa.Multiply((dsat, dsat))),
        iaa.WithChannels([2], iaa.Multiply((dexp, dexp))),
        iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
    ])
def main():
    image = data.astronaut()
    print("image shape:", image.shape)

    aug = iaa.WithColorspace(from_colorspace="RGB",
                             to_colorspace="HSV",
                             children=iaa.WithChannels(0, iaa.Add(50)))

    aug_no_colorspace = iaa.WithChannels(0, iaa.Add(50))

    img_show = np.hstack([
        image,
        aug.augment_image(image),
        aug_no_colorspace.augment_image(image)
    ])
    misc.imshow(img_show)
Exemple #18
0
def main():
    image = data.astronaut()
    print("image shape:", image.shape)
    print("Press any key or wait %d ms to proceed to the next image." %
          (TIME_PER_STEP, ))

    children_all = [("hflip", iaa.Fliplr(1)), ("add", iaa.Add(50)),
                    ("dropout", iaa.Dropout(0.2)),
                    ("affine", iaa.Affine(rotate=35))]

    channels_all = [None, 0, [], [0], [0, 1], [1, 2], [0, 1, 2]]

    cv2.namedWindow("aug", cv2.WINDOW_NORMAL)
    cv2.imshow("aug", image[..., ::-1])
    cv2.waitKey(TIME_PER_STEP)

    for children_title, children in children_all:
        for channels in channels_all:
            aug = iaa.WithChannels(channels=channels, children=children)
            img_aug = aug.augment_image(image)
            print("dtype", img_aug.dtype, "averages",
                  np.average(img_aug, axis=tuple(range(0, img_aug.ndim - 1))))
            #print("dtype", img_aug.dtype, "averages", img_aug.mean(axis=range(1, img_aug.ndim)))

            title = "children=%s | channels=%s" % (children_title, channels)
            img_aug = ia.draw_text(img_aug, x=5, y=5, text=title)

            cv2.imshow("aug", img_aug[..., ::-1])  # here with rgb2bgr
            cv2.waitKey(TIME_PER_STEP)
Exemple #19
0
def create_positive_set(pictures, coords, label=1):
    """
    Create positive train set for one face from a list of three pictures of this face.
    Data Augmentation on these three pictures to generate 10 pictures.
    Encoding of the ten pictures
    :param pictures: list of three full pictures
    :param coords: list of the coordinates of the face in the first frame
    :return: pandas dataframe of the faces for one person
    """
    # original coordinate
    x1, y1, x2, y2 = coords
    # Load the three same faces
    images = [pictures[j][y1:y2, x1:x2, :] for j in range(3)]
    # triple each picture
    images = [item for item in images for i in range(5)]
    # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
    st = lambda aug: iaa.Sometimes(0.5, aug)
    # add a random value from the range (-30, 30) to the first three channels and gaussian noise
    aug = iaa.Sequential([
        iaa.WithChannels(channels=[0, 1, 2], children=iaa.Add((-30, 30))),
        st(
            iaa.AdditiveGaussianNoise(loc=0,
                                      scale=(0.0, 0.05 * 255),
                                      per_channel=0.5))
    ])

    images_aug = aug.augment_images(images)
    # add the original frame
    images_aug.append(pictures[0][y1:y2, x1:x2, :])

    # encode each of 10 faces (of the same person)
    coords_detect = [coords for k in range(len(images_aug))]
    return pd.DataFrame(encoding_faces(images_aug, label, coords_detect))
Exemple #20
0
    def __init__(self,
                 data_dir,
                 back_dir,
                 batch_size=50,
                 gan=True,
                 imsize=128,
                 res_x=640,
                 res_y=480,
                 **kwargs):
        '''
        data_dir: Folder that contains cropped image+xyz
        back_dir: Folder that contains random background images
            batch_size: batch size for training
        gan: if False, gt for GAN is not yielded
        '''
        self.data_dir = data_dir
        self.back_dir = back_dir
        self.imsize = imsize
        self.batch_size = batch_size
        self.gan = gan
        self.backfiles = os.listdir(back_dir)
        data_list = os.listdir(data_dir)
        self.datafiles = []
        self.res_x = res_x
        self.res_y = res_y

        for file in data_list:
            if (file.endswith(".npy")):
                self.datafiles.append(file)

        self.n_data = len(self.datafiles)
        self.n_background = len(self.backfiles)
        print("Total training views:", self.n_data)

        self.seq_syn = iaa.Sequential([
            iaa.WithChannels(0, iaa.Add((-15, 15))),
            iaa.WithChannels(1, iaa.Add((-15, 15))),
            iaa.WithChannels(2, iaa.Add((-15, 15))),
            iaa.ContrastNormalization((0.8, 1.3)),
            iaa.Multiply((0.8, 1.2), per_channel=0.5),
            iaa.GaussianBlur(sigma=(0.0, 0.5)),
            iaa.Sometimes(
                0.1, iaa.AdditiveGaussianNoise(scale=10, per_channel=True)),
            iaa.Sometimes(
                0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3)),
        ],
                                      random_order=True)
Exemple #21
0
    def __init__(self, list_path, img_size=416, augment=True):

        with open(list_path, 'r') as file:
            self.img_files = file.readlines()

        self.label_files = [
            path.replace('Images',
                         'Labels').replace('.png',
                                           '.txt').replace('.jpg', '.txt')
            for path in self.img_files
        ]
        self.img_shape = (img_size, img_size)
        self.max_objects = 50

        # augment = True
        # multiscale = False
        # augmentHSV = True
        # lrFlip = True
        # udFlip = True

        if augment:
            self.augments = iaa.Sequential([
                iaa.WithColorspace(to_colorspace="HSV",
                                   from_colorspace="RGB",
                                   children=[
                                       iaa.WithChannels(1, iaa.Add((-50, 50))),
                                       iaa.WithChannels(2, iaa.Add((-50, 50)))
                                   ]),
                iaa.Fliplr(0.5),
                iaa.Flipud(0.5),
                iaa.Affine(scale={
                    "x": (0.90, 1.10),
                    "y": (0.90, 1.10)
                },
                           translate_percent={
                               "x": (-0.1, 0.1),
                               "y": (-0.1, 0.1)
                           },
                           rotate=(-10, 10),
                           shear=(-16, 16),
                           order=[0, 1],
                           mode="constant",
                           cval=128),
            ])
        else:
            self.augments = iaa.Noop()
Exemple #22
0
def chapter_augmenters_withcolorspace():
    aug = iaa.WithColorspace(to_colorspace="HSV",
                             from_colorspace="RGB",
                             children=iaa.WithChannels(0, iaa.Add((10, 50))))
    run_and_save_augseq("withcolorspace.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
def generate_images(row):
    path, width, height, xmin, ymin, xmax, ymax = row

    image = cv2.imread(path)

    bbs = ia.BoundingBoxesOnImage(
        [ia.BoundingBox(x1=xmin, y1=ymin, x2=xmax, y2=ymax)],
        shape=image.shape)

    seq = iaa.Sequential([
        iaa.Affine(scale=(0.4, 1.6)),
        iaa.Crop(percent=(0, 0.2)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.AddToHueAndSaturation((-30, 30)),
        iaa.Sometimes(0.5, iaa.Affine(rotate=(-45, 45)),
                      iaa.Affine(shear=(-16, 16))),
        iaa.Sometimes(
            0.2,
            iaa.WithColorspace(to_colorspace="HSV",
                               from_colorspace="RGB",
                               children=iaa.WithChannels(0, iaa.Add(
                                   (10, 50)))),
        ),
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        iaa.GaussianBlur(sigma=(0, 1.0))
    ])

    new_rows = []
    for i in range(0, AUGMENTATION_PER_IMAGE):
        seq_det = seq.to_deterministic()
        image_aug = seq_det.augment_images([image])[0]
        bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
        if not bbs_aug.bounding_boxes[0].is_fully_within_image(image.shape):
            continue
        # Another possibility is:
        # bbs_aug = bbs_aug.remove_out_of_image().cut_out_of_image()
        # if not bbs_aug.bounding_boxes:
        #    continue
        after = bbs_aug.bounding_boxes[0]

        if AUGMENTATION_DEBUG:
            image_aug = bbs_aug.draw_on_image(image_aug,
                                              thickness=2,
                                              color=[0, 0, 255])

        name, ftype = os.path.splitext(os.path.basename(path))
        new_filename = "{}_aug_{}{}".format(name, i, ftype)
        new_path = os.path.join(TRAIN_FOLDER, new_filename)
        cv2.imwrite(new_path, cv2.resize(image_aug, (IMAGE_SIZE, IMAGE_SIZE)))

        new_rows.append([
            new_path, *scale_coordinates(width, height, after.x1, after.y1,
                                         after.x2, after.y2)
        ])

    return new_rows
def change_color_space(image):
    x1 = int(np.random.randint(20, 50, size=1, dtype='int8'))
    x2 = int(np.random.randint(60, 100, size=1, dtype='int8'))
    image_aug = iaa.Sequential([
        iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
        iaa.WithChannels(0, iaa.Add((x1, x2))),
        iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
    ])(image=image)
    return image_aug
    def get_simple_ill_seq(self):
        light_change = 20
        seq = iaa.Sequential([
            # 全局调整,含有颜色空间调整
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.WithColorspace(
                        to_colorspace="HSV",
                        from_colorspace="RGB",
                        children=iaa.OneOf([
                            iaa.WithChannels(0, iaa.Add((-5, 5))),
                            iaa.WithChannels(1, iaa.Add((-20, 20))),
                            iaa.WithChannels(
                                2, iaa.Add((-light_change, light_change))),
                        ])),
                    iaa.Grayscale((0.2, 0.6)),
                    iaa.Add((-light_change, light_change)),
                    iaa.Multiply((0.8, 1.2)),
                ])),

            # 椒盐噪声
            iaa.Sometimes(
                0.5,
                iaa.OneOf(
                    [iaa.Alpha((0.2, 0.6), iaa.SaltAndPepper((0.01, 0.03)))])),

            # 对比度调整
            iaa.Sometimes(0.5,
                          iaa.OneOf([
                              iaa.ContrastNormalization((0.8, 1.2)),
                          ])),
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(0, 1),
                    iaa.AdditivePoissonNoise(1),
                    iaa.JpegCompression((30, 60)),
                    iaa.GaussianBlur(sigma=1),
                    iaa.AverageBlur(1),
                    iaa.MedianBlur(1),
                ])),
        ])
        return seq
def coloring(digit):
    image_name = '00' + str(digit) + '000' + ".jpg"
    # Load and display
    image = imageio.imread(image_name)
    # image = cv.resize(image, (input_width,input_height), interpolation=cv.INTER_CUBIC)
    #    print("Original:", image.shape)
    #    ia.imshow(image)

    images = np.array([image for _ in range(sample)], dtype=np.uint8)
    seq_color = iaa.Sequential([
        iaa.WithChannels(0, iaa.Add(red_range)),
        iaa.WithChannels(1, iaa.Add(green_range)),
        iaa.WithChannels(2, iaa.Add(blue_range))
    ],
                               random_order=True)

    images_color_aug = seq_color(images=images)
    #    ia.imshow(ia.draw_grid(images_color_aug, cols=sample/8, rows=8))
    return images_color_aug
Exemple #27
0
def chapter_augmenters_withhueandsaturation():
    fn_start = "color/withhueandsaturation"

    aug = iaa.WithHueAndSaturation(iaa.WithChannels(0, iaa.Add((0, 50))))
    run_and_save_augseq(fn_start + "_add_to_hue.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)

    aug = iaa.WithHueAndSaturation([
        iaa.WithChannels(0, iaa.Add((-30, 10))),
        iaa.WithChannels(
            1, [iaa.Multiply((0.5, 1.5)),
                iaa.LinearContrast((0.75, 1.25))])
    ])
    run_and_save_augseq(fn_start + "_modify_both.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
Exemple #28
0
def chapter_augmenters_changecolorspace():
    aug = iaa.Sequential([
        iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
        iaa.WithChannels(0, iaa.Add((50, 100))),
        iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
    ])
    run_and_save_augseq("changecolorspace.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
    def get_example(self, i):
        files_dir = self._files_dirs[i]

        image_file = osp.join(files_dir, 'image.png')
        image = skimage.io.imread(image_file)
        assert image.dtype == np.uint8
        assert image.ndim == 3

        label_file = osp.join(files_dir, 'label.png')
        with open(label_file, 'r') as f:
            label = np.asarray(PIL.Image.open(f)).astype(np.int32)
        assert label.dtype == np.int32
        assert label.ndim == 2

        # Data augmentation
        if self.aug:
            # 1. Color augmentation
            obj_datum = dict(img=image)
            random_state = np.random.RandomState()

            def st(x):
                return iaa.Sometimes(0.3, x)

            augs = [
                st(iaa.Add([-50, 50], per_channel=True)),
                st(
                    iaa.InColorspace('HSV',
                                     children=iaa.WithChannels(
                                         [1, 2], iaa.Multiply([0.5, 2])))),
                st(iaa.GaussianBlur(sigma=[0.0, 1.0])),
                st(
                    iaa.AdditiveGaussianNoise(scale=(0.0, 0.1 * 255),
                                              per_channel=True)),
            ]
            obj_datum = next(
                mvtk.aug.augment_object_data([obj_datum],
                                             random_state=random_state,
                                             augmentations=augs))
            image = obj_datum['img']

            # 2. Geometric augmentation
            np.random.seed()
            if np.random.uniform() < 0.5:
                image = np.fliplr(image)
                label = np.fliplr(label)
            if np.random.uniform() < 0.5:
                image = np.flipud(image)
                label = np.flipud(label)
            if np.random.uniform() < 0.5:
                angle = (np.random.uniform() * 180) - 90
                image = self.rotate_image(image, angle, cv2.INTER_LINEAR)
                label = self.rotate_image(label, angle, cv2.INTER_NEAREST)
        return image, label
 def __call__(self, image, target):
     if random.random() < self.prob:
         aug_seq = iaa.OneOf([
             iaa.WithColorspace(to_colorspace="HSV",
                                from_colorspace="RGB",
                                children=iaa.WithChannels(
                                    0, iaa.Add((10, 50)))),
             iaa.Grayscale(alpha=(0.0, 1.0))
         ])
         image = aug_seq.augment_image(np.array(image))
         image = Image.fromarray(np.uint8(image))
     return image, target