Пример #1
0
    def __init__(self,
                 dataset_type,
                 dataset_path,
                 obj_id,
                 real_path,
                 mesh_path,
                 mesh_info,
                 batch_size,
                 img_res=(224, 224),
                 is_testing=False,
                 workers=3):
        self.data_type = dataset_type
        self.img_res = img_res
        self.dataset_path = dataset_path
        self.real_path = [
            os.path.join(real_path, x) for x in os.listdir(real_path)
        ]
        self.batch_size = batch_size
        self.is_testing = is_testing
        self.ply_path = mesh_path
        self.obj_id = int(obj_id)

        # multi-proc
        self.pool = multiprocessing.Pool(workers)
        #self.obsv_batch = []
        #self.anno_batch = []
        #self.real_batch = []
        #self.data_type = None

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

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

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

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

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

        c = list(zip(self.Anns, self.image_ids))
        np.random.shuffle(c)
        self.Anns, self.image_ids = zip(*c)

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

        self.n_batches = int(np.floor(len(self.image_ids) / self.batch_size))
        self.on_epoch_end()
        self.dataset_length = len(self.image_ids)
Пример #2
0
def _load_augmentation_aug_all():
    """ Load image augmentation model """
    def sometimes(aug):
        return iaa.Sometimes(0.5, aug)

    return iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.2),  # vertically flip 20% of all images
            # crop images by -5% to 10% of their height/width
            sometimes(
                iaa.CropAndPad(percent=(-0.05, 0.1),
                               pad_mode='constant',
                               pad_cval=(0, 255))),
            sometimes(
                iaa.Affine(
                    # scale images to 80-120% of their size, individually per axis
                    scale={
                        "x": (0.8, 1.2),
                        "y": (0.8, 1.2)
                    },
                    # translate by -20 to +20 percent (per axis)
                    translate_percent={
                        "x": (-0.2, 0.2),
                        "y": (-0.2, 0.2)
                    },
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    # use nearest neighbour or bilinear interpolation (fast)
                    order=[0, 1],
                    # if mode is constant, use a cval between 0 and 255
                    cval=(0, 255),
                    # use any of scikit-image's warping modes
                    # (see 2nd image from the top for examples)
                    mode='constant')),
            # execute 0 to 5 of the following (less important) augmenters per
            # image don't execute all of them, as that would often be way too
            # strong
            iaa.SomeOf(
                (0, 5),
                [
                    # convert images into their superpixel representation
                    sometimes(
                        iaa.Superpixels(p_replace=(0, 1.0),
                                        n_segments=(20, 200))),
                    iaa.OneOf([
                        # blur images with a sigma between 0 and 3.0
                        iaa.GaussianBlur((0, 3.0)),
                        # blur image using local means with kernel sizes
                        # between 2 and 7
                        iaa.AverageBlur(k=(2, 7)),
                        # blur image using local medians with kernel sizes
                        # between 2 and 7
                        iaa.MedianBlur(k=(3, 11)),
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0),
                                lightness=(0.75, 1.5)),  # sharpen images
                    iaa.Emboss(alpha=(0, 1.0),
                               strength=(0, 2.0)),  # emboss images
                    # search either for all edges or for directed edges,
                    # blend the result with the original image using a blobby mask
                    iaa.BlendAlphaSimplexNoise(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0.5, 1.0)),
                            iaa.DirectedEdgeDetect(alpha=(0.5, 1.0),
                                                   direction=(0.0, 1.0)),
                        ])),
                    # add gaussian noise to images
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                    iaa.OneOf([
                        # randomly remove up to 10% of the pixels
                        iaa.Dropout((0.01, 0.1), per_channel=0.5),
                        iaa.CoarseDropout((0.03, 0.15),
                                          size_percent=(0.02, 0.05),
                                          per_channel=0.2),
                    ]),
                    # invert color channels
                    iaa.Invert(0.05, per_channel=True),
                    # change brightness of images (by -10 to 10 of original value)
                    iaa.Add((-10, 10), per_channel=0.5),
                    # change hue and saturation
                    iaa.AddToHueAndSaturation((-20, 20)),
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.5, 1.5), per_channel=0.5),
                        iaa.BlendAlphaFrequencyNoise(
                            exponent=(-4, 0),
                            foreground=iaa.Multiply(
                                (0.5, 1.5), per_channel=True),
                            background=iaa.contrast.LinearContrast((0.5, 2.0)))
                    ]),
                    # improve or worsen the contrast
                    iaa.contrast.LinearContrast((0.5, 2.0), per_channel=0.5),
                    iaa.Grayscale(alpha=(0.0, 1.0)),
                    # move pixels locally around (with random strengths)
                    sometimes(
                        iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
                    ),
                    # sometimes move parts of the image around
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))),
                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                ],
                random_order=True)
        ],
        random_order=True)
def build_augmenters(imgaug=True):
    if imgaug:
        ia.seed(SEED)
        sometimes = lambda aug: iaa.Sometimes(0.8, aug, seed=SEED)

        augmenters = iaa.Sequential(
            [
                iaa.SomeOf(
                    (1, 3) if EXTRACT_IMAGES else (2, 4),
                    [
                        iaa.Add((-10, 10), per_channel=0.5, seed=SEED),
                        iaa.AdditiveGaussianNoise(loc=0,
                                                  scale=(0.0, 0.05 * 255),
                                                  per_channel=0.5,
                                                  seed=SEED),
                        iaa.OneOf(
                            [
                                iaa.GaussianBlur(sigma=(0, 0.5), seed=SEED),
                                iaa.AverageBlur(k=1, seed=SEED),
                                iaa.MedianBlur(k=1, seed=SEED),
                            ],
                            seed=SEED,
                        ),
                        iaa.LinearContrast(
                            (0.8, 1.2), per_channel=0.5, seed=SEED),
                    ],
                ),
                sometimes(
                    iaa.OneOf(
                        [
                            iaa.Fliplr(0.5, seed=SEED),
                            iaa.Flipud(0.2, seed=SEED),
                        ],
                        seed=SEED,
                    ), ),
                sometimes(
                    iaa.Affine(
                        scale={
                            "x": (0.5, 1) if EXTRACT_IMAGES else (0.8, 1.2),
                            "y": (0.5, 1) if EXTRACT_IMAGES else (0.8, 1.2)
                        },
                        translate_percent={
                            "x": (-0.01, 0.01) if EXTRACT_IMAGES else
                            (-0.2, 0.2),
                            "y": (-0.01, 0.01) if EXTRACT_IMAGES else
                            (-0.2, 0.2),
                        },
                        rotate=(-25, 25) if EXTRACT_IMAGES else (-45, 45),
                        shear=(-16, 16),
                        order=[0, 1],
                        cval=(0, 255),
                        mode=ia.ALL,
                        seed=SEED,
                    )),
            ],
            random_order=True,
            seed=SEED,
        )
        return augmenters
    else:
        img_augmentation = Sequential(
            [
                preprocessing.RandomRotation(factor=0.3, seed=SEED),
                preprocessing.RandomTranslation(
                    height_factor=0.01 if EXTRACT_IMAGES else 0.2,
                    width_factor=0.01 if EXTRACT_IMAGES else 0.2,
                    seed=SEED),
                preprocessing.RandomFlip(seed=SEED),
                preprocessing.RandomContrast(
                    factor=0.1 if EXTRACT_IMAGES else 0.2, seed=SEED),
            ],
            name="img_augmentation",
        )
        return img_augmentation
    def __init__(self, images, config, shuffle=True, jitter=True, norm=None):
        self.generator = None

        self.images = images
        self.config = config

        self.shuffle = shuffle
        self.jitter = jitter
        self.norm = norm

        self.anchors = [
            BoundBox(0, 0, config['ANCHORS'][2 * i],
                     config['ANCHORS'][2 * i + 1])
            for i in range(int(len(config['ANCHORS']) // 2))
        ]

        ### augmentors by https://github.com/aleju/imgaug
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)

        # Define our sequence of augmentation steps that will be applied to every image
        # All augmenters with per_channel=0.5 will sample one value _per image_
        # in 50% of all cases. In all other cases they will sample new values
        # _per channel_.
        self.aug_pipe = iaa.Sequential(
            [
                # apply the following augmenters to most images
                #iaa.Fliplr(0.5), # horizontally flip 50% of all images
                #iaa.Flipud(0.2), # vertically flip 20% of all images
                #sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
                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
                        #translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                        #rotate=(-5, 5), # rotate by -45 to +45 degrees
                        #shear=(-5, 5), # shear by -16 to +16 degrees
                        #order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                        #cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                        #mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                    )),
                # execute 0 to 5 of the following (less important) augmenters per image
                # don't execute all of them, as that would often be way too strong
                iaa.SomeOf(
                    (0, 5),
                    [
                        #sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
                        iaa.OneOf([
                            iaa.GaussianBlur(
                                (0, 3.0)
                            ),  # blur images with a sigma between 0 and 3.0
                            iaa.AverageBlur(
                                k=(2, 7)
                            ),  # blur image using local means with kernel sizes between 2 and 7
                            iaa.MedianBlur(
                                k=(3, 11)
                            ),  # blur image using local medians with kernel sizes between 2 and 7
                        ]),
                        iaa.Sharpen(alpha=(0, 1.0),
                                    lightness=(0.75, 1.5)),  # sharpen images
                        #iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                        # search either for all edges or for directed edges
                        #sometimes(iaa.OneOf([
                        #    iaa.EdgeDetect(alpha=(0, 0.7)),
                        #    iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
                        #])),
                        iaa.AdditiveGaussianNoise(
                            loc=0, scale=(0.0, 0.05 * 255),
                            per_channel=0.5),  # add gaussian noise to images
                        iaa.OneOf([
                            iaa.Dropout(
                                (0.01, 0.1), per_channel=0.5
                            ),  # randomly remove up to 10% of the pixels
                            #iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                        ]),
                        #iaa.Invert(0.05, per_channel=True), # invert color channels
                        iaa.Add(
                            (-10, 10), per_channel=0.5
                        ),  # change brightness of images (by -10 to 10 of original value)
                        iaa.Multiply(
                            (0.5, 1.5), per_channel=0.5
                        ),  # change brightness of images (50-150% of original value)
                        iaa.ContrastNormalization(
                            (0.5, 2.0),
                            per_channel=0.5),  # improve or worsen the contrast
                        #iaa.Grayscale(alpha=(0.0, 1.0)),
                        #sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                        #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around
                    ],
                    random_order=True)
            ],
            random_order=True)

        if shuffle: np.random.shuffle(self.images)
 # don't execute all of them, as that would often be way too strong
 iaa.SomeOf(
     (0, 5),
     [
         sometimes(
             iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))
         ),  # convert images into their superpixel representation
         iaa.OneOf([
             iaa.GaussianBlur(
                 (0,
                  3.0)),  # blur images with a sigma between 0 and 3.0
             iaa.AverageBlur(
                 k=(2, 7)
             ),  # blur image using local means with kernel sizes between 2 and 7
             iaa.MedianBlur(
                 k=(3, 11)
             ),  # blur image using local medians with kernel sizes between 2 and 7
         ]),
         iaa.Sharpen(alpha=(0, 1.0),
                     lightness=(0.75, 1.5)),  # sharpen images
         iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),  # emboss images
         # search either for all edges or for directed edges,
         # blend the result with the original image using a blobby mask
         iaa.SimplexNoiseAlpha(
             iaa.OneOf([
                 iaa.EdgeDetect(alpha=(0.5, 1.0)),
                 iaa.DirectedEdgeDetect(alpha=(0.5, 1.0),
                                        direction=(0.0, 1.0)),
             ])),
         iaa.AdditiveGaussianNoise(
             loc=0, scale=(0.0, 0.05 * 255),
Пример #6
0
def get_default_imgaug():

    # Возвращает набор аугментаций imgaug'а, который по нашей договоренности является дефолтным (наиболее используемым)
    # Набор аугментаций уже непосредствено можно применять к данным
    # Вызывается конструктором DataGenerator, если в конструктор DataGenerator не передан свой набор аугментаций.
    # По большей части повторяет пример из документации: https://imgaug.readthedocs.io/en/latest/source/examples_basics.html#a-simple-and-common-augmentation-sequence

    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    return iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.2),  # vertically flip 20% of all images
            sometimes(iaa.Crop(percent=(0, 0.1))),
            sometimes(
                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=(-45, 45),
                           shear=(-16, 16),
                           order=[0, 1],
                           cval=(0, 255),
                           mode=ia.ALL)),
            iaa.SomeOf(
                (0, 5),
                [
                    sometimes(
                        iaa.Superpixels(p_replace=(0, 1.0),
                                        n_segments=(20, 200))),
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)),
                        iaa.AverageBlur(k=(2, 7)),
                        iaa.MedianBlur(k=(3, 11)),
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),

                    # Same as sharpen, but for an embossing effect.
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
                    sometimes(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0, 0.7)),
                            iaa.DirectedEdgeDetect(alpha=(0, 0.7),
                                                   direction=(0.0, 1.0)),
                        ])),
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                    iaa.OneOf([
                        iaa.Dropout((0.01, 0.1), per_channel=0.5),
                        iaa.CoarseDropout((0.03, 0.15),
                                          size_percent=(0.02, 0.05),
                                          per_channel=0.2),
                    ]),
                    iaa.Invert(0.05,
                               per_channel=True),  # invert color channels

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

                    # Change brightness of images (50-150% of original value).
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),

                    # Improve or worsen the contrast of images.
                    #iaa.LinearContrast((0.5, 2.0), per_channel=0.5), ПОЧЕМУ ТО НЕ РОБИТ

                    # Convert each image to grayscale and then overlay the
                    # result with the original with random alpha. I.e. remove
                    # colors with varying strengths.
                    iaa.Grayscale(alpha=(0.0, 1.0)),

                    # In some images move pixels locally around (with random
                    # strengths).
                    sometimes(
                        iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
                    ),

                    # In some images distort local areas with varying strength.
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05)))
                ],
                # do all of the above augmentations in random order
                random_order=True)
        ],
        # do all of the above augmentations in random order
        random_order=True)
