Exemplo n.º 1
0
def train(model, dataset_dir, annotations_file, epochs):
    from imgaug import augmenters as iaa
    """Train the mask rcnn model.

    Inputs:
        model: Model to train.
        dataset_dir: Root directory of dataset.
        epochs: Epochs to train for. If given two values, the network
                heads are first trained for epochs[0] before training
                the full model to epochs[1].
    """
    # Training dataset
    dataset_train = FoodDataset()
    dataset_train.load_food(dataset_dir, 'train', annotations_file)
    dataset_train.prepare()
    print('[*] Training dataset:')
    print(' ', 'Image Count: {}'.format(len(dataset_train.image_ids)))
    print(' ', 'Class Count: {}'.format(dataset_train.num_classes))
    print(' ', 'Classes:', dataset_train.class_names)

    #Validation dataset
    dataset_val = FoodDataset()
    dataset_val.load_food(dataset_dir, 'val', annotations_file)
    dataset_val.prepare()
    print('[*] Validation dataset:')
    print(' ', 'Image Count: {}'.format(len(dataset_val.image_ids)))
    print(' ', 'Class Count: {}'.format(dataset_val.num_classes))
    print(' ', 'Classes:', dataset_val.class_names)

    # Input augmentations
    augmentation = iaa.SomeOf(
        (0, None),
        [
            iaa.Fliplr(0.5),  # Left-right flip with probability 0.5
            iaa.Flipud(0.5),  # Up-down flip with probability 0.5
            iaa.Add((-40, 40)),  # Add delta value to brightness
            iaa.LinearContrast((0.8, 1.2)),  # Transform contrast
            iaa.AddToSaturation((-40, 40)),  # Add delta value to saturation
            iaa.AddToHue((-20, 20))  # Add delta value to hue
        ])

    # Train network heads first if two epoch values are given
    if len(epochs) > 1:
        print('[*] Training network heads.')
        model.train(dataset_train,
                    dataset_val,
                    learning_rate=config.LEARNING_RATE,
                    augmentation=augmentation,
                    epochs=epochs[0],
                    layers='heads')
    else:
        epochs.append(epochs[0])

    print('[*] Training network.')
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                augmentation=augmentation,
                epochs=epochs[1],
                layers='all')
def main():
    image = data.astronaut()

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

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

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

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

    image = ia.quokka_square((128, 128))
    images_aug = []
    images_aug.extend(iaa.AddToHue().augment_images([image] * 10))
    images_aug.extend(iaa.AddToSaturation().augment_images([image] * 10))
    ia.imshow(ia.draw_grid(images_aug, rows=2))
Exemplo n.º 3
0
def chapter_augmenters_addtosaturation():
    fn_start = "color/addtosaturation"

    aug = iaa.AddToSaturation((-50, 50))
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
Exemplo n.º 4
0
def get_augmentations():
    def sometimes(aug, p=0.5):
        return iaa.Sometimes(p, aug)

    return iaa.Sequential([
        # sometimes(iaa.Affine(scale=(1, 1.6), translate_percent=0.2)),
        iaa.SomeOf(
            2,
            [
                sometimes(iaa.Multiply()),
                sometimes(iaa.GammaContrast()),
                sometimes(iaa.AddToSaturation()),
                sometimes(iaa.AddToBrightness()),
                # sometimes(iaa.AddToHue()),
                sometimes(iaa.CLAHE())
            ])
    ])
