Example #1
0
    def augmentation(self, pil_img, pil_mask):
        input_img = np.expand_dims(pil_img, axis=0)
        input_mask = np.expand_dims(pil_mask, axis=0)
        input_mask = np.expand_dims(input_mask, axis=3)

        prob = random.uniform(0, 1)
        if self.split == 'train' and prob > 0.5:# we do augmentation in 50% of the cases
            seq = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace=self.color_map),
                iaa.ChannelShuffle(0.35),
                iaa.Affine(translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}),
                iaa.Affine(rotate=(-180, 180)),
                iaa.Affine(shear=(-16, 16)),
                iaa.Fliplr(0.5),
                iaa.GaussianBlur(sigma=(0, 3.0))
            ])
            images_aug, segmaps_aug = seq(images=input_img, segmentation_maps=input_mask)

            output_img = np.transpose(images_aug[0], (2, 0, 1))
            output_mask = np.transpose(segmaps_aug[0], (2, 0, 1))
        else:
            seq = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace=self.color_map),
            ])
            images_aug, segmaps_aug = seq(images=input_img, segmentation_maps=input_mask)
            output_img = np.transpose(images_aug[0], (2, 0, 1))
            output_mask = np.transpose(segmaps_aug[0], (2, 0, 1))



        return output_img, output_mask
Example #2
0
def get_seq(params):
    """
    Main filters and augmentations for pilotnet data augmentation.
    """
    filters = iaa.SomeOf(params.filters_repeat, [
        iaa.ChangeColorspace("BGR"),
        iaa.ChangeColorspace("GRAY"),
        iaa.GaussianBlur(sigma=(0.0, 3.0)),
        iaa.AverageBlur(k=(2, 9)),
        iaa.MedianBlur(k=(3, 9)),
        iaa.Add((-40, 40), per_channel=0.5),
        iaa.Add((-40, 40)),
        iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5),
        iaa.AdditiveGaussianNoise(scale=0.05 * 255),
        iaa.Multiply((0.5, 1.5), per_channel=0.5),
        iaa.Multiply((0.5, 1.5)),
        iaa.MultiplyElementwise((0.5, 1.5)),
        iaa.ContrastNormalization((0.5, 1.5)),
        iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
        iaa.ElasticTransformation(alpha=(0, 2.5), sigma=0.25),
        iaa.Sharpen(alpha=(0.6, 1.0)),
        iaa.Emboss(alpha=(0.0, 0.5)),
        iaa.CoarseDropout(0.2, size_percent=0.00001, per_channel=1.0),
    ])
    affine = iaa.Affine(
        rotate=(-7, 7),
        scale=(0.9, 1.1),
        translate_percent=dict(x=(-0.05, 0.05)),
        mode="symmetric",
    )

    return iaa.Sequential([
        filters,
        affine,
    ])
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 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
Example #5
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")])
Example #6
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)
Example #7
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")
     ])
Example #8
0
def example(imgs, pieceWiseAffine, rotate):
    # 옵션별로 aug 설정
    if imgs.dtype != np.uint8:
        imgs = imgs.astype(np.uint8)

    if_ = lambda tf, t, f: t if tf else f

    ia.seed(int(time.time()))
    sometimes1 = lambda aug: iaa.Sometimes(0.1, aug)
    sometimes3 = lambda aug: iaa.Sometimes(0.3, aug)
    sometimes5 = lambda aug: iaa.Sometimes(0.5, aug)
    sometimes7 = lambda aug: iaa.Sometimes(0.7, aug)
    sometimes9 = lambda aug: iaa.Sometimes(0.9, aug)

    seq = iaa.Sequential(
        [
            sometimes5(
                iaa.PiecewiseAffine(
                    scale=if_(pieceWiseAffine, (0.03, 0.03), 0.0))),
            sometimes1(iaa.ElasticTransformation(alpha=(0, 1.0), sigma=0.1)),
            sometimes3(iaa.Affine(shear=(-12, 12))),
            iaa.Crop(percent=(0.0, 0.25)),  # 항상 하는게 좋음 (crop2 기준)
            iaa.SomeOf(if_(rotate, 1, 0), [
                iaa.Affine(rotate=0),
                iaa.Affine(rotate=90),
                iaa.Affine(rotate=180),
                iaa.Affine(rotate=270)
            ]),
            sometimes3(iaa.ContrastNormalization((0.7, 1.3), per_channel=0.5)),
            sometimes1(iaa.Dropout(p=(0, 0.01), per_channel=0.5)),
            sometimes1(iaa.Add((-40, 40), per_channel=0.5)),
            sometimes1(iaa.Sharpen(alpha=(0.3, 0.7), lightness=(0.75, 1.25))),
            sometimes1(iaa.MedianBlur(k=(3, 9))),
            sometimes1(iaa.GaussianBlur(sigma=(1.0, 2.5))),
            sometimes1(iaa.Grayscale(alpha=(0.1, 1.0))),
            sometimes1(
                iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB",
                                         to_colorspace="HSV"),
                    iaa.WithChannels(0, iaa.Add((30, 70))),
                    iaa.ChangeColorspace(from_colorspace="HSV",
                                         to_colorspace="RGB")
                ])),
        ],
        random_order=False)  # apply augmenters in random order

    aug_imgs = seq.augment_images(imgs)

    if aug_imgs.dtype != np.float32:
        aug_imgs = aug_imgs.astype(np.float32)

    return aug_imgs
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(),
    ])