Пример #7
0
def imgaug_img(img):
    # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
    # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second
    # image.
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    # Define our sequence of augmentation steps that will be applied to every image.
    seq = iaa.Sequential(
        [
            #
            # Apply the following augmenters to most images.
            #
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            # iaa.Flipud(0.2),  # vertically flip 20% of all images

            # crop some of the images by 0-10% of their height/width
            sometimes(iaa.Crop(percent=(0, 0.1))),

            # Apply affine transformations to some of the images
            # - scale to 80-120% of image height/width (each axis independently)
            # - translate by -20 to +20 relative to height/width (per axis)
            # - rotate by -45 to +45 degrees
            # - shear by -16 to +16 degrees
            # - order: use nearest neighbour or bilinear interpolation (fast)
            # - mode: use any available mode to fill newly created pixels
            #         see API or scikit-image for which modes are available
            # - cval: if the mode is constant, then use a random brightness
            #         for the newly created pixels (e.g. sometimes black,
            #         sometimes white)
            sometimes(
                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=(-45, 45),
                           shear=(-16, 16),
                           order=[0, 1],
                           cval=(0, 255),
                           mode=ia.ALL)),

            #
            # Execute 0 to 5 of the following (less important) augmenters per
            # image. Don't execute all of them, as that would often be way too
            # strong.
            #
            iaa.SomeOf(
                (0, 5),
                [
                    # Convert some images into their superpixel representation,
                    # sample between 20 and 200 superpixels per image, but do
                    # not replace all superpixels with their average, only
                    # some of them (p_replace).
                    sometimes(
                        iaa.Superpixels(p_replace=(0, 1.0),
                                        n_segments=(20, 200))),

                    # Blur each image with varying strength using
                    # gaussian blur (sigma between 0 and 3.0),
                    # average/uniform blur (kernel size between 2x2 and 7x7)
                    # median blur (kernel size between 3x3 and 11x11).
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 3.0)),
                        iaa.AverageBlur(k=(2, 7)),
                        iaa.MedianBlur(k=(3, 11)),
                    ]),

                    # Sharpen each image, overlay the result with the original
                    # image using an alpha between 0 (no sharpening) and 1
                    # (full sharpening effect).
                    iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),

                    # Same as sharpen, but for an embossing effect.
                    iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),

                    # Search in some images either for all edges or for
                    # directed edges. These edges are then marked in a black
                    # and white image and overlayed with the original image
                    # using an alpha of 0 to 0.7.
                    sometimes(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0, 0.7)),
                            iaa.DirectedEdgeDetect(alpha=(0, 0.7),
                                                   direction=(0.0, 1.0)),
                        ])),

                    # Add gaussian noise to some images.
                    # In 50% of these cases, the noise is randomly sampled per
                    # channel and pixel.
                    # In the other 50% of all cases it is sampled once per
                    # pixel (i.e. brightness change).
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),

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

                    # Invert each image's channel with 5% probability.
                    # This sets each pixel value v to 255-v.
                    iaa.Invert(0.05,
                               per_channel=True),  # invert color channels

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

                    # Change brightness of images (50-150% of original value).
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),

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

                    # Convert each image to grayscale and then overlay the
                    # result with the original with random alpha. I.e. remove
                    # colors with varying strengths.
                    iaa.Grayscale(alpha=(0.0, 1.0)),

                    # In some images move pixels locally around (with random
                    # strengths).
                    sometimes(
                        iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
                    ),

                    # In some images distort local areas with varying strength.
                    sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05)))
                ],
                # do all of the above augmentations in random order
                random_order=True)
        ],
        # do all of the above augmentations in random order
        random_order=True)

    images_aug = seq.augment_images([img])
    return images_aug[0]
Пример #8
0
    def __init__(self, images,
                       config,
                       shuffle=True,
                       jitter=True,
                       norm=None):
        self.generator = None

        self.images = images
        self.config = config

        self.shuffle = shuffle
        self.jitter  = jitter
        self.norm    = norm
        self.image_counter = 0

        ia.seed( 1 )


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

        # Here the augmentation is defined but not executed.
        # Define our sequence of augmentation steps that will be applied to every image
        # All augmenters with per_channel=0.5 will sample one value _per image_
        # in 50% of all cases. In all other cases they will sample new values
        # _per channel_.
        self.aug_pipe = iaa.Sequential(
            [
                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
                    translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-10, 10), # rotate by -45 to +45 degrees
                    #shear=(-5, 5), # shear by -16 to +16 degrees
                    #order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    #cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    #mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                    mode = "edge"

                )),
                iaa.SomeOf((0, 5),
                    [
                        #sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
                        iaa.OneOf([
                            iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                            iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                            iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                        ]),
                        #    iaa.CoarseSalt(0.01, size_percent=(0.002, 0.01)),
                        iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                        #iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                        # search either for all edges or for directed edges
                        #sometimes(iaa.OneOf([
                        #    iaa.EdgeDetect(alpha=(0, 0.7)),
                        #    iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
                        #])),
                        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1*255), per_channel=0), # add gaussian noise to images
                        iaa.OneOf([
                            iaa.Dropout((0.01, 0.1), per_channel=0), # randomly remove up to 10% of the pixels
                            #iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                        ]),
                        #iaa.Invert(0.05, per_channel=True), # invert color channels
                        iaa.Add((-15, 15), per_channel=0), # change brightness of images (by -10 to 10 of original value)
                        iaa.Multiply((0.5, 1.5), per_channel=0), # change brightness of images (50-150% of original value)
                        iaa.ContrastNormalization((0.5, 2.0), per_channel=0), # improve or worsen the contrast
                        #iaa.Grayscale(alpha=(0.0, 1.0)),
                        #sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                        #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around
                    ])

            ],
            random_order=True
        )

        if shuffle: np.random.shuffle(self.images)
Пример #9
0
#    transforms.ToTensor(),
#    transforms.Normalize(mean=[0.485, 0.456, 0.406],
#                                 std=[0.229, 0.224, 0.225])
#])