Exemplo n.º 5
0
def augment_batch(images, masks):
    #initial_image = images

    images = (images.numpy() * 255).astype(np.uint8)
    images = np.swapaxes(images, 1, 3)
    images = np.swapaxes(images, 1, 2)
    masks = masks.numpy().astype(np.int32)
    masks = np.swapaxes(masks, 1, 3)
    masks = np.swapaxes(masks, 1, 2)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5),  # vertically flip 50% of all images
            # change brightness of images (by -10 to 40 of original value)
            iaa.Add((-20, 60)),
            iaa.AddToSaturation((-20, 20)),  # change saturation
            iaa.Multiply((0.8, 1.6)),
            # improve or worsen the contrast
            iaa.LinearContrast((0.8, 1.2))
        ], )
    images, masks = seq(images=images, segmentation_maps=masks)
    images = np.swapaxes(images, 1, 3)
    images = np.swapaxes(images, 2, 3)
    images = images.astype(np.float32) / 255.0
    masks = np.swapaxes(masks, 1, 3)
    masks = np.swapaxes(masks, 2, 3)
    masks = masks.astype(np.uint8)
    images, masks = torch.tensor(images), torch.tensor(masks)

    # Uncomment next lines if you want to see a few examples of augmented images.
    #output_folder_path = "augmented_images"
    #utils.create_dir_if_doesnt_exist(output_folder_path)
    #random_str = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
    #for i in range(images.shape[0]):
    #    image = images[i]
    #    image = np.swapaxes(image, 0, 2)
    #    image = np.swapaxes(image, 0, 1)
    #    init_image = initial_image[i]
    #    init_image = np.swapaxes(init_image, 0, 2)
    #    init_image = np.swapaxes(init_image, 0, 1)

    #    imageio.imwrite("{}/{}_{}.jpg".format(output_folder_path, random_str, i), np.uint8(image * 255), 'RGB')
    #    imageio.imwrite("{}/{}_{}_init.jpg".format(output_folder_path, random_str, i), np.uint8(init_image * 255), 'RGB')

    return images, masks
Exemplo n.º 6
0
def apply_augmentation(img):
    '''
    apply augmentation for TFRecords

    Parameters
    ----------
    img : numpy array N dimensional
        an MxNxD array that contains the image

    Returns
    -------
    img : numpy array
        an MxNxD array that contains the augmented image
    '''
    
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    
    
    # channel invariant augmentations
    seq = iaa.Sequential([
        iaa.Fliplr(0.5), # horizontally flip 50% of the images
        iaa.Flipud(0.5),
        sometimes(iaa.Rot90((1, 3)))
    ], random_order=True)
    
    # RGB dependent augmentations
    seq2 = sometimes(
            iaa.SomeOf((1,4),[
                iaa.AddToHue((-8,8)),
                iaa.AddToSaturation((-10,10)),
                iaa.Multiply((0.90, 1.25)),
                iaa.LinearContrast((0.90, 1.3))
                ], random_order=True)
    )
    
    img = seq(image=img)
    img2 = np.array(img[:,:,0:3] * 255, dtype=np.uint8)
    img2 = seq2(image=img2)
    img2 = np.array(img2/255, dtype=np.float32)
    img[:,:,0:3] = img2
    #print(img)
    return img