Example #10
0
def zanoni_aug():
    return iaa.SomeOf(
        (1, 4),
        [
            # OKS
            iaa.Noop(),
            iaa.Grayscale(alpha=(0.25, 1.0)),
            iaa.Flipud(0.45),
            iaa.Fliplr(0.45),
            iaa.AddToHueAndSaturation((-40, 40)),
            iaa.ContrastNormalization((0.8, 1.2), per_channel=0.33),
            iaa.AdditiveGaussianNoise(scale=0.02 * 255),
            iaa.GaussianBlur(sigma=(0.1, 1.0)),
            iaa.OneOf([
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="BGR")
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="YCrCb"),
                #iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSL")
            ]),
            iaa.OneOf([
                iaa.Emboss(alpha=(0.0, 0.33), strength=(0.02, 0.25)),
                iaa.Sharpen(alpha=(0.0, 0.33), lightness=(0.02, 0.25))
            ]),
            iaa.OneOf([
                iaa.Add((-15, 15), per_channel=0.25),
                iaa.Multiply((0.5, 2.0), per_channel=0.25)
            ])
            #iaa.CoarseDropout((0.1, 0.25), size_percent=(0.5, 0.75), per_channel=0.5)
        ])
Example #11
0
def saturation(origImage, value):
    '''
    Increases/decreeases the saturation of the image
    
    origImage: Array of image pixels as numpy array
    value: Value to add/subtract saturation by  

    Returns: Augmented image array with saturation changed , value
    '''

    aug = iaa.Sequential([
        iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
        iaa.WithChannels(1, iaa.Add(value)),
        iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
    ])
    return (aug, value)
Example #12
0
def run_and_save_augseq(filename,
                        augseq,
                        images,
                        cols,
                        rows,
                        quality=95,
                        seed=1,
                        image_colorspace="RGB"):
    ia.seed(seed)
    # augseq may be a single seq (applied to all images) or a list (one seq per
    # image).
    # use type() here instead of isinstance, because otherwise Sequential is
    # also interpreted as a list
    if type(augseq) == list:
        # one augmenter per image specified
        assert len(augseq) == len(images)
        images_aug = [
            augseq[i].augment_image(images[i]) for i in range(len(images))
        ]
    else:
        # calling N times augment_image() is here critical for random order in
        # Sequential
        images_aug = [
            augseq.augment_image(images[i]) for i in range(len(images))
        ]

    if image_colorspace != "RGB":
        images_aug = iaa.ChangeColorspace(
            from_colorspace=image_colorspace,
            to_colorspace="RGB")(images=images_aug)

    save("overview_of_augmenters",
         filename,
         grid(images_aug, cols=cols, rows=rows),
         quality=quality)
Example #13
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")
    ])
Example #14
0
def train(model, dataset_dir, subset):
    """Train the model."""
    # Training dataset.
    dataset_train = NucleusDataset()
    dataset_train.load_nucleus(dataset_dir, subset)
    dataset_train.prepare()

    # Validation dataset
    dataset_val = NucleusDataset()
    dataset_val.load_nucleus(dataset_dir, "val")
    dataset_val.prepare()

    # Image augmentation
    # http://imgaug.readthedocs.io/en/latest/source/augmenters.html
    augmentation = iaa.Sequential([
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(
            0.75,
            iaa.OneOf([
                iaa.Affine(rotate=90),
                iaa.Affine(rotate=180),
                iaa.Affine(rotate=270)
            ])),
        iaa.SomeOf(
            (0, 3),
            [
                iaa.Multiply((0.8, 1.5)),
                iaa.GaussianBlur(sigma=(0.0, 3.0)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.AdditiveGaussianNoise(scale=0.02 * 255),
                iaa.Sharpen(alpha=0.5),
                iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
                iaa.Grayscale(alpha=(0.0, 1.0)),
                iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5),
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV")  # maybe not
            ])
    ])

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

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

    print("Train all layers")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=40,
                augmentation=augmentation,
                layers='all')