augmenter = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.Sometimes(0.5, iaa.CropAndPad(percent=(-0.05, 0.1),
                                      pad_mode='median')),
    iaa.SomeOf((0, 5), [
        iaa.Sometimes(
            0.5, iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
        iaa.OneOf([
            iaa.GaussianBlur((0, 3.0)),
            iaa.AverageBlur(k=(2, 7)),
            iaa.MedianBlur(k=(3, 11)),
        ]),
        iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
        iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
        iaa.SimplexNoiseAlpha(
            iaa.OneOf([
                iaa.EdgeDetect(alpha=(0.5, 1.0)),
                iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
            ])),
        iaa.AdditiveGaussianNoise(
            loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
        iaa.OneOf([
            iaa.Dropout((0.01, 0.1), per_channel=0.5),
            iaa.CoarseDropout(
                (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
        ]),
Пример #10
0
def main():
    parser = argparse.ArgumentParser(description="Check augmenters visually.")
    parser.add_argument(
        '--only',
        default=None,
        help=
        "If this is set, then only the results of an augmenter with this name will be shown. Optionally, comma-separated list.",
        required=False)
    args = parser.parse_args()

    images = [
        ia.quokka_square(size=(128, 128)),
        misc.imresize(data.astronaut(), (128, 128))
    ]

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=50, y=40),
            ia.Keypoint(x=70, y=38),
            ia.Keypoint(x=62, y=52)
        ],
                            shape=images[0].shape),
        ia.KeypointsOnImage([
            ia.Keypoint(x=55, y=32),
            ia.Keypoint(x=42, y=95),
            ia.Keypoint(x=75, y=89)
        ],
                            shape=images[1].shape)
    ]

    bounding_boxes = [
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[0].shape),
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[1].shape)
    ]

    # missing: InColorspace, Lambda, AssertLambda, AssertShape, Convolve
    augmenters = [
        iaa.Sequential([
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                       name="Sequential"),
        iaa.SomeOf(2,
                   children=[
                       iaa.CoarseDropout(p=0.5, size_percent=0.05),
                       iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                       iaa.Crop(percent=0.1)
                   ],
                   name="SomeOf"),
        iaa.OneOf(children=[
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                  name="OneOf"),
        iaa.Sometimes(0.5,
                      iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                      name="Sometimes"),
        iaa.WithColorspace("HSV",
                           children=[iaa.Add(20)],
                           name="WithColorspace"),
        iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"),
        iaa.AddToHueAndSaturation((-20, 20),
                                  per_channel=True,
                                  name="AddToHueAndSaturation"),
        iaa.Noop(name="Noop"),
        iaa.Scale({
            "width": 64,
            "height": 64
        }, name="Scale"),
        iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"),
        iaa.Pad(px=(0, 8), name="Pad-px"),
        iaa.Crop(px=(0, 8), name="Crop-px"),
        iaa.Crop(percent=(0, 0.1), name="Crop-percent"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"),
        iaa.Grayscale(0.5, name="Grayscale0.5"),
        iaa.Grayscale(1.0, name="Grayscale1.0"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=10, name="BilateralBlur"),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0),
                               direction=(0, 1.0),
                               name="DirectedEdgeDetect"),
        iaa.Add((-50, 50), name="Add"),
        iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"),
        iaa.AddElementwise((-50, 50), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1 * 255),
                                  name="AdditiveGaussianNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.CoarseDropout(p=0.05,
                          size_percent=(0.05, 0.5),
                          name="CoarseDropout"),
        iaa.Invert(p=0.5, name="Invert"),
        iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"),
        iaa.Salt(p=0.05, name="Salt"),
        iaa.Pepper(p=0.05, name="Pepper"),
        iaa.CoarseSaltAndPepper(p=0.05,
                                size_percent=(0.01, 0.1),
                                name="CoarseSaltAndPepper"),
        iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"),
        iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1),
                         name="CoarsePepper"),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_px={
                       "x": (-16, 16),
                       "y": (-16, 16)
                   },
                   rotate=(-45, 45),
                   shear=(-16, 16),
                   order=ia.ALL,
                   cval=(0, 255),
                   mode=ia.ALL,
                   name="Affine"),
        iaa.PiecewiseAffine(scale=0.03,
                            nb_rows=(2, 6),
                            nb_cols=(2, 6),
                            name="PiecewiseAffine"),
        iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=False,
                  name="Alpha"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=True,
                  name="AlphaPerChannel"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Affine(rotate=(-45, 45)),
                  per_channel=True,
                  name="AlphaAffine"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=False,
                             name="AlphaElementwise"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=True,
                             name="AlphaElementwisePerChannel"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Affine(rotate=(-45, 45)),
                             per_channel=True,
                             name="AlphaElementwiseAffine"),
        iaa.SimplexNoiseAlpha(
            #first=iaa.GaussianBlur((1.0, 3.0)),
            #first=iaa.MedianBlur((3, 7)),
            first=iaa.EdgeDetect(1.0),
            #first=iaa.Affine(rotate=-45), #(-45, 45)),
            per_channel=False,
            name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(
            #first=iaa.GaussianBlur((1.0, 3.0)),
            #first=iaa.MedianBlur((3, 7)),
            first=iaa.EdgeDetect(1.0),
            #first=iaa.Affine(rotate=-45), #(-45, 45)),
            per_channel=False,
            name="FrequencyNoiseAlpha")
    ]

    augmenters.append(
        iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters],
                       name="Sequential"))
    augmenters.append(
        iaa.Sometimes(0.5, [aug.copy() for aug in augmenters],
                      name="Sometimes"))

    for augmenter in augmenters:
        if args.only is None or augmenter.name in [
                v.strip() for v in args.only.split(",")
        ]:
            print("Augmenter: %s" % (augmenter.name, ))
            grid = []
            for image, kps, bbs in zip(images, keypoints, bounding_boxes):
                aug_det = augmenter.to_deterministic()
                imgs_aug = aug_det.augment_images(
                    np.tile(image[np.newaxis, ...], (16, 1, 1, 1)))
                kps_aug = aug_det.augment_keypoints([kps] * 16)
                bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16)
                imgs_aug_drawn = [
                    kps_aug_one.draw_on_image(img_aug)
                    for img_aug, kps_aug_one in zip(imgs_aug, kps_aug)
                ]
                imgs_aug_drawn = [
                    bbs_aug_one.draw_on_image(img_aug)
                    for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug)
                ]
                grid.append(np.hstack(imgs_aug_drawn))
            misc.imshow(np.vstack(grid))
Пример #11
0
def det_aug(image, polys_np=None):
    """
    随机对图像做以下的增强操作
    :param image: cv2 read
    :param polys_np:[N, 4, 2]
    :return:
    """
    aug_sample = random.sample(cfg.TRAIN.AUG_TOOL, 1)[0]  #从数组中随机取出一个增强的功能

    ######################################################################################################
    # blur-模糊
    aug = None
    # 高斯滤波 sigma 为1-10的保留小数点后一位的float的随机值,可根据情况调整
    if aug_sample == 'GaussianBlur':
        sigma = random.uniform(1, 2)
        sigma = round(8, 10)
        aug = iaa.GaussianBlur(sigma)

    # 平均模糊 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'AverageBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.AverageBlur(k)

    # 中值滤波 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'MedianBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.MedianBlur(k)

    # 双边滤波 d=1 为 奇 数, sigma_color=(10, 250), sigma_space=(10, 250)
    if aug_sample == 'BilateralBlur':
        d = random.randint(0, 2) * 2 + 1
        sigma_color = random.randint(10, 250)
        sigma_space = random.randint(10, 250)
        aug = iaa.BilateralBlur(d, sigma_color, sigma_space)

    # 运动模糊 k=5 一定大于3 的 奇 数, angle=(0, 360), direction=(-1.0, 1.0)
    if aug_sample == 'MotionBlur':
        k = random.randint(15, 20) * 2 + 1
        angle = random.randint(0, 360)
        direction = random.uniform(-1, 1)
        direction = round(direction, 1)
        aug = iaa.MotionBlur(k, angle, direction)

    ######################################################################################################
    # geometric  几何学

    # 弹性变换
    if aug_sample == 'ElasticTransformation':
        alpha = random.uniform(10, 20)
        alpha = round(alpha, 1)
        sigma = random.uniform(5, 10)
        sigma = round(sigma, 1)
        # print(alpha, sigma)
        aug = iaa.ElasticTransformation(alpha, sigma)

    # 透视
    if aug_sample == 'PerspectiveTransform':
        scale = random.uniform(0, 0.2)
        scale = round(scale, 3)
        aug = iaa.PerspectiveTransform(scale)

    # 旋转角度
    # if aug_sample == 'Affine_rot':
    #     rotate = random.randint(-20, 20)
    #     aug = iaa.Affine(rotate=rotate)

    # 缩放
    # if aug_sample == 'Affine_scale':
    #     scale = random.uniform(0, 2)
    #     scale = round(scale, 1)
    #     aug = iaa.Affine(scale=scale)
    ######################################################################################################
    # flip 镜像

    # 水平镜像
    # if aug_sample == 'Fliplr':
    #     aug = iaa.Fliplr(1)
    #
    # 垂直镜像
    # if aug_sample == 'Flipud':
    #     aug = iaa.Flipud(1)

    ######################################################################################################
    # size 尺寸

    # if aug_sample == 'CropAndPad':
    #     top = random.randint(0, 10)
    #     right = random.randint(0, 10)
    #     bottom = random.randint(0, 10)
    #     left = random.randint(0, 10)
    #     aug = iaa.CropAndPad(px=(top, right, bottom, left))  # 上 右 下 左 各crop多少像素,然后进行padding

    if aug_sample == 'Crop':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Crop(px=(top, right, bottom, left))  # 上 右 下 左

    if aug_sample == 'Pad':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Pad(px=(top, right, bottom, left))  # 上 右 下 左

    # if aug_sample == 'PadToFixedSize':
    #     height = image.shape[0] + 32
    #     width = image.shape[1] + 100
    #     aug = iaa.PadToFixedSize(width=width, height=height)z

    # if aug_sample == 'CropToFixedSize':
    #     height = image.shape[0] - 32
    #     width = image.shape[1] - 100
    #     aug = iaa.CropToFixedSize(width=width, height=height)

    if polys_np is not None:
        if aug is not None:
            # print(aug_sample)
            h, w, _ = image.shape
            boxes_info_list = []
            for box in polys_np:
                boxes_info_list.append(Polygon(box))

            psoi = ia.PolygonsOnImage(boxes_info_list,
                                      shape=image.shape)  # 生成单个图像上所有多边形的对象
            image, psoi_aug = aug(image=image, polygons=psoi)

            pts_list = []
            for each_poly in psoi_aug.polygons:
                pts_list.append(np.array(each_poly.exterior).reshape((4, 2)))
            return image, np.array(pts_list, np.float32).reshape((-1, 4, 2))
        else:

            return image, polys_np
    else:
        image = aug(image=image)
        return image