Exemplo n.º 7
0
 def test_returns_correct_class(self):
     aug = iaa.AddToSaturation((-20, 20))
     assert isinstance(aug, iaa.AddToHueAndSaturation)
     assert isinstance(aug.value_saturation, iap.DiscreteUniform)
     assert aug.value_saturation.a.value == -20
     assert aug.value_saturation.b.value == 20
    def __init__(self):
        self.seq = iaa.Sequential(
            [
                iaa.Fliplr(0.5),
                iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.1))),

                iaa.Sometimes(0.5, iaa.Affine(
                    rotate=(-20, 20),  # 旋转±20度
                    # shear=(-16, 16),   # 剪切变换±16度,矩形变平行四边形
                    # order=[0, 1],  # 使用最近邻插值 或 双线性插值
                    cval=0,  # 填充值
                    mode=ia.ALL  # 定义填充图像外区域的方法
                )),

                # 使用0~3个方法进行图像增强
                iaa.SomeOf((0, 3),
                           [
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.GaussianBlur((0, 2.0)),  # 高斯模糊
                                   iaa.AverageBlur(k=(1, 5)),  # 平均模糊,磨砂
                               ])),

                               # 要么运动,要么美颜
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.MotionBlur(k=(3, 11)),  # 运动模糊
                                   iaa.BilateralBlur(d=(1, 5),
                                                     sigma_color=(10, 250),
                                                     sigma_space=(10, 250)),  # 双边滤波,美颜
                               ])),

                               # 模仿雪花
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.SaltAndPepper(p=(0., 0.03)),
                                   iaa.AdditiveGaussianNoise(loc=0, scale=(0., 0.05 * 255), per_channel=False)
                               ])),

                               # 对比度
                               iaa.Sometimes(0.8, iaa.LinearContrast((0.6, 1.4), per_channel=0.5)),

                               # 锐化
                               iaa.Sometimes(0.8, iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),

                               # 整体亮度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   # 加性调整
                                   iaa.AddToBrightness((-30, 30)),
                                   # 线性调整
                                   iaa.MultiplyBrightness((0.5, 1.5)),
                                   # 加性 & 线性
                                   iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                                ])),

                               # 饱和度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToSaturation((-75, 75)),
                                   iaa.MultiplySaturation((0., 3.)),
                               ])),

                               # 色相
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToHue((-255, 255)),
                                   iaa.MultiplyHue((-3.0, 3.0)),
                               ])),

                               # 云雾
                               # iaa.Sometimes(0.3, iaa.Clouds()),

                               # 卡通化
                               # iaa.Sometimes(0.01, iaa.Cartoon()),
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
Exemplo n.º 9
0
aug_colorTemperature = iaa.ChangeColorTemperature((1100, 10000))
'''
Convert each image to a colorspace with a brightness-related channel, extract
that channel, multiply it by a factor between 0.5 and 1.5, add a value between
-30 and 30 and convert back to the original colorspace
'''
aug_brightness = iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30))
'''
Multiply the hue and saturation of images by random values;
Sample random values from the discrete uniform range [-50..50],and add them
'''
aug_hueSaturation = [
    iaa.MultiplyHue((0.5, 1.5)),
    iaa.MultiplySaturation((0.5, 1.5)),
    iaa.AddToHue((-50, 50)),
    iaa.AddToSaturation((-50, 50))
]
'''
Increase each pixel’s R-value (redness) by 10 to 100
'''
aug_redChannels = iaa.WithChannels(0, iaa.Add((10, 100)))