Example #15
0
    def augmentation(self, pil_img, pil_mask):
        input_img = np.expand_dims(pil_img, axis=0)
        input_mask = np.expand_dims(pil_mask, axis=0)
        input_mask = np.expand_dims(input_mask, axis=3)

        prob = random.uniform(0, 1)
        if self.split == 'train' and prob > 0.5:  # we do augmentation in 50% of the cases
            seq = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace=self.color_map),
                iaa.ChannelShuffle(0.35),
                # iaa.Cutout(nb_iterations=(1, 5), size=0.1, squared=False, fill_mode="constant", cval=0),
                # iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
                # iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                # iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True),
                # iaa.GammaContrast((0.5, 2.0)),
                iaa.Affine(translate_percent={
                    "x": (-0.2, 0.2),
                    "y": (-0.2, 0.2)
                }),
                iaa.Affine(rotate=(-180, 180)),
                iaa.Affine(shear=(-16, 16)),
                iaa.Fliplr(0.5),
                iaa.GaussianBlur(sigma=(0, 3.0))
            ])
            images_aug, segmaps_aug = seq(images=input_img,
                                          segmentation_maps=input_mask)

            # if we would like to see the data augmentation
            # segmaps_aug = np.concatenate((segmaps_aug,segmaps_aug,segmaps_aug), 3)
            # seq.show_grid([images_aug[0], segmaps_aug[0]*255], cols=16, rows=8)

            output_img = np.transpose(images_aug[0], (2, 0, 1))
            output_mask = np.transpose(segmaps_aug[0], (2, 0, 1))
        else:
            seq = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace=self.color_map),
            ])
            images_aug, segmaps_aug = seq(images=input_img,
                                          segmentation_maps=input_mask)
            output_img = np.transpose(images_aug[0], (2, 0, 1))
            output_mask = np.transpose(segmaps_aug[0], (2, 0, 1))

        return output_img, output_mask
 def __call__(self, sample):
     image, polygon, labels = sample["image"], sample["polygon"], sample[
         "labels"]
     t = iaa.ChangeColorspace(from_colorspace=self.from_colorspace,
                              to_colorspace=self.to_colorspace)
     image = np.array(image)
     img = t(image=image)
     image = Image.fromarray(img)
     sample = {'image': image, 'polygon': polygon, 'labels': labels}
     return sample
Example #17
0
  def __init__(self):
    self.aug = iaa.Sequential([
        iaa.Scale((224, 224)),
        iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))),
        iaa.Affine(rotate=(-20, 20), mode='constant'),
        iaa.Sometimes(0.25,
                      iaa.OneOf([iaa.Dropout(p=(0, 0.1)),
                                 iaa.CoarseDropout(0.1, size_percent=0.5),
				 iaa.Sharpen(alpha=0.5)])),
        iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True),
	iaa.Sometimes(0.15,iaa.WithChannels(0, iaa.Add((10, 100)))),
	iaa.Sometimes(0.05,iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"))
    ])
def image_aug(images):
    a1 = iaa.Sometimes(0.8, iaa.Fliplr(0.5))
    a2 = iaa.Sometimes(0.8, iaa.CoarseDropout(p=0.1, size_percent=0.1))
    a3 = iaa.Sometimes(
        0.8,
        iaa.Affine(
            scale={
                "x": (0.80, 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=(-30, 30),  # rotate by -15 to +15 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)
        ))
    a4 = iaa.Sometimes(0.8, iaa.Grayscale(alpha=(0.1, 1.0)))
    a5 = iaa.Sometimes(
        0.8,
        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")
        ]))
    a6 = iaa.Sometimes(0.8, iaa.GaussianBlur(sigma=(0.5, 2)))
    a7 = iaa.Sometimes(0.8, iaa.Sharpen(alpha=(0.5, 1.0),
                                        lightness=(0.75, 2.0)))
    a8 = iaa.Sometimes(0.8, iaa.EdgeDetect(alpha=(0.2, 0.5)))

    seq = iaa.Sequential([a1, a2, a3, a4, a5, a6, a7, a8], random_order=True)

    images_aug = seq.augment_images(images)
    return images_aug
Example #19
0
def random_color(data):
    """
    Changing Color Randomly for Augmentation
    reference : https://github.com/neptune-ml/data-science-bowl-2018/blob/master/augmentation.py
    """
    s = random.randint(0, 1)
    if s <= 0:
        return data
    aug = iaa.Sequential(
        [
            # Color
            iaa.OneOf([
                iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB",
                                         to_colorspace="HSV"),
                    iaa.WithChannels(0, iaa.Add((0, 100))),
                    iaa.ChangeColorspace(from_colorspace="HSV",
                                         to_colorspace="RGB")
                ]),
                iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB",
                                         to_colorspace="HSV"),
                    iaa.WithChannels(1, iaa.Add((0, 100))),
                    iaa.ChangeColorspace(from_colorspace="HSV",
                                         to_colorspace="RGB")
                ]),
                iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB",
                                         to_colorspace="HSV"),
                    iaa.WithChannels(2, iaa.Add((0, 100))),
                    iaa.ChangeColorspace(from_colorspace="HSV",
                                         to_colorspace="RGB")
                ]),
                iaa.WithChannels(0, iaa.Add((0, 100))),
                iaa.WithChannels(1, iaa.Add((0, 100))),
                iaa.WithChannels(2, iaa.Add((0, 100)))
            ])
        ],
        random_order=True)
    data.img = aug.augment_image(data.img)
    # data.masks = [aug.augment_image(mask) for mask in data.masks]
    return data