Пример #12
0
    def __init__(self,
                 list_file,
                 train,
                 transform,
                 device,
                 little_train=False,
                 with_file_path=False,
                 C=20,
                 test_mode=False):
        print('data init')

        self.train = train
        self.transform = transform
        self.fnames = []
        self.boxes = []
        self.labels = []
        self.resize = 416
        self.C = C
        self.device = device
        self._test = test_mode
        self.with_file_path = with_file_path
        self.img_augsometimes = lambda aug: iaa.Sometimes(0.25, aug)
        self.bbox_augsometimes = lambda aug: iaa.Sometimes(0.5, aug)

        self.augmentation = iaa.Sequential(
            [
                # augment without change bboxes
                self.img_augsometimes(
                    iaa.SomeOf(
                        (1, 3),
                        [
                            iaa.Dropout([0.05, 0.2
                                         ]),  # drop 5% or 20% of all pixels
                            iaa.Sharpen((0.1, .8)),  # sharpen the image
                            # iaa.GaussianBlur(sigma=(2., 3.5)),
                            iaa.OneOf([
                                iaa.GaussianBlur(sigma=(2., 3.5)),
                                iaa.AverageBlur(k=(2, 5)),
                                iaa.BilateralBlur(d=(7, 12),
                                                  sigma_color=(10, 250),
                                                  sigma_space=(10, 250)),
                                iaa.MedianBlur(k=(3, 7)),
                            ]),
                            iaa.AddElementwise((-50, 50)),
                            iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                            iaa.JpegCompression(compression=(80, 95)),
                            iaa.Multiply((0.5, 1.5)),
                            iaa.MultiplyElementwise((0.5, 1.5)),
                            iaa.ReplaceElementwise(0.05, [0, 255]),
                            # iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                            #                 children=iaa.WithChannels(2, iaa.Add((-10, 50)))),
                            iaa.OneOf([
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       1, iaa.Add((-10, 50)))),
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       2, iaa.Add((-10, 50)))),
                            ]),
                        ],
                        random_order=True)),
                iaa.Fliplr(.5),
                iaa.Flipud(.125),
                # # augment changing bboxes
                self.bbox_augsometimes(
                    iaa.Affine(
                        # translate_px={"x": 40, "y": 60},
                        scale={
                            "x": (0.8, 1.2),
                            "y": (0.8, 1.2)
                        },
                        translate_percent={
                            "x": (-0.1, 0.1),
                            "y": (-0.1, 0.1)
                        },
                        rotate=(-5, 5),
                    ))
            ],
            random_order=True)

        # torch.manual_seed(23)
        with open(list_file) as f:
            lines = f.readlines()

        if little_train:
            lines = lines[:little_train]

        for line in lines:
            splited = line.strip().split()
            self.fnames.append(splited[0])

        self.num_samples = len(self.fnames)
def augmentation(image, mask=None):
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),
            iaa.Flipud(0.2),
            sometimes(
                iaa.CropAndPad(
                    percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))),
            sometimes(
                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=(-45, 45),
                           shear=(-16, 16),
                           order=[0, 1],
                           cval=(0, 255),
                           mode=ia.ALL)),
            # execute 0 to 5 of the following (less important) augmenters per image
            iaa.SomeOf((0, 5), [
                sometimes(
                    iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.AverageBlur(k=(2, 7)),
                    iaa.MedianBlur(k=(3, 11)),
                ]),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
                iaa.SimplexNoiseAlpha(
                    iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0.5, 1.0)),
                        iaa.DirectedEdgeDetect(alpha=(0.5, 1.0),
                                               direction=(0.0, 1.0)),
                    ])),
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                iaa.OneOf([
                    iaa.Dropout((0.01, 0.1), per_channel=0.5),
                    iaa.CoarseDropout((0.03, 0.15),
                                      size_percent=(0.02, 0.05),
                                      per_channel=0.2),
                ]),
                iaa.Invert(0.05, per_channel=True),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.AddToHueAndSaturation((-20, 20)),
                iaa.OneOf([
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),
                ]),
                iaa.Grayscale(alpha=(0.0, 1.0)),
                sometimes(
                    iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
                sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))),
                sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
            ],
                       random_order=True)
        ],
        random_order=True)
    if mask is None:
        image_heavy = seq(images=image)
        return image_heavy
    else:
        if image.ndim == 4:
            mask = np.array(mask)
            image_heavy, mask_heavy = seq(images=image,
                                          segmentation_maps=mask.astype(
                                              np.int32))
        else:
            image_heavy, mask_heavy = seq(images=image[np.newaxis, ...],
                                          segmentation_maps=mask[np.newaxis,
                                                                 ...])
            image_heavy, mask_heavy = image_heavy[0], mask_heavy[0]
        return image_heavy, mask_heavy
Пример #14
0
def main():
    '''
	Dirty image augmentation to get better recognition for systems presentation.

    '''
    augs = [
        "superpixel", "colorspace", "grayscale", "gaussian_blur",
        "average_blur", "median_blur", "edge_detect", "add", "add_eltwise",
        "invert", "contrast_norm", "dropout"
    ]

    superpixel = iaa.Superpixels(p_replace=(0.4, 0.6), n_segments=(16, 64))
    #colorspace    = iaa.Sequential([iaa.ChangeColorspace(from_colorspace="BGR",  to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add(-50, 50), iaa.ChangeColorspace(from_colorspace="BGR", to_colorspace="BGR")])
    grayscale = iaa.Grayscale(alpha=(0.0, 1.0))
    gaussian_blur = iaa.GaussianBlur(sigma=(0.0, 3.0))
    average_blur = iaa.AverageBlur(k=(2, 10))
    median_blur = iaa.MedianBlur(k=(5, 11))
    edge_detect = iaa.EdgeDetect(alpha=(0.0, 1.0))
    add = iaa.Add((-50, 50), per_channel=0.5)
    add_eltwise = iaa.AddElementwise((-50, 50), per_channel=0.5)
    invert = iaa.Invert(0.25, per_channel=0.5)
    contrast_norm = iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5)
    dropout = iaa.Dropout(p=(0, 0.3), per_channel=0.5)

    image_paths = get_files("JPG")
    cv_images = get_images(image_paths)

    for augmentation in augs:
        if augmentation == "superpixel":
            aug_images = superpixel.augment_images(cv_images)
            save_augmented_images("superpixel", aug_images, image_paths)

        #elif augmentation == "colorspace":
        #   aug_images = colorspace.augment_images(cv_images)
        #    save_augmented_images("colorspace", aug_images, image_paths)

        elif augmentation == "grayscale":
            aug_images = grayscale.augment_images(cv_images)
            save_augmented_images("grayscale", aug_images, image_paths)

        elif augmentation == "gaussian_blur":
            aug_images = gaussian_blur.augment_images(cv_images)
            save_augmented_images("gaussian_blur", aug_images, image_paths)

        elif augmentation == "average_blur":
            aug_images = average_blur.augment_images(cv_images)
            save_augmented_images("average_blur", aug_images, image_paths)

        elif augmentation == "edge_detect":
            aug_images = edge_detect.augment_images(cv_images)
            save_augmented_images("edge_detect", aug_images, image_paths)

        elif augmentation == "add":
            aug_images = add.augment_images(cv_images)
            save_augmented_images("add", aug_images, image_paths)

        elif augmentation == "add_eltwise":
            aug_images = add_eltwise.augment_images(cv_images)
            save_augmented_images("add_eltwise", aug_images, image_paths)

        elif augmentation == "invert":
            aug_images = invert.augment_images(cv_images)
            save_augmented_images("invert", aug_images, image_paths)

        elif augmentation == "contrast_norm":
            aug_images = contrast_norm.augment_images(cv_images)
            save_augmented_images("contrast_norm", aug_images, image_paths)

        elif augmentation == "dropout":
            aug_images = dropout.augment_images(cv_images)
            save_augmented_images("dropout", aug_images, image_paths)