### add the augmenters ###
seq = iaa.Sequential([
    ## 0.5 is the probability, horizontally flip 50% of the images
    iaa.Fliplr(0.5),
    #iaa.Flipud(0.5),
    ## crop images from each side by 0 to 16px(randomly chosen)
    #iaa.Crop(percent=(0, 0.1)),
    #iaa.LinearContrast((0.75, 1.5)),
    #iaa.Multiply((0.8, 1.2), per_channel=0.2),
Exemplo n.º 10
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
''' Noisy Data Aug'''
seq_2 = iaa.Sequential(
        [
        iaa.OneOf(
            [   
            # Blur each image using a median over neihbourhoods that have a random size between 3x3 and 7x7
            sometimes(iaa.MedianBlur(k=(3, 7))),
            # blur images using gaussian kernels with random value (sigma) from the interval [a, b]
            sometimes(iaa.GaussianBlur(sigma=(0.0, 1.0))),
            sometimes(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5))
            ]
        ),
        iaa.Sequential(
            [
            sometimes(iaa.AddToHue((-8, 8))),
            sometimes(iaa.AddToSaturation((-20, 20))),
            sometimes(iaa.AddToBrightness((-26, 26))),
            sometimes(iaa.Lambda(func_images = add_to_contrast))
            ], random_order=True)
        ], random_order=True)
#%%
for p in range(iterations): # how many times to apply random augmentations
    for idx in trange(len(img_path), desc='Augumenting Dataset (iteration {}of{})'.format(p+1, iterations)):
        
        img = cv2.imread(img_path[idx])
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        filepath = xml_path[idx]
        
        full_dict = xmltodict.parse(open( filepath , 'rb' ))
        
        # Extracting the coords and class names from xml file
Exemplo n.º 12
0
def get_segments(label_list, image_dir, output_dir, max_len, pixel_constant=1):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    base_seq = iaa.Sequential([
        sometimes(0.3, iaa.AdditiveGaussianNoise(scale=(0.05, 0.4))),
        sometimes(0.3, iaa.GammaContrast(gamma=(0.8, 1.2))),
        sometimes(0.2, iaa.AddToSaturation(value=(-10, 10))),
        sometimes(0.2, iaa.GaussianBlur(sigma=(0.05, 0.3))),
    ],
                              random_order=True)

    seq_origin = iaa.Sequential([
        base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_lr = iaa.Sequential([
        iaa.Fliplr(), base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_ud = iaa.Sequential([
        iaa.Flipud(), base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_rot90 = iaa.Sequential([
        # iaa.Rotate(rotate=90),
        iaa.Rot90(k=1, keep_size=False),
        base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_rot180 = iaa.Sequential([
        # iaa.Rotate(rotate=180),
        iaa.Rot90(k=2, keep_size=False),
        base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_rot270 = iaa.Sequential([
        iaa.Rot90(k=3, keep_size=False), base_seq,
        iaa.PadToFixedSize(width=max_len,
                           height=max_len,
                           position="right-bottom")
    ])

    seq_normal = iaa.Sequential([
        base_seq,
        iaa.PadToFixedSize(width=max_len, height=max_len, position="normal")
    ])

    seq_resize = iaa.Sequential([
        base_seq,
        iaa.Resize((max_len, max_len))
        # iaa.PadToFixedSize(width=max_len, height=max_len, position="normal")
    ])

    with open(label_list, 'r') as fr:
        for i, label_path in enumerate(fr):
            label_path = label_path.strip()
            name = os.path.split(label_path)[-1]
            shortname = os.path.splitext(name)[0]

            # get points
            content = json.load(open(label_path, 'r'))
            points = get_points_from_label(content)
            psoi = Polygon(points[0])

            # get image

            image_path = glob.glob(os.path.join(image_dir,
                                                shortname + ".*"))[0]
            image = cv2.imread(image_path)

            # resize
            new_width, new_height = resize_by_scale(image, max_len)
            seq = iaa.Sequential([
                iaa.Resize({
                    "height": new_height,
                    "width": new_width
                }),
            ])
            image, psoi = seq(image=image, polygons=psoi)

            #
            image_aug, psoi_aug = seq_origin(image=image, polygons=psoi)
            mask = np.zeros((max_len, max_len), dtype=np.uint8)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_origion.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_origion.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_lr(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_horizontal.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_horizontal.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_ud(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_vertical.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_vertical.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_rot90(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_rot90.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_rot90.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_rot180(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_rot180.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_rot180.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_rot270(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_rot270.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_rot270.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_normal(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_normal.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_normal.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            #
            image_aug, psoi_aug = seq_resize(image=image, polygons=psoi)
            mask_new = convert_to_segment(
                mask.copy(),
                psoi_aug.coords.round().astype(np.int), pixel_constant)
            save_image_path = os.path.join(output_dir,
                                           shortname + "_resize.jpg")
            save_label_path = os.path.join(output_dir,
                                           shortname + "_resize.png")
            cv2.imwrite(save_image_path, image_aug)
            cv2.imwrite(save_label_path, mask_new)

            # image_polys_aug = psoi_aug.draw_on_image(image_aug)
            # cv2.imwrite("output.jpg", image_polys_aug)
            # exit()

            if i % 10 == 0:
                print("{} has done".format(i))
Exemplo n.º 13
0
    def __init__(self, args):
        if args.inference:
            logger.info(
                f"`args.inference` is set, so switching off all augmentations")
            self.seq = self.shift_seq = None
            return

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

        logger.info(f"Pixelwise augmentation: {args.use_pixelwise_augs}")
        logger.info(f"Affine scale augmentation: {args.use_affine_scale}")
        logger.info(f"Affine shift augmentation: {args.use_affine_shift}")

        total_augs = []

        if args.use_pixelwise_augs:
            pixelwise_augs = [
                iaa.SomeOf(
                    (0, 5),
                    [
                        # sometimes(iaa.Superpixels(p_replace=(0, 0.25), n_segments=(150, 200))),
                        iaa.OneOf([
                            iaa.GaussianBlur(
                                (0, 1.0)
                            ),  # blur images with a sigma between 0 and 3.0
                            iaa.AverageBlur(k=(1, 3)),
                            # blur image using local means with kernel sizes between 2 and 7
                            iaa.MedianBlur(k=(1, 3)),
                            # blur image using local medians with kernel sizes between 2 and 7
                        ]),
                        iaa.Sharpen(alpha=(0, 1.0),
                                    lightness=(1.0, 1.5)),  # sharpen images
                        iaa.Emboss(alpha=(0, 1.0),
                                   strength=(0, 0.5)),  # emboss images
                        # search either for all edges or for directed edges,
                        # blend the result with the original image using a blobby mask
                        iaa.BlendAlphaSimplexNoise(
                            iaa.EdgeDetect(alpha=(0.0, 0.15)), ),
                        iaa.AdditiveGaussianNoise(
                            loc=0, scale=(0.0, 0.05 * 255), per_channel=False),
                        # add gaussian noise to images
                        iaa.Add((-10, 10), per_channel=0.5),
                        # change brightness of images (by -10 to 10 of original value)
                        iaa.AddToSaturation(
                            (-20, 20)),  # change hue and saturation
                        iaa.JpegCompression((70, 99)),
                        iaa.Multiply((0.5, 1.5), per_channel=False),
                        iaa.OneOf([
                            iaa.LinearContrast(
                                (0.75, 1.25), per_channel=False),
                            iaa.SigmoidContrast(cutoff=0.5, gain=(3.0, 11.0))
                        ]),
                        sometimes(
                            iaa.ElasticTransformation(alpha=(0.5, 3.5),
                                                      sigma=0.15)),
                        # move pixels locally around (with random strengths)
                    ],
                    random_order=True)
            ]
            total_augs.extend(pixelwise_augs)

        if args.use_affine_scale:
            affine_augs_scale = [
                sometimes(
                    iaa.Affine(
                        scale={
                            "x": (0.8, 1.2),
                            "y": (0.8, 1.2)
                        },
                        # scale images to 80-120% of their size, individually per axis
                        order=[1],  # use  bilinear interpolation (fast)
                        mode=["reflect"]))
            ]
            total_augs.extend(affine_augs_scale)

        if args.use_affine_shift:
            affine_augs_shift = [
                sometimes(
                    iaa.Affine(
                        translate_percent={
                            "x": (-0.05, 0.05),
                            "y": (-0.05, 0.05)
                        },
                        order=[1],  # use bilinear interpolation (fast)
                        mode=["reflect"]))
            ]
        else:
            affine_augs_shift = []

        self.shift_seq = iaa.Sequential(affine_augs_shift)
        self.seq = iaa.Sequential(total_augs, random_order=True)
Exemplo n.º 14
0
    ## Color

    elif augmentation == 'multiply_hue':
        transform = iaa.MultiplyHue((0.5, 1.5))
        transformed_image = transform(image=image)

    elif augmentation == 'addto_hue':
        transform = iaa.AddToHue((-100, 100))
        transformed_image = transform(image=image)
    
    elif augmentation == 'multiply_saturation':
        transform = iaa.MultiplySaturation((0.5, 1.5))
        transformed_image = transform(image=image)
    
    elif augmentation == 'addto_saturation':
        transform = iaa.AddToSaturation((-100, 100))
        transformed_image = transform(image=image)
    
    elif augmentation == 'saturate':
        transform = iaa.imgcorruptlike.Saturate(severity=5)
        transformed_image = transform(image=image)

    elif augmentation == 'remove_saturation':
        transform = iaa.RemoveSaturation()
        transformed_image = transform(image=image)
    
    elif augmentation == 'multiply_hue_and_saturation':
        transform = iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)
        transformed_image = transform(image=image)

    elif augmentation == 'brightness_contrast':