Example #20
0
def DA_ColorspaceWithChannel(inputimg, AUG_OPS_PARAM=[-50, -25, 25, 50]):
    assert inputimg.ndim in [2, 3], "input invalid! Please check input"
    AUG_OPS_NAME = ["ColorspaceWithChannels_AddC23", "ColorspaceWithChannels_AddC2",
                    "ColorspaceWithChannels_AddC3"]
    ret = []

    for i_param in np.arange(len(AUG_OPS_PARAM)):
        VALUE = AUG_OPS_PARAM[i_param]
        for i_name in np.arange(len(AUG_OPS_NAME)):
            Name = AUG_OPS_NAME[i_name]
            if Name == "ColorspaceWithChannels_AddC23":
                aug = iaa.Sequential([
                                iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                                iaa.WithChannels([1,2], iaa.Add(VALUE)),
                                iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
                            ])
                augimg = aug.augment_image(inputimg)
                ret.append((Name, VALUE, augimg))
            elif Name == "ColorspaceWithChannels_AddC2":
                aug = iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                    iaa.WithChannels(1, iaa.Add(VALUE)),
                    iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
                ])
                augimg = aug.augment_image(inputimg)
                ret.append((Name, VALUE, augimg))
            elif Name == "ColorspaceWithChannels_AddC3":
                aug = iaa.Sequential([
                    iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                    iaa.WithChannels(2, iaa.Add(VALUE)),
                    iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
                ])
                augimg = aug.augment_image(inputimg)
                ret.append((Name, VALUE, augimg))
            else:
                print "invalid ops in DA_WithChannels_Add"
    assert len(ret) == 12, "DA_ColorspaceWithChannel Fails"
    return ret