Пример #15
0
         # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
     )),
 # execute 0 to 5 of the following (less important) augmenters per image
 # don't execute all of them, as that would often be way too strong
 iaa.SomeOf(
     (0, 5),
     [
         # sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
         iaa.OneOf([
             iaa.GaussianBlur(
                 (0,
                  0.5)),  # blur images with a sigma between 0 and 3.0
             iaa.AverageBlur(
                 k=(2, 3)
             ),  # blur image using local means with kernel sizes between 2 and 3
             iaa.MedianBlur(k=(3, 5)),
             # blur image using local medians with kernel sizes between 2 and 7
         ]),
         iaa.Sharpen(alpha=(0, 1.0),
                     lightness=(0.75, 1.5)),  # sharpen images
         # iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
         # search either for all edges or for directed edges
         # sometimes(iaa.OneOf([
         #    iaa.EdgeDetect(alpha=(0, 0.7)),
         #    iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
         # ])),
         iaa.AdditiveGaussianNoise(
             loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
         # add gaussian noise to images
         iaa.OneOf([
             iaa.Dropout((0.01, 0.1), per_channel=0.5
Пример #16
0
 def __init__(self, image):
     self.img = image
     # 随机通道处理,加减100以内
     # self.aug_WithChannels = iaa.WithChannels((0,2), iaa.Add((-100, 100)))
     # 随机裁剪和填充,percent为裁剪与填充比例,负数为放大后裁剪,正数为缩小和填充,pad_mode为填充方式,pad_cval为当空白填充时,填充像素值
     self.aug_CropAndPad = iaa.CropAndPad(percent=(-0.05, 0.1),
                                          pad_mode=ia.ALL,
                                          pad_cval=(0, 255))
     # 随机水平翻转,参数为概率
     self.aug_Fliplr = iaa.Fliplr(0.5)
     # 随机垂直翻转,参数为概率
     self.aug_Flipud = iaa.Flipud(0.5)
     # 超像素表示,p_replace被超像素代替的百分比,n_segments分割块数
     self.aug_Superpixels = iaa.Superpixels(p_replace=(0, 1.0),
                                            n_segments=(20, 200))
     # 灰度化 (0.0,1.0),前者为偏彩色部分,后者为偏灰度部分,随机灰度化
     self.aug_GrayScale = iaa.Grayscale(alpha=(0.0, 0.6))
     # 高斯模糊
     self.aug_GaussianBlur = iaa.GaussianBlur(sigma=(0, 3.0))
     # 均值模糊,k为kernel size
     self.aug_AverageBlur = iaa.AverageBlur(k=(2, 7))
     # 中值模糊, k为kernel size
     self.aug_MedianBlur = iaa.MedianBlur(k=(3, 11))
     # 双边滤波,d为kernel size,sigma_color为颜色域标准差,sigma_space为空间域标准差
     self.aug_BilateralBlur = iaa.BilateralBlur(sigma_color=(0, 250),
                                                sigma_space=(0, 250),
                                                d=(3, 7))
     # 锐化
     self.aug_Sharpen = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))
     # 浮雕效果
     self.aug_Emboss = iaa.Emboss(alpha=(0.0, 1.0), strength=(0.0, 1.5))
     # 边缘检测
     self.aug_EdgeDetect = iaa.EdgeDetect(alpha=(0.0, 1.0))
     # 方向性边缘检测
     self.aug_DirectedEdgeDetece = iaa.DirectedEdgeDetect(alpha=(0.0, 1.0),
                                                          direction=(0.0,
                                                                     1.0))
     # 暴力叠加像素值,每个像素统一加一个值
     self.aug_Add = iaa.Add((-40, 40))
     # 暴力叠加像素值,每个像素加不同的值
     self.aug_AddElementwise = iaa.AddElementwise((-40, 40))
     # 随机高斯加性噪声
     self.aug_AdditiveGaussianNoise = iaa.AdditiveGaussianNoise(scale=(0.0,
                                                                       0.1 *
                                                                       255))
     # 暴力乘法,每个像素统一乘以一个值
     self.aug_Multiply = iaa.Multiply((0.8, 1.2))
     # 暴力乘法,每个像素乘以不同值
     self.aug_MultiplyElementwise = iaa.MultiplyElementwise((0.8, 1.2))
     # 随机dropout像素值
     self.aug_Dropout = iaa.Dropout(p=(0, 0.2))
     # 随机粗dropout,2*2方块像素被dropout
     self.aug_CoarseDropout = iaa.CoarseDropout(0.02, size_percent=0.5)
     # 50%的图片,p概率反转颜色
     self.aug_Invert = iaa.Invert(0.25, per_channel=0.5)
     # 对比度归一化
     self.aug_ContrastNormalization = iaa.ContrastNormalization((0.5, 1.5))
     # 仿射变换
     self.aug_Affine = iaa.Affine(rotate=(0, 20),
                                  scale={
                                      "x": (0.8, 1.2),
                                      "y": (0.8, 1.2)
                                  })
     # 仿射变换, 局部像素仿射扭曲
     self.aug_PiecewiseAffine = iaa.PiecewiseAffine(scale=(0.01, 0.05))
     # 单应性变换
     self.aug_PerspectiveTransform = iaa.PerspectiveTransform(scale=(0.01,
                                                                     0.1))
     # 弹性变换
     self.aug_ElasticTransformation = iaa.ElasticTransformation(alpha=(0,
                                                                       5.0),
                                                                sigma=0.25)
     # 简单的加噪,小黑块
     self.aug_SimplexNoiseAlpha = iaa.SimplexNoiseAlpha(
         iaa.OneOf([
             iaa.EdgeDetect(alpha=(0.0, 0.5)),
             iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 1.0)),
         ]))
     # 频域加噪,表现为色彩的块状变换
     self.aug_FrequencyNoiseAlpha = iaa.FrequencyNoiseAlpha(
         exponent=(-4, 0),
         first=iaa.Multiply((0.5, 1.5), per_channel=True),
         second=iaa.ContrastNormalization((0.5, 2.0)))
Пример #17
0
def test_MedianBlur():
    reseed()

    base_img = np.zeros((11, 11, 1), dtype=np.uint8)
    base_img[3:8, 3:8, 0] = 1
    base_img[4:7, 4:7, 0] = 2
    base_img[5:6, 5:6, 0] = 3

    blur3x3 = np.zeros_like(base_img)
    blur3x3[3:8, 3:8, 0] = 1
    blur3x3[4:7, 4:7, 0] = 2
    blur3x3[4, 4, 0] = 1
    blur3x3[4, 6, 0] = 1
    blur3x3[6, 4, 0] = 1
    blur3x3[6, 6, 0] = 1
    blur3x3[3, 3, 0] = 0
    blur3x3[3, 7, 0] = 0
    blur3x3[7, 3, 0] = 0
    blur3x3[7, 7, 0] = 0

    blur5x5 = np.copy(blur3x3)
    blur5x5[4, 3, 0] = 0
    blur5x5[3, 4, 0] = 0
    blur5x5[6, 3, 0] = 0
    blur5x5[7, 4, 0] = 0
    blur5x5[4, 7, 0] = 0
    blur5x5[3, 6, 0] = 0
    blur5x5[6, 7, 0] = 0
    blur5x5[7, 6, 0] = 0
    blur5x5[blur5x5 > 1] = 1

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=0, y=0, vis=None, label=None),
            ia.Keypoint(x=1, y=1, vis=None, label=None),
            ia.Keypoint(x=2, y=2, vis=None, label=None)
        ],
                            shape=base_img.shape)
    ]

    # no blur, shouldnt change anything
    aug = iaa.MedianBlur(k=1)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, base_img)

    # k=3
    aug = iaa.MedianBlur(k=3)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, blur3x3)

    # k=5
    aug = iaa.MedianBlur(k=5)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, blur5x5)

    # k as (3, 5)
    aug = iaa.MedianBlur(k=(3, 5))
    seen = [False, False]
    for i in sm.xrange(100):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, blur3x3):
            seen[0] = True
        elif np.array_equal(observed, blur5x5):
            seen[1] = True
        else:
            raise Exception("Unexpected result in MedianBlur@1")
        if all(seen):
            break
    assert all(seen)

    # k as stochastic parameter
    aug = iaa.MedianBlur(k=iap.Choice([3, 5]))
    seen = [False, False]
    for i in sm.xrange(100):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, blur3x3):
            seen[0] += True
        elif np.array_equal(observed, blur5x5):
            seen[1] += True
        else:
            raise Exception("Unexpected result in MedianBlur@2")
        if all(seen):
            break
    assert all(seen)

    # keypoints shouldnt be changed
    aug = iaa.MedianBlur(k=3)
    aug_det = aug.to_deterministic()
    observed = aug.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)

    observed = aug_det.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)
Пример #18
0
import numpy as np
import tensorflow as tf
from imgaug import augmenters as iaa

sometimes_010 = lambda aug: iaa.Sometimes(0.10, aug)

blurs = iaa.Sequential([
    sometimes_010(
        iaa.OneOf([
            iaa.GaussianBlur(sigma=(0, 1.5)),
            iaa.AverageBlur(k=(1, 5)),
            iaa.MedianBlur(k=(1, 5)),
            iaa.MotionBlur(k=(3, 5)),
        ]))
])

contrasts = iaa.Sequential([
    sometimes_010(
        iaa.OneOf([
            iaa.LogContrast((0.8, 1.2)),
            iaa.GammaContrast((0.8, 1.2)),
            iaa.LinearContrast((0.8, 1.2)),
            iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()),
            iaa.Alpha((0.0, 1.0), iaa.HistogramEqualization()),
            iaa.CLAHE(clip_limit=(1, 3)),
            iaa.AllChannelsCLAHE(clip_limit=(1, 3)),
        ]))
])

dropouts = iaa.Sequential([
    sometimes_010(
Пример #19
0
def get_seq():
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.2),  # vertically flip 20% of all images
            sometimes(
                iaa.Affine(
                    scale={
                        "x": (0.9, 1.1),
                        "y": (0.9, 1.1)
                    },  # scale images to 80-120% of their size, individually per axis
                    translate_percent={
                        "x": (-0.1, 0.1),
                        "y": (-0.1, 0.1)
                    },  # translate by -20 to +20 percent (per axis)
                    rotate=(-10, 10),  # rotate by -45 to +45 degrees
                    shear=(-5, 5),  # shear by -16 to +16 degrees
                    order=[
                        0, 1
                    ],  # use nearest neighbour or bilinear interpolation (fast)
                    cval=(
                        0, 255
                    ),  # if mode is constant, use a cval between 0 and 255
                    mode=ia.
                    ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf(
                (0, 5),
                [
                    sometimes(
                        iaa.Superpixels(p_replace=(0, 1.0),
                                        n_segments=(20, 200))
                    ),  # convert images into their superpixel representation
                    iaa.OneOf([
                        iaa.GaussianBlur(
                            (0, 1.0
                             )),  # blur images with a sigma between 0 and 3.0
                        iaa.AverageBlur(
                            k=(3, 5)
                        ),  # blur image using local means with kernel sizes between 2 and 7
                        iaa.MedianBlur(
                            k=(3, 5)
                        ),  # blur image using local medians with kernel sizes between 2 and 7
                    ]),
                    iaa.Sharpen(alpha=(0, 1.0),
                                lightness=(0.9, 1.1)),  # sharpen images
                    iaa.Emboss(alpha=(0, 1.0),
                               strength=(0, 2.0)),  # emboss images
                    # search either for all edges or for directed edges,
                    # blend the result with the original image using a blobby mask
                    iaa.SimplexNoiseAlpha(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0.5, 1.0)),
                            iaa.DirectedEdgeDetect(alpha=(0.5, 1.0),
                                                   direction=(0.0, 1.0)),
                        ])),
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.01 * 255),
                        per_channel=0.5),  # add gaussian noise to images
                    iaa.OneOf([
                        iaa.Dropout(
                            (0.01, 0.05), per_channel=0.5
                        ),  # randomly remove up to 10% of the pixels
                        iaa.CoarseDropout((0.01, 0.03),
                                          size_percent=(0.01, 0.02),
                                          per_channel=0.2),
                    ]),
                    iaa.Invert(0.01,
                               per_channel=True),  # invert color channels
                    iaa.Add(
                        (-2, 2), per_channel=0.5
                    ),  # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation(
                        (-1, 1)),  # change hue and saturation
                    # either change the brightness of the whole image (sometimes
                    # per channel) or change the brightness of subareas
                    iaa.OneOf([
                        iaa.Multiply((0.9, 1.1), per_channel=0.5),
                        iaa.FrequencyNoiseAlpha(
                            exponent=(-1, 0),
                            first=iaa.Multiply((0.9, 1.1), per_channel=True),
                            second=iaa.ContrastNormalization((0.9, 1.1)))
                    ]),
                    sometimes(
                        iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
                    ),  # move pixels locally around (with random strengths)
                    sometimes(iaa.PiecewiseAffine(scale=(
                        0.01,
                        0.05))),  # sometimes move parts of the image around
                    sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                ],
                random_order=True)
        ],
        random_order=True)
    return seq
Пример #20
0
def test_keypoint_augmentation():
    reseed()

    keypoints = []
    for y in range(40 // 5):
        for x in range(60 // 5):
            keypoints.append(ia.Keypoint(y=y * 5, x=x * 5))

    keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3))
    keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3))

    augs = [
        iaa.Add((-5, 5), name="Add"),
        iaa.AddElementwise((-5, 5), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"),
        iaa.Multiply((0.95, 1.05), name="Multiply"),
        iaa.Dropout(0.01, name="Dropout"),
        iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
        iaa.Invert(0.01, per_channel=True, name="Invert"),
        iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"),
        iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
        iaa.AverageBlur((3, 5), name="AverageBlur"),
        iaa.MedianBlur((3, 5), name="MedianBlur"),
        # iaa.BilateralBlur((3, 5), name="BilateralBlur"),
        # WithColorspace ?
        # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"),
        # ChangeColorspace ?
        # Grayscale cannot be tested, input not RGB
        # Convolve ?
        iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
        iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.0, 0.1),
                               direction=0,
                               name="DirectedEdgeDetect"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
        iaa.Affine(translate_percent=(-0.05, 0.05),
                   name="Affine-translate-percent"),
        iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
        iaa.Affine(shear=(-20, 20), name="Affine-shear"),
        iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
        iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
        # iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.1, 0.2),
                                  sigma=(0.1, 0.2),
                                  name="ElasticTransformation"),
        # Sequential
        # SomeOf
        # OneOf
        # Sometimes
        # WithChannels
        # Noop
        # Lambda
        # AssertLambda
        # AssertShape
        iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"),
        iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                first=iaa.Add(10),
                                name="SimplexNoiseAlpha"),
        iaa.Superpixels(p_replace=0.01, n_segments=64),
        iaa.Resize(0.5, name="Resize"),
        iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
        iaa.Pad(px=(0, 10), name="Pad"),
        iaa.Crop(px=(0, 10), name="Crop")
    ]

    for aug in augs:
        dss = []
        for i in range(10):
            aug_det = aug.to_deterministic()

            kp_fully_empty_aug = aug_det.augment_keypoints([])
            assert kp_fully_empty_aug == []

            kp_first_empty_aug = aug_det.augment_keypoints(
                [keypoints_oi_empty])[0]
            assert len(kp_first_empty_aug.keypoints) == 0

            kp_image = keypoints_oi.to_keypoint_image(size=5)
            kp_image_aug = aug_det.augment_image(kp_image)
            kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image(
                kp_image_aug,
                if_not_found_coords={
                    "x": -9999,
                    "y": -9999
                },
                nb_channels=1)
            kp_aug = aug_det.augment_keypoints([keypoints_oi])[0]
            ds = []
            assert len(kp_image_aug_rev.keypoints) == len(kp_aug.keypoints),\
                "Lost keypoints for '%s' (%d vs expected %d)" \
                % (aug.name, len(kp_aug.keypoints), len(kp_image_aug_rev.keypoints))
            for kp_pred, kp_pred_img in zip(kp_aug.keypoints,
                                            kp_image_aug_rev.keypoints):
                kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999)
                kp_pred_img_lost = (kp_pred_img.x == -9999
                                    and kp_pred_img.y == -9999)

                if not kp_pred_lost and not kp_pred_img_lost:
                    d = np.sqrt((kp_pred.x - kp_pred_img.x)**2 +
                                (kp_pred.y - kp_pred_img.y)**2)
                    ds.append(d)
            dss.extend(ds)
            if len(ds) == 0:
                print("[INFO] No valid keypoints found for '%s' "
                      "in test_keypoint_augmentation()" % (str(aug), ))
        assert np.average(dss) < 5.0, \
            "Average distance too high (%.2f, with ds: %s)" \
            % (np.average(dss), str(dss))
Пример #21
0
#SAVE AUGMENTED IMAGES WITH BOUNDING BOX TO FILEPATH#
filepathSaveFolder = script_dir + "/augmented/"

filepath_img = filepathSaveFolder + batchName + "%d.jpg"
filepath_txt = filepathSaveFolder + batchName + "%d.txt"

#DESIRED AUGMENTATION#
seq = iaa.Sequential(
    [
        #iaa.AddToHue((-255,255)),  # change their color
        #iaa.MultiplySaturation((0.1,0.7)), #calm down color
        #iaa.ElasticTransformation(alpha=20, sigma=4),  # water-like effect (smaller sigma = smaller "waves")
        #iaa.PiecewiseAffine(scale=(0.01,0.05)), #sometimes moves pieces of image around (RAM-heavy)
        iaa.LogContrast((0.5, 1.0), True),  #overlay color
        #iaa.MotionBlur(20,(0,288),1,0), #motion blur for realism
        iaa.Alpha((0.0, 1.0), iaa.MedianBlur(11),
                  per_channel=True),  #alpha-blending with median blur
        iaa.PerspectiveTransform(scale=(0.1, 0.1)),
        iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=True),  #noise
        iaa.CoarseDropout(p=0.1,
                          size_percent=0.005),  #blocks removed from image
        iaa.Affine(rotate=(
            -15,
            15))  #rotate #PROBLEM WITH BOUNDING BOXES MOSTLY CAUSED BY THIS
    ],
    random_order=True)

#---------------------------------------------------------------------

imagesToAugment = []
bbs_images = []
Пример #22
0
def test_unusual_channel_numbers():
    reseed()

    images = [(0, create_random_images((4, 16, 16))),
              (1, create_random_images((4, 16, 16, 1))),
              (2, create_random_images((4, 16, 16, 2))),
              (4, create_random_images((4, 16, 16, 4))),
              (5, create_random_images((4, 16, 16, 5))),
              (10, create_random_images((4, 16, 16, 10))),
              (20, create_random_images((4, 16, 16, 20)))]

    augs = [
        iaa.Add((-5, 5), name="Add"),
        iaa.AddElementwise((-5, 5), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"),
        iaa.Multiply((0.95, 1.05), name="Multiply"),
        iaa.Dropout(0.01, name="Dropout"),
        iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
        iaa.Invert(0.01, per_channel=True, name="Invert"),
        iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"),
        iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
        iaa.AverageBlur((3, 5), name="AverageBlur"),
        iaa.MedianBlur((3, 5), name="MedianBlur"),
        # iaa.BilateralBlur((3, 5), name="BilateralBlur"), # works only with 3/RGB channels
        # WithColorspace ?
        # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with 3/RGB channels
        # ChangeColorspace ?
        # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with 3 channels
        # Convolve ?
        iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
        iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.0, 0.1),
                               direction=0,
                               name="DirectedEdgeDetect"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
        iaa.Affine(translate_percent=(-0.05, 0.05),
                   name="Affine-translate-percent"),
        iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
        iaa.Affine(shear=(-20, 20), name="Affine-shear"),
        iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
        iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
        iaa.PerspectiveTransform(scale=(0.01, 0.10),
                                 name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.1, 0.2),
                                  sigma=(0.1, 0.2),
                                  name="ElasticTransformation"),
        iaa.Sequential([iaa.Add((-5, 5)),
                        iaa.AddElementwise((-5, 5))]),
        iaa.SomeOf(1, [iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))]),
        iaa.OneOf([iaa.Add((-5, 5)),
                   iaa.AddElementwise((-5, 5))]),
        iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"),
        # WithChannels
        iaa.Noop(name="Noop"),
        # Lambda
        # AssertLambda
        # AssertShape
        iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"),
        iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                first=iaa.Add(10),
                                name="SimplexNoiseAlpha"),
        iaa.Superpixels(p_replace=0.01, n_segments=64),
        iaa.Resize({
            "height": 4,
            "width": 4
        }, name="Resize"),
        iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
        iaa.Pad(px=(0, 10), name="Pad"),
        iaa.Crop(px=(0, 10), name="Crop")
    ]

    for aug in augs:
        for (nb_channels, images_c) in images:
            if aug.name != "Resize":
                images_aug = aug.augment_images(images_c)
                assert images_aug.shape == images_c.shape
                image_aug = aug.augment_image(images_c[0])
                assert image_aug.shape == images_c[0].shape
            else:
                images_aug = aug.augment_images(images_c)
                image_aug = aug.augment_image(images_c[0])
                if images_c.ndim == 3:
                    assert images_aug.shape == (4, 4, 4)
                    assert image_aug.shape == (4, 4)
                else:
                    assert images_aug.shape == (4, 4, 4, images_c.shape[3])
                    assert image_aug.shape == (4, 4, images_c.shape[3])
Пример #23
0
    def __call__(self, sample):
        image, pose = sample['image'], sample['pose'].reshape([-1, 2])

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

        seq = iaa.Sequential(
            [
                # Apply the following augmenters to most images.
                sometimes(
                    iaa.CropAndPad(percent=(-0.25, 0.25),
                                   pad_mode=["edge"],
                                   keep_size=False)),
                sometimes(
                    iaa.Affine(scale={
                        "x": (0.75, 1.25),
                        "y": (0.75, 1.25)
                    },
                               translate_percent={
                                   "x": (-0.25, 0.25),
                                   "y": (-0.25, 0.25)
                               },
                               rotate=(-45, 45),
                               shear=(-5, 5),
                               order=[0, 1],
                               cval=(0, 255),
                               mode=ia.ALL)),
                iaa.SomeOf(
                    (0, 3),
                    [
                        iaa.OneOf([
                            iaa.GaussianBlur((0, 3.0)),
                            # iaa.AverageBlur(k=(2, 7)),
                            iaa.MedianBlur(k=(3, 11)),
                            iaa.MotionBlur(k=5, angle=[-45, 45])
                        ]),
                        iaa.OneOf([
                            iaa.AdditiveGaussianNoise(loc=0,
                                                      scale=(0.0, 0.05 * 255),
                                                      per_channel=0.5),
                            iaa.AdditivePoissonNoise(lam=(0, 8),
                                                     per_channel=True),
                        ]),
                        iaa.OneOf([
                            iaa.Add((-10, 10), per_channel=0.5),
                            iaa.Multiply((0.2, 1.2), per_channel=0.5),
                            iaa.ContrastNormalization(
                                (0.5, 2.0), per_channel=0.5),
                        ]),
                    ],
                    # do all of the above augmentations in random order
                    random_order=True)
            ],
            # do all of the above augmentations in random order
            random_order=True)

        # augmentation choices
        seq_det = seq.to_deterministic()

        image_aug = seq_det.augment_images([image])[0]
        keypoints_aug = seq_det.augment_keypoints(
            [self.pose2keypoints(image, pose)])[0]

        return {'image': image_aug, 'pose': self.keypoints2pose(keypoints_aug)}