Example #21
0
def color_augmentation():
    return iaa.Sequential([
        # Color
        iaa.OneOf([
            iaa.Sequential([
                iaa.ChangeColorspace(
                    from_colorspace="RGB", to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((0, 100))),
                iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
            iaa.Sequential([
                iaa.ChangeColorspace(
                    from_colorspace="RGB", to_colorspace="HSV"),
                iaa.WithChannels(1, iaa.Add((0, 100))),
                iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
            iaa.Sequential([
                iaa.ChangeColorspace(
                    from_colorspace="RGB", to_colorspace="HSV"),
                iaa.WithChannels(2, iaa.Add((0, 100))),
                iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
            iaa.WithChannels(0, iaa.Add((0, 100))),
            iaa.WithChannels(1, iaa.Add((0, 100))),
            iaa.WithChannels(2, iaa.Add((0, 100)))
        ])
    ], random_order=True)
Example #22
0
    def augmenter_function(self, images, labels):

        high = lambda aug: iaa.Sometimes(0.9, aug)
        medium = lambda aug: iaa.Sometimes(0.6, aug)
        low = lambda aug: iaa.Sometimes(0.15, aug)

        if (self._augment_amount == 3):  #max augmentation
            road_aug = iaa.Sequential([
                iaa.Multiply((0.5, 1.25)),
                #texture
                iaa.SomeOf(
                    1,
                    [
                        #iaa.AdditiveGaussianNoise(scale=(0, 0.01*255)),
                        iaa.AddElementwise((-20, 20)),
                        medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))
                    ])
            ])

            building_aug = iaa.Sequential([
                iaa.Multiply((0.5, 1.5), per_channel=0.8),
                #texture
                #iaa.SomeOf(1, [
                #high(iaa.AdditiveGaussianNoise(scale=(0, 0.03*255))),
                #medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))])
            ])

            grass_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                (iaa.WithChannels(0, iaa.Add((50, -50)))),
                iaa.WithChannels(2, iaa.Add((-30, 30))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                #texture
                iaa.SomeOf(
                    2,
                    [
                        medium(
                            iaa.AdditiveGaussianNoise(scale=(0, 0.06 * 255))),
                        medium(iaa.AddElementwise((-25, 25))),
                        medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))
                        #low(iaa.Superpixels(p_replace=1, n_segments=(126, 128)))
                    ])
            ])

            sky_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((-20, 20))),
                iaa.WithChannels(1, iaa.Add((0, 30))),
                iaa.WithChannels(2, iaa.Add((-10, 0))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                #medium(iaa.WithChannels(2, iaa.Add((-40, 40)))),
                #texture
                #iaa.SomeOf(1, [
                #medium(iaa.AddElementwise((-5, 5))),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.6, 1.4)))
                #low(iaa.Superpixels(p_replace=1, n_segments=(126, 144)))])
            ])

        elif (self._augment_amount == 2):  #mid augmentation
            road_aug = iaa.Sequential([
                high(iaa.Multiply((0.75, 1.15))),
                #texture
                iaa.SomeOf(
                    1,
                    [
                        #iaa.AdditiveGaussianNoise(scale=(0, 0.01*255)),
                        iaa.AddElementwise((-8, 8)),
                        medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))
                    ])
            ])

            building_aug = iaa.Sequential([
                iaa.Multiply((0.5, 1.5), per_channel=0.8),
                #texture
                #iaa.SomeOf(1, [
                #high(iaa.AdditiveGaussianNoise(scale=(0, 0.03*255))),
                #medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))])
            ])

            grass_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                (iaa.WithChannels(0, iaa.Add((50, -50)))),
                iaa.WithChannels(2, iaa.Add((-30, 30))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                #texture
                iaa.SomeOf(
                    1,
                    [
                        medium(
                            iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))),
                        medium(iaa.AddElementwise((-11, 11))),
                        medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 0.75), strength=(0.5, 1.5)))
                        #low(iaa.Superpixels(p_replace=1, n_segments=(126, 128)))
                    ])
            ])

            sky_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((-30, 30))),
                iaa.WithChannels(1, iaa.Add((0, 30))),
                iaa.WithChannels(2, iaa.Add((-10, 0))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                #medium(iaa.WithChannels(2, iaa.Add((-40, 40)))),
                #texture
                #iaa.SomeOf(1, [
                #medium(iaa.AddElementwise((-5, 5))),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.6, 1.4)))
                #low(iaa.Superpixels(p_replace=1, n_segments=(126, 144)))])
            ])

        elif (self._augment_amount == 1):  #min augmentation
            road_aug = iaa.Sequential([
                high(iaa.Multiply((0.9, 1.05))),
                #texture
                iaa.SomeOf(
                    1,
                    [
                        #iaa.AdditiveGaussianNoise(scale=(0, 0.01*255)),
                        iaa.AddElementwise((-3, 3)),
                        medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 1.0), strength=(0.8, 1.2)))
                    ])
            ])

            building_aug = iaa.Sequential([
                iaa.Multiply((0.5, 1.5), per_channel=0.8),
                #texture
                #iaa.SomeOf(1, [
                #high(iaa.AdditiveGaussianNoise(scale=(0, 0.03*255))),
                #medium(iaa.Sharpen(alpha=(0.0, 1.0), lightness=1)),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)))])
            ])

            grass_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((50, -50))),
                iaa.WithChannels(2, iaa.Add((-30, 30))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                #texture
                iaa.SomeOf(
                    1,
                    [
                        medium(
                            iaa.AdditiveGaussianNoise(scale=(0, 0.02 * 255))),
                        medium(iaa.AddElementwise((-10, 10))),
                        medium(iaa.Sharpen(alpha=(0.0, 0.5), lightness=1)),
                        medium(
                            iaa.Emboss(alpha=(0.0, 0.5), strength=(0.5, 1.5)))
                        #low(iaa.Superpixels(p_replace=1, n_segments=(126, 128)))
                    ])
            ])

            sky_aug = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((-20, 20))),
                iaa.WithChannels(1, iaa.Add((0, 10))),
                iaa.WithChannels(2, iaa.Add((-10, 0))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                medium(iaa.WithChannels(2, iaa.Add((-40, 40)))),
                #texture
                #iaa.SomeOf(1, [
                #medium(iaa.AddElementwise((-5, 5))),
                #medium(iaa.Emboss(alpha=(0.0, 1.0), strength=(0.6, 1.4)))
                #low(iaa.Superpixels(p_replace=1, n_segments=(126, 144)))])
            ])
        '''fence = np.zeros_like(images)
        cond = labels == 2
        b = np.repeat(cond[:, :, :, np.newaxis], 3, axis=3)
        fence[b[:,:,:,:,0]] = images[b[:,:,:,:,0]]'''

        if self._labels_to_augment["road"] == True:
            road = np.zeros_like(images)
            cond = labels == 8
            b = np.repeat(cond[:, :, :, np.newaxis], 3, axis=3)
            road[b[:, :, :, :, 0]] = images[b[:, :, :, :, 0]]

            new_road = sky_aug.augment_images(road)

            images = images + new_road - road

        if self._labels_to_augment["buildings"] == True:
            buildings = np.zeros_like(images)
            cond = labels == 1
            b = np.repeat(cond[:, :, :, np.newaxis], 3, axis=3)
            buildings[b[:, :, :, :, 0]] = images[b[:, :, :, :, 0]]

            new_buildings = sky_aug.augment_images(buildings)

            images = images + new_buildings - buildings

        if self._labels_to_augment["grass"] == True:
            grass = np.zeros_like(images)
            cond = labels == 9
            b = np.repeat(cond[:, :, :, np.newaxis], 3, axis=3)
            grass[b[:, :, :, :, 0]] = images[b[:, :, :, :, 0]]

            new_grass = sky_aug.augment_images(grass)

            images = images + new_grass - grass

        if self._labels_to_augment["sky_n_zebra"] == True:

            sky_n_zebra = np.zeros_like(images)
            cond = labels == 0
            b = np.repeat(cond[:, :, :, np.newaxis], 3, axis=3)
            sky_n_zebra[b[:, :, :, :, 0]] = images[b[:, :, :, :, 0]]

            new_sky_n_zebra = sky_aug.augment_images(sky_n_zebra)

            images = images + new_sky_n_zebra - sky_n_zebra

        #final = road + buildings + grass + sky_n_zebra + fence
        #comp = np.hstack((initial,final))

        #initial = buildings+grass+fence+road+sky_n_zebra

        return images