Пример #24
0
def test_determinism():
    reseed()

    images = [
        ia.quokka(size=(128, 128)),
        ia.quokka(size=(64, 64)),
        ia.imresize_single_image(skimage.data.astronaut(), (128, 256))
    ]
    images.extend([ia.quokka(size=(16, 16))] * 20)

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=20, y=10),
            ia.Keypoint(x=5, y=5),
            ia.Keypoint(x=10, y=43)
        ],
                            shape=(50, 60, 3))
    ] * 20

    augs = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))),
        # iaa.WithChannels([0], iaa.Add((-50, 50))),
        # iaa.Noop(name="Noop-nochange"),
        # iaa.Lambda(
        #     func_images=lambda images, random_state, parents, hooks: images,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images,
        #     name="Lambda-nochange"
        # ),
        # iaa.AssertLambda(
        #     func_images=lambda images, random_state, parents, hooks: True,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True,
        #     name="AssertLambda-nochange"
        # ),
        # iaa.AssertShape(
        #     (None, None, None, 3),
        #     check_keypoints=False,
        #     name="AssertShape-nochange"
        # ),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)),
        # iaa.ChangeColorspace(to_colorspace="GRAY"),
        iaa.Grayscale(alpha=(0.1, 1.0)),
        iaa.GaussianBlur((0.1, 3.0)),
        iaa.AverageBlur((3, 11)),
        iaa.MedianBlur((3, 11)),
        # iaa.Convolve(np.array([[0, 1, 0],
        #                       [1, -4, 1],
        #                       [0, 1, 0]])),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)),
        iaa.EdgeDetect(alpha=(0.1, 1.0)),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)),
        iaa.Add((-50, 50)),
        iaa.AddElementwise((-50, 50)),
        iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)),
        iaa.Multiply((0.6, 1.4)),
        iaa.MultiplyElementwise((0.6, 1.4)),
        iaa.Dropout((0.3, 0.5)),
        iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)),
        iaa.Invert(0.5),
        iaa.ContrastNormalization((0.6, 1.4)),
        iaa.Affine(scale=(0.7, 1.3),
                   translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20),
                   shear=(-20, 20),
                   order=ia.ALL,
                   mode=ia.ALL,
                   cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=0.5)
    ]

    augs_affect_geometry = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(scale=(0.7, 1.3),
                   translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20),
                   shear=(-20, 20),
                   order=ia.ALL,
                   mode=ia.ALL,
                   cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5))
    ]

    for aug in augs:
        aug_det = aug.to_deterministic()
        images_aug1 = aug_det.augment_images(images)
        images_aug2 = aug_det.augment_images(images)

        aug_det = aug.to_deterministic()
        images_aug3 = aug_det.augment_images(images)
        images_aug4 = aug_det.augment_images(images)

        assert array_equal_lists(images_aug1, images_aug2), \
            "Images (1, 2) expected to be identical for %s" % (aug.name,)

        assert array_equal_lists(images_aug3, images_aug4), \
            "Images (3, 4) expected to be identical for %s" % (aug.name,)

        assert not array_equal_lists(images_aug1, images_aug3), \
            "Images (1, 3) expected to be different for %s" % (aug.name,)

    for aug in augs_affect_geometry:
        aug_det = aug.to_deterministic()
        kps_aug1 = aug_det.augment_keypoints(keypoints)
        kps_aug2 = aug_det.augment_keypoints(keypoints)

        aug_det = aug.to_deterministic()
        kps_aug3 = aug_det.augment_keypoints(keypoints)
        kps_aug4 = aug_det.augment_keypoints(keypoints)

        assert keypoints_equal(kps_aug1, kps_aug2), \
            "Keypoints (1, 2) expected to be identical for %s" % (aug.name,)

        assert keypoints_equal(kps_aug3, kps_aug4), \
            "Keypoints (3, 4) expected to be identical for %s" % (aug.name,)

        assert not keypoints_equal(kps_aug1, kps_aug3), \
            "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
Пример #25
0
    'addLNp': iaa.AdditiveLaplaceNoise(scale=0.10 * 255, per_channel=True),
    'addPN': iaa.AdditivePoissonNoise(lam=16.00),
    'addPNp': iaa.AdditivePoissonNoise(lam=16.00, per_channel=True),
    'mul-': iaa.Multiply(mul=0.50),
    'mul+': iaa.Multiply(mul=1.50),
    'mulp-': iaa.Multiply(mul=0.50, per_channel=True),
    'mulp+': iaa.Multiply(mul=1.50, per_channel=True),
    'jpeg': iaa.JpegCompression(compression=62),
    'jpeg+': iaa.JpegCompression(compression=75),
    'jpeg++': iaa.JpegCompression(compression=87)
}

blur = {
    'GBlur': iaa.GaussianBlur(sigma=1.00),
    'ABlur': iaa.AverageBlur(k=3),
    'MBlur': iaa.MedianBlur(k=3),
    'BBlur': iaa.BilateralBlur(sigma_color=250, sigma_space=250, d=5),
    'MoBlur': iaa.MotionBlur(angle=0, k=7),
    'MoBlurAng': iaa.MotionBlur(angle=144, k=5)
}

color = {
    'ATHAS-': iaa.AddToHueAndSaturation(value=-45),
    'ATHAS+': iaa.AddToHueAndSaturation(value=45),
    'Gray': iaa.Grayscale(alpha=0.2)
}

contrast = {
    'GContrast-': iaa.GammaContrast(gamma=0.81),
    'GContrast+': iaa.GammaContrast(gamma=1.44),
    'SContrast': iaa.SigmoidContrast(cutoff=0.5, gain=10),
Пример #26
0
def test_dtype_preservation():
    reseed()

    size = (4, 16, 16, 3)
    images = [
        np.random.uniform(0, 255, size).astype(np.uint8),
        np.random.uniform(0, 65535, size).astype(np.uint16),
        np.random.uniform(0, 4294967295, size).astype(np.uint32),
        np.random.uniform(-128, 127, size).astype(np.int16),
        np.random.uniform(-32768, 32767, size).astype(np.int32),
        np.random.uniform(0.0, 1.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float16),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float64)
    ]

    default_dtypes = set([arr.dtype for arr in images])

    # Some dtypes are here removed per augmenter, because the respective
    # augmenter does not support them. This test currently only checks whether
    # dtypes are preserved from in- to output for all dtypes that are supported
    # per augmenter.
    # dtypes are here removed via list comprehension instead of
    # `default_dtypes - set([dtype])`, because the latter one simply never
    # removed the dtype(s) for some reason?!

    def _not_dts(dts):
        return [dt for dt in default_dtypes if dt not in dts]

    augs = [
        (iaa.Add((-5, 5),
                 name="Add"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AddElementwise((-5, 5), name="AddElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Multiply((0.95, 1.05), name="Multiply"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Dropout(0.01, name="Dropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes),
        (iaa.ContrastNormalization(
            (0.95, 1.05), name="ContrastNormalization"), default_dtypes),
        (iaa.GaussianBlur(sigma=(0.95, 1.05),
                          name="GaussianBlur"), _not_dts([np.float16])),
        (iaa.AverageBlur((3, 5), name="AverageBlur"),
         _not_dts([np.uint32, np.int32, np.float16])),
        (iaa.MedianBlur((3, 5), name="MedianBlur"),
         _not_dts([np.uint32, np.int32, np.float16, np.float64])),
        (iaa.BilateralBlur((3, 5), name="BilateralBlur"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64
         ])),
        # WithColorspace ?
        # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with RGB/uint8
        # ChangeColorspace ?
        # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with RGB/uint8
        # Convolve ?
        (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1),
                                direction=0,
                                name="DirectedEdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes),
        (iaa.Flipud(0.5, name="Flipud"), default_dtypes),
        (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(translate_percent=(-0.05, 0.05),
                    name="Affine-translate-percent"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(rotate=(-20, 20),
                    name="Affine-rotate"), _not_dts([np.uint32, np.int32])),
        (iaa.Affine(shear=(-20, 20),
                    name="Affine-shear"), _not_dts([np.uint32, np.int32])),
        (iaa.Affine(scale=(0.9, 1.1),
                    name="Affine-scale"), _not_dts([np.uint32, np.int32])),
        (iaa.PiecewiseAffine(scale=(0.001, 0.005),
                             name="PiecewiseAffine"), default_dtypes),
        # (iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), not_dts([np.uint32])),
        (iaa.ElasticTransformation(alpha=(0.1, 0.2),
                                   sigma=(0.1, 0.2),
                                   name="ElasticTransformation"),
         _not_dts([np.float16])),
        (iaa.Sequential([iaa.Noop(), iaa.Noop()],
                        name="SequentialNoop"), default_dtypes),
        (iaa.SomeOf(1, [iaa.Noop(), iaa.Noop()],
                    name="SomeOfNoop"), default_dtypes),
        (iaa.OneOf([iaa.Noop(), iaa.Noop()],
                   name="OneOfNoop"), default_dtypes),
        (iaa.Sometimes(0.5, iaa.Noop(), name="SometimesNoop"), default_dtypes),
        (iaa.Sequential([iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                        name="Sequential"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.SomeOf(1, [iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                    name="SomeOf"), _not_dts([np.uint32, np.int32,
                                              np.float64])),
        (iaa.OneOf([iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                   name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"),
         _not_dts([np.uint32, np.int32, np.float64])),
        # WithChannels
        (iaa.Noop(name="Noop"), default_dtypes),
        # Lambda
        # AssertLambda
        # AssertShape
        (iaa.Alpha((0.0, 0.1), iaa.Noop(), name="AlphaNoop"), default_dtypes),
        (iaa.AlphaElementwise((0.0, 0.1),
                              iaa.Noop(),
                              name="AlphaElementwiseNoop"), default_dtypes),
        (iaa.SimplexNoiseAlpha(iaa.Noop(),
                               name="SimplexNoiseAlphaNoop"), default_dtypes),
        (iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                 first=iaa.Noop(),
                                 name="SimplexNoiseAlphaNoop"),
         default_dtypes),
        (iaa.Alpha((0.0, 0.1), iaa.Add(10),
                   name="Alpha"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10),
                              name="AlphaElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                 first=iaa.Add(10),
                                 name="SimplexNoiseAlpha"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Superpixels(p_replace=0.01, n_segments=64),
         _not_dts([np.float16, np.float32, np.float64])),
        (iaa.Resize({
            "height": 4,
            "width": 4
        }, name="Resize"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.Pad(px=(0, 10), name="Pad"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.Crop(px=(0, 10), name="Crop"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ]))
    ]

    for (aug, allowed_dtypes) in augs:
        # print("aug", aug.name)
        # print("allowed_dtypes", allowed_dtypes)
        for images_i in images:
            if images_i.dtype in allowed_dtypes:
                # print("image dt", images_i.dtype)
                images_aug = aug.augment_images(images_i)
                assert images_aug.dtype == images_i.dtype
            else:
                # print("image dt", images_i.dtype, "[SKIPPED]")
                pass
Пример #27
0
def get_augs():
    iaa_Blur = iaa.OneOf([
                    iaa.GaussianBlur((0, 1.5)), # blur images with a sigma between 0 and 1.5
                    iaa.AverageBlur(k=(2, 4)), # blur image using local means with kernel sizes between 2 and 7
                    iaa.MedianBlur(k=(3, 5)), # blur image using local medians with kernel sizes between 3 and 5
    ])
    iaa_Sharpen = iaa.OneOf([
                    iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.5)), # sharpen images
                    iaa.Emboss(alpha=(0, 0.5), strength=(0, 0.5)), # emboss images
    ])
    iaa_Noise = iaa.OneOf([
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03*255), per_channel=0.1), # add gaussian noise to images
                    iaa.Dropout((0, 0.02), per_channel=0.1), # randomly remove up to 10% of the pixels
                    #iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
    ])
    iaa_Affine = iaa.OneOf([
                    iaa.Affine(
                        scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
                        translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                        rotate=(-45, 45), # rotate by -45 to +45 degrees
                        shear=(-16, 16), # shear by -16 to +16 degrees
                        order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                        cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                        mode='edge' # use zeros for padding
                    ),
                    iaa.PiecewiseAffine(scale=(0.01, 0.05)), # move parts of the image around # is it fast?
                    iaa.PerspectiveTransform(scale=(0.01, 0.1)) # is it fast?
                    #iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25), # move pixels locally around (with random strengths) # we need borders
    ])
    iaa_HSV = iaa.OneOf([
                    iaa.Grayscale(alpha=(0.0, 0.5)),  
                    iaa.Add((-10, 10), per_channel=0.01), # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                    iaa.Sequential([
                       iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                       iaa.WithChannels(0, iaa.Add((-100, 100))),
                       iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")
                    ]),
                    iaa.ContrastNormalization((0.7, 1.6), per_channel=0), # improve or worsen the contrast
                    iaa.Multiply((0.7, 1.2), per_channel=0.01),
                    iaa.FrequencyNoiseAlpha(
                        exponent=(-2, 2),
                        first=iaa.Multiply((0.85, 1.2), per_channel=False),
                        second=iaa.ContrastNormalization((0.5, 1.7))),
    ])
    iaa_Simple = iaa.OneOf([
                iaa.Fliplr(0.5), # horizontally flip 50% of all images
                iaa.Flipud(0.5), # vertically flip 20% of all images
                iaa.CropAndPad(
                    percent=(-0.05, 0.1),
                    pad_mode='edge',
                    pad_cval=(0, 255)
                ),
    ])
        
    augs = {
        'simple': iaa_Simple,
        'affine': iaa_Affine,
        'hsv': iaa_HSV,
        'sharpen': iaa_Sharpen,
        'blur': iaa_Blur,
        'noise': iaa_Noise,
    }
    return augs
def img_aug_heavy(img):
    # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
    # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    # Define our sequence of augmentation steps that will be applied to every image
    # All augmenters with per_channel=0.5 will sample one value _per image_
    # in 50% of all cases. In all other cases they will sample new values
    # _per channel_.
    global heavy_seq
    if heavy_seq is None:
        print("init heavy_seq")
        heavy_seq = iaa.Sequential(
            [
                # apply the following augmenters to most images
                iaa.Fliplr(0.5),  # horizontally flip 50% of all images
                # iaa.Flipud(0.2),  # vertically flip 20% of all images
                # crop images by -5% to 10% of their height/width
                sometimes(iaa.CropAndPad(
                    percent=(-0.05, 0.1),
                    pad_mode=ia.ALL,
                    pad_cval=(0, 255)
                )),
                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
                    translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},  # translate by -20 to +20 percent (per axis)
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
                    cval=(0, 255),  # if mode is constant, use a cval between 0 and 255
                    mode=ia.ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
                # execute 0 to 5 of the following (less important) augmenters per image
                # don't execute all of them, as that would often be way too strong
                iaa.SomeOf((0, 5),
                           [
                               sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
                               # convert images into their superpixel representation
                               iaa.OneOf([
                                   iaa.GaussianBlur((0, 3.0)),  # blur images with a sigma between 0 and 3.0
                                   iaa.AverageBlur(k=(2, 7)),
                                   # blur image using local means with kernel sizes between 2 and 7
                                   iaa.MedianBlur(k=(3, 11)),
                                   # blur image using local medians with kernel sizes between 2 and 7
                               ]),
                               iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),  # sharpen images
                               iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),  # emboss images
                               # search either for all edges or for directed edges,
                               # blend the result with the original image using a blobby mask
                               iaa.SimplexNoiseAlpha(iaa.OneOf([
                                   iaa.EdgeDetect(alpha=(0.5, 1.0)),
                                   iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                               ])),
                               iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                               # add gaussian noise to images
                               iaa.OneOf([
                                   iaa.Dropout((0.01, 0.1), per_channel=0.5),  # randomly remove up to 10% of the pixels
                                   iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                               ]),
                               iaa.Invert(0.05, per_channel=True),  # invert color channels
                               iaa.Add((-10, 10), per_channel=0.5),
                               # change brightness of images (by -10 to 10 of original value)
                               iaa.AddToHueAndSaturation((-20, 20)),  # change hue and saturation
                               # either change the brightness of the whole image (sometimes
                               # per channel) or change the brightness of subareas
                               iaa.OneOf([
                                   iaa.Multiply((0.5, 1.5), per_channel=0.5),
                                   iaa.FrequencyNoiseAlpha(
                                       exponent=(-4, 0),
                                       first=iaa.Multiply((0.5, 1.5), per_channel=True),
                                       second=iaa.ContrastNormalization((0.5, 2.0))
                                   )
                               ]),
                               iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),  # improve or worsen the contrast
                               iaa.Grayscale(alpha=(0.0, 1.0)),
                               sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
                               # move pixels locally around (with random strengths)
                               sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))),
                               # sometimes move parts of the image around
                               sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
    img_aug = heavy_seq.augment_image(img)
    return img_aug