Example #23
0
    def __init__(self,
                 src='train',
                 img_size=256,
                 validation_pct=0,
                 testing_pct=0,
                 fold_keys=None,
                 valid_same=True,
                 use_mosaics=False,
                 excl_keys=None):
        """
            Build data processor for yielding train, valid and test data

            Providing a fold_key will put all data with the given cluster in the validation set and everything else
            in training. The percentages are thus ignored.
            
            'valid_same' parameter will assure the sampling points on 'valid' mode will be the same across calls
        """
        self.src_dir = os.path.join('model-data', src)
        self.img_size = img_size
        self.validation_pct = validation_pct
        self.testing_pct = testing_pct
        self.fold_keys = fold_keys if fold_keys is None or isinstance(
            fold_keys, list) else [fold_keys]
        self.excl_keys = excl_keys if excl_keys is None or isinstance(
            excl_keys, list) else [excl_keys]
        self.data_index = {'train': [], 'valid': [], 'test': []}
        self.data_dist = {
            'train': defaultdict(int),
            'valid': defaultdict(int),
            'test': defaultdict(int)
        }
        self.use_mosaics = use_mosaics

        self._generate_data_index()

        self.valid_seeds = None
        if valid_same:
            self.valid_seeds = np.random.rand(self.mode_size(mode='valid'), 2)

        # Color augmentation
        self._color_aug = iaa.Sometimes(
            0.5,
            iaa.Sequential([
                iaa.OneOf([
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(0, iaa.Add((0, 100))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(1, iaa.Add((0, 100))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(2, iaa.Add((0, 100))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.WithChannels(0, iaa.Add((0, 100))),
                    iaa.WithChannels(1, iaa.Add((0, 100))),
                    iaa.WithChannels(2, iaa.Add((0, 100)))
                ])
            ],
                           random_order=True))

        # Image augmentation (noise, blur, contrast, brightness)
        self._image_aug = iaa.Sometimes(
            0.5,
            iaa.OneOf([
                iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
                iaa.GaussianBlur(sigma=(0, 1.0)),
                iaa.ContrastNormalization((0.9, 1.1)),
                iaa.Multiply((0.8, 1.2))
            ]))
Example #24
0
def load_data_aug(name,
                  path_to_train,
                  path_to_target,
                  size=512,
                  color=0,
                  aug=3,
                  nb_class=5):
    imgs = []
    labels = []
    img_ori = Image.open(path_to_train + "or{}.png".format(name))
    label_ori = Image.open(path_to_target + "col{}.png".format(name))
    w, h = img_ori.size
    cn = iaa.SomeOf(
        (0, 1),
        [
            iaa.ContrastNormalization((0.7, 1.3)),  # 明るさ正規化
            iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((-10, 10))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB")
            ]),
            iaa.Grayscale(alpha=(0, 0.5))
        ])

    cn2 = iaa.SomeOf(
        (0, 1),
        [
            iaa.ContrastNormalization((0.7, 1.3)),  # 明るさ正規化
            iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((-10, 10))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB")
            ]),
        ])

    for i in range(aug):
        # ランダムにcrop
        lw = np.random.randint(0, (w - size))
        lh = np.random.randint(0, (h - size))
        img = img_ori.crop((lw, lh, lw + size, lh + size))
        label = label_ori.crop((lw, lh, lw + size, lh + size))

        # ランダムに反転
        j = np.random.randint(3)
        if j == 1:
            img = ImageOps.flip(img)
            label = ImageOps.flip(label)
        elif j == 2:
            img = ImageOps.mirror(img)
            label = ImageOps.mirror(label)

        if color == 0:
            # そのまま
            img = np.array(img, dtype=np.uint8)
        if color == 1:
            # gray scale
            img = img.convert('L')
            img = np.array(img, dtype=np.uint8)
        if color == 2:
            # augmentation color and gray
            img = np.array(img, dtype=np.uint8)
            img = cn.augment_image(img)
        if color == 3:
            # augmentation color
            img = np.array(img, dtype=np.uint8)
            img = cn2.augment_image(img)

        # ランダムにratate
        t = np.random.rand() * 90
        rotate = iaa.Affine(rotate=(t, t))
        img = rotate.augment_image(img)
        label = np.array(label, dtype=np.int32)
        label = rotate.augment_image(label)

        label = multi_label(label, size, nb_class)
        imgs.append(img)
        labels.append(label)

    labels = np.array(labels)
    imgs = np.array(imgs, dtype=np.float64)
    if color == 1:
        imgs = imgs / 255.0
    else:
        # imgs = preprocess_input(imgs)
        imgs = imgs / 255.0
    return imgs, labels
Example #25
0
    def __getitem__(self, index):
        """
        Returns
        - path: (str) Image filename
        - img: (Tensor)
        - annos: (Tensor) Annotation with size [100, 5]
            5 = [xc, yc, w, h, label]
        """
        coco = self.coco
        img_id = self.ids[index]
        ann_ids = coco.getAnnIds(imgIds=img_id)
        path = coco.loadImgs(img_id)[0]['file_name']
        img = Image.open(os.path.join(self.root, path)).convert('RGB')
        w, h = img.size
        target = coco.loadAnns(ann_ids)
        annos = torch.zeros(len(target), 5)

        # if self.reso is None, skip the whole image augmentation part.
        # Apply image augmentations before coordinate conversion, resizing and toTensor transform.
        if self.reso is not None:
            #Convert image to numpy array
            img = np.array(img)

            # Get all the annotations (bboxes)
            bboxes = []
            for i in range(len(target)):
                bbox = torch.Tensor(target[i]['bbox'])
                bbox_x = bbox[0]
                bbox_y = bbox[1]
                bbox_w = bbox[2]
                bbox_h = bbox[3]
                bboxes.append(
                    BoundingBox(x1=bbox_x,
                                y1=bbox_y,
                                x2=bbox_x + bbox_w,
                                y2=bbox_y + bbox_h))
            bbs = BoundingBoxesOnImage(bboxes, shape=img.shape)

            # NOTE: resize first, otherwise rotating will crop the image (since image is not square)
            # Rescale image and bounding boxes
            img = ia.imresize_single_image(img, (self.reso, self.reso))
            bbs = bbs.on(img)

            # Define augmentations
            seq = iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(1, iaa.Add((-50, 50))),  # Change saturation
                iaa.WithChannels(2, iaa.Add((-50, 50))),  # Change intensity
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB"),
                iaa.Sometimes(
                    0.5,
                    iaa.Affine(rotate=90)),  # Rotate 90 deg. (0.5 probability)
                iaa.Affine(shear=(-2, 2)),  # Shear (-2 +2 degrees)
                iaa.Flipud(0.5),  # Flip up-down (with 0.5 probability)
                iaa.Fliplr(0.5)  # Flip left-right (with 0.5 probability)
            ])

            img_aug, bbs_aug = seq(image=img, bounding_boxes=bbs)

            # # DEBUG: output image
            # image_after = bbs_aug.draw_on_image(img_aug)
            # # Convert numpy array to PIL image
            # image_after = Image.fromarray(image_after, 'RGB')
            # image_after.save('debug.png')
            # print(path)
            # print("DEBUG IMAGE SAVED")
            # print(bbox)
            # print(bbs_aug)
            # # sys.exit(0)

            # Overwrite original image
            img = img_aug

        for i in range(len(target)):
            # [x1, y1, w, h] => [xc, yc, w, h]

            # if self.reso is not None it means we have to update the bounding boxes coordinates
            if self.reso is not None:
                bbox = []
                bbox.append(int(bbs_aug.bounding_boxes[i].x1))
                bbox.append(int(bbs_aug.bounding_boxes[i].y1))
                bbox.append(
                    int(bbs_aug.bounding_boxes[i].x2) -
                    int(bbs_aug.bounding_boxes[i].x1))
                bbox.append(
                    int(bbs_aug.bounding_boxes[i].y2) -
                    int(bbs_aug.bounding_boxes[i].y1))
                bbox = torch.Tensor(bbox)
                w = self.reso
                h = self.reso
                # print(bbox)
                # print()
            else:
                bbox = torch.Tensor(target[i]['bbox'])

            annos[i, 0] = (bbox[0] + bbox[2] / 2) / w
            annos[i, 1] = (bbox[1] + bbox[3] / 2) / h
            annos[i, 2] = bbox[2] / w
            annos[i, 3] = bbox[3] / h
            annos[i, 4] = self.class_map[int(target[i]['category_id'])]

        #if self.transform is not None:
        #    img = self.transform(img)

        # ndarray -> Tensor conversion
        img = torch.from_numpy(img.transpose((2, 0, 1)).copy())
        if isinstance(img, torch.ByteTensor):
            img = img.float().div(255)

        return path, img, annos
Example #26
0
def aug_seq():
    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.4),  # horizontal flips
            iaa.Flipud(0.4),  # vertical flips
            # iaa.Crop(percent=(0, 0.1)), # random crops
            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            # iaa.Sometimes(
            #    0.3,
            #    iaa.GaussianBlur(sigma=(0, 0.5))
            # ),
            # Strengthen or weaken the contrast in each image.
            iaa.LinearContrast((0.75, 1.5)),
            # 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.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            # iaa.Sometimes(
            #    0.5,
            #    iaa.Multiply((0.8, 1.2), per_channel=0.2)
            # ),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            iaa.Sometimes(
                0.5,
                iaa.Affine(
                    # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                    # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
                    rotate=(-25, 25),
                    shear=(-8, 8)
                )  # to add shear back, remove the second ) above, and add the , back as well
            ),
            iaa.Sometimes(
                0.10,
                iaa.OneOf([
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(0, iaa.Add((0, 50))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(1, iaa.Add((0, 50))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.Sequential([
                        iaa.ChangeColorspace(from_colorspace="RGB",
                                             to_colorspace="HSV"),
                        iaa.WithChannels(2, iaa.Add((0, 50))),
                        iaa.ChangeColorspace(from_colorspace="HSV",
                                             to_colorspace="RGB")
                    ]),
                    iaa.WithChannels(0, iaa.Add((0, 50))),
                    iaa.WithChannels(1, iaa.Add((0, 50))),
                    iaa.WithChannels(2, iaa.Add((0, 50)))
                ]))
        ],
        random_order=True)  # apply augmenters in random order
    return seq
Example #27
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import numpy as np
import cv2
from imgaug import augmenters as iaa

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(),
def create_augmenters(height, width, height_augmentable, width_augmentable,
                      only_augmenters):
    def lambda_func_images(images, random_state, parents, hooks):
        return images

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

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

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

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

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

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

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

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

    return augmenters