Пример #29
0
    def __init__(self,
                 X,
                 Y,
                 batch_size=32,
                 shape=(256, 256, 1),
                 shuffle=False,
                 da=True,
                 rotation90=False,
                 rotation_range=0.0,
                 vflip=False,
                 hflip=False,
                 elastic=False,
                 g_blur=False,
                 median_blur=False,
                 gamma_contrast=False,
                 zoom=0.0,
                 random_crops_in_DA=False,
                 prob_map=False,
                 train_prob=None,
                 val=False,
                 n_classes=1,
                 out_number=1,
                 extra_data_factor=1):

        if rotation_range != 0 and rotation90:
            raise ValueError(
                "'rotation_range' and 'rotation90' can not be set "
                "together")
        if median_blur and g_blur:
            raise ValuError(
                "'median_blur' and 'g_blur' can not be set together")
        if random_crops_in_DA and (shape[0] != shape[1]):
            raise ValuError("When 'random_crops_in_DA' is selected the shape "
                            "given must be square, e.g. (256, 256, 1)")

        if isinstance(zoom, (float, int)):
            self.zoom = [1 - zoom, 1 + zoom]
        elif (len(zoom) == 2
              and all(isinstance(val, (float, int)) for val in zoom)):
            self.zoom = [zoom[0], zoom[1]]
        else:
            raise ValueError('`zoom` should be a float or '
                             'a tuple or list of two floats. '
                             'Received: %s' % (zoom, ))

        self.shape = shape
        self.batch_size = batch_size
        self.X = (X / 255).astype(np.float32) if np.max(X) > 2 else (X).astype(
            np.float32)
        self.Y = (Y / 255).astype(np.uint8) if np.max(Y) > 2 else (Y).astype(
            np.uint8)
        self.shuffle = shuffle
        self.n_classes = n_classes
        self.out_number = out_number
        self.da = da
        self.random_crops_in_DA = random_crops_in_DA
        self.train_prob = train_prob
        self.val = val
        self.o_indexes = np.arange(len(self.X))
        if extra_data_factor > 1:
            self.extra_data_factor = extra_data_factor
            self.o_indexes = np.concatenate([self.o_indexes] *
                                            extra_data_factor)
        else:
            self.extra_data_factor = 1
        self.on_epoch_end()

        da_options = []
        self.t_made = ''
        if rotation90:
            da_options.append(iaa.Rot90((0, 3)))
            self.t_made += '_rot[0,90,180,270]'
        if rotation_range != 0:
            t = iaa.Affine(rotate=(-rotation_range, rotation_range),
                           mode='reflect')
            # Force the reflect mode on segmentation maps
            t._mode_segmentation_maps = "reflect"
            da_options.append(t)
            self.t_made += '_rot_range[-' + str(rotation_range) + ',' + str(
                rotation_range) + ']'
        if vflip:
            da_options.append(iaa.Flipud(0.5))
            self.t_made += '_vf'
        if hflip:
            da_options.append(iaa.Fliplr(0.5))
            self.t_made += '_hf'
        if elastic:
            da_options.append(
                iaa.Sometimes(
                    0.5,
                    iaa.ElasticTransformation(alpha=(240, 250),
                                              sigma=25,
                                              mode="reflect")))
            self.t_made += '_elastic'
        if g_blur:
            da_options.append(
                iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(1.0, 2.0))))
            self.t_made += '_gblur'
        if median_blur:
            da_options.append(iaa.Sometimes(0.5, iaa.MedianBlur(k=(3, 7))))
            self.t_made += '_mblur'
        if gamma_contrast:
            da_options.append(
                iaa.Sometimes(0.5, iaa.GammaContrast((1.25, 1.75))))
            self.t_made += '_gcontrast'
        if self.zoom[0] != 1 or self.zoom[1] != 1:
            da_options.append(
                iaa.Sometimes(
                    1,
                    iaa.Affine(
                        scale={
                            "x": (self.zoom[0], self.zoom[1]),
                            "y": (self.zoom[0], self.zoom[1])
                        })))
            self.t_made += '_zoom'

        self.seq = iaa.Sequential(da_options)
        self.t_made = '_none' if self.t_made == '' else self.t_made
 #     cval=(0, 255),  #全白全黑填充
 #     mode=ia.ALL    #定义填充图像外区域的方法
 # )),
 # 使用下面的0个到5个之间的方法去增强图像。注意SomeOf的用法
 iaa.SomeOf(
     (1, 5),
     [
         # 将部分图像进行超像素的表示。
         sometimes(
             iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))),
         #用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法
         iaa.OneOf([
             iaa.GaussianBlur((0, 3.0)),
             iaa.AverageBlur(
                 k=(2, 7)),  # 核大小2~7之间,k=((5, 7), (1, 3))时,核高度5~7,宽度1~3
             iaa.MedianBlur(k=(3, 7)),
         ]),
         #锐化处理
         iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
         #边缘检测,将检测到的赋值0或者255然后叠在原图上
         sometimes(
             iaa.OneOf([
                 iaa.EdgeDetect(alpha=(0, 0.7)),
                 iaa.DirectedEdgeDetect(alpha=(0, 0.7),
                                        direction=(0.0, 1.0)),
             ])),
         # 加入高斯噪声
         iaa.AdditiveGaussianNoise(
             loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
         # 将1%到5%的像素设置为黑色
         # 或者将3%到15%的像素用原图大小2%到5%的黑色方块覆盖