Example #29
0
     #t.RandomCrop.Both(original_size=(1918, 1280), crop_size=(config.INPUT_SIZE, config.INPUT_SIZE)),
     t.ExpandDims.Mask(axis=2),
     t.ImgAug.Both(iaa.Fliplr(0.5)),
     t.ImgAug.Both(
         iaa.Affine(scale={
             'x': (0.95, 1.05),
             'y': (0.95, 1.05)
         },
                    rotate=(-16, 16),
                    shear=(-16, 16),
                    order=[0, 1],
                    mode='reflect'),
         p=0.25,
     ),
     t.ImgAug.Image(iaa.Sequential([
         iaa.ChangeColorspace(from_colorspace='RGB', to_colorspace='HSV'),
         iaa.WithChannels([0], iaa.Add((-30, 30))),
         iaa.ChangeColorspace(from_colorspace='HSV', to_colorspace='RGB')
     ]),
                    p=0.25),
     t.Clip.Image(),
 ),
 'crop_fliplr': (
     t.Resize.Both((config.INPUT_SIZE, config.INPUT_SIZE)),
     #t.RandomCrop.Both(original_size=(1918, 1280), crop_size=(config.INPUT_SIZE, config.INPUT_SIZE)),
     t.ExpandDims.Mask(axis=2),
     t.ImgAug.Both(iaa.Fliplr(0.5)),
 ),
 'pad_fliplr_affine_color': (
     t.Pad.Both(size=(0, 0, 1, 1)),
     t.ExpandDims.Mask(axis=2),
Example #30
0
def gen_batch_function(data_folder, image_shape):
    """
    Generate function to create batches of training data
    :param data_folder: Path to folder that contains all the datasets
    :param image_shape: Tuple - Shape of image
    :return:
    """
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    # apply to images
    seq_images = iaa.Sequential(
        [
            # Change the colorspace from RGB to HSV, then add 0-100 to the first channel (hue),
            # then convert back to RGB.
            iaa.Sequential([
                iaa.ChangeColorspace(from_colorspace="RGB",
                                     to_colorspace="HSV"),
                iaa.WithChannels(0, iaa.Add((0, 100))),
                iaa.ChangeColorspace(from_colorspace="HSV",
                                     to_colorspace="RGB")
            ]),
            # Strengthen or weaken the contrast in each image.
            iaa.ContrastNormalization((0.75, 1.5)),
            # Multiply 50% of all images with a random value between 0.5 and 2.0
            # and multiply the remaining 50% channel-wise.
            iaa.Multiply((0.5, 2.0), per_channel=0.5),
            # Sometimes blur image using a mean over neihbourhoods that have a random size between 1x1 and 5x5,
            # or sharpen an image, then overlay the results with the original using an alpha between 0.0 and 1.0.
            sometimes(
                iaa.OneOf([
                    iaa.AverageBlur(k=(1, 5)),
                    iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
                ])),
            # 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.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Sometimes drop 2% of all pixels by converting them to black pixels,
            # but do that on a lower-resolution version of the image
            # that has 50% of the original size
            sometimes(iaa.CoarseDropout(0.02, size_percent=0.5)),
            # Sometimes sample per image a value p from the range 0<=p<=0.2
            # and then drop p percent of all pixels in the image
            sometimes(iaa.Dropout(p=(0, 0.2))),
        ],
        random_order=True  # apply augmenters in random order
    )

    # apply to both images and labels
    seq_images_labels = iaa.Sequential(
        [
            # horizontally flip 50% of all images
            iaa.Fliplr(0.5),
            sometimes(
                iaa.OneOf([
                    iaa.Affine(
                        scale={
                            'x': (0.5, 2.0),
                            'y': (0.5, 2.0)
                        },
                        rotate=(-15, 15),
                        shear=(-16, 16),
                        translate_percent={
                            "x": (-0.2, 0.2),
                            "y": (-0.2, 0.2)
                        },
                        mode='constant',
                    ),
                    iaa.PiecewiseAffine(scale=(0.01, 0.05))
                ])),
        ],
        random_order=False)

    def get_batches_fn(batch_size):
        """
        Create batches of training data
        :param batch_size: Batch Size
        :return: Batches of training data
        """
        image_paths = glob(os.path.join(data_folder, 'image_2', '*.png'))
        label_paths = {
            re.sub(r'_(lane|road)_', '_', os.path.basename(path)): path
            for path in glob(
                os.path.join(data_folder, 'gt_image_2', '*_road_*.png'))
        }
        background_color = np.array([255, 0, 0])

        random.shuffle(image_paths)
        for batch_i in range(0, len(image_paths), batch_size):
            images = []
            gt_images = []
            for image_file in image_paths[batch_i:batch_i + batch_size]:
                gt_image_file = label_paths[os.path.basename(image_file)]

                image = scipy.misc.imresize(scipy.misc.imread(image_file),
                                            image_shape)
                gt_image = scipy.misc.imresize(
                    scipy.misc.imread(gt_image_file), image_shape)

                gt_bg = np.all(gt_image == background_color, axis=2)
                gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
                gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)

                images.append(image)
                gt_images.append(gt_image)

            # these augmentations won't affect labels
            images_aug = seq_images.augment_images(images)

            # make sure we apply same augmentation for both images and labels
            seq_images_labels_deterministic = seq_images_labels.to_deterministic(
            )
            images_aug = seq_images_labels_deterministic.augment_images(
                images_aug)
            gt_images_aug = seq_images_labels_deterministic.augment_images(
                gt_images)

            yield np.array(images), np.array(gt_images)

    return get_batches_fn