Exemplo n.º 1
0
                },
                rotate=(-10, 10),
                shear=(-5, 5),
                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, 1.0)),
             iaa.AverageBlur(k=(3, 5)),
             #iaa.MedianBlur(k=(3, 5)),
         ]),
         iaa.Sharpen(alpha=(0, 1.0), lightness=(0.9, 1.1)),
         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.01 * 255), per_channel=0.5),
         iaa.OneOf([
             iaa.Dropout((0.01, 0.05), per_channel=0.5),
             iaa.CoarseDropout((0.01, 0.03),
                               size_percent=(0.01, 0.02),
                               per_channel=0.2),
         ]),
Exemplo n.º 2
0
    ]),
    iaa.OneOf([
        iaa.MedianBlur(k=(3, 7)),
        iaa.BilateralBlur(
            d=(3, 10), sigma_color=(10, 250), sigma_space=(10, 250)),
        iaa.MotionBlur(k=(3, 9), angle=[-45, 45]),
        iaa.MeanShiftBlur(spatial_radius=(5.0, 10.0),
                          color_radius=(5.0, 10.0)),
        iaa.AllChannelsCLAHE(clip_limit=(1, 10)),
        iaa.AllChannelsHistogramEqualization(),
        iaa.GammaContrast((0.5, 1.5), per_channel=True),
        iaa.GammaContrast((0.5, 1.5)),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
        iaa.HistogramEqualization(),
        iaa.Sharpen(alpha=0.5)
    ]),
    iaa.OneOf([
        iaa.AveragePooling([2, 3]),
        iaa.MaxPooling(([2, 3], [2, 3])),
    ]),
    iaa.OneOf([
        iaa.Clouds(),
        iaa.Snowflakes(flake_size=(0.1, 0.4), speed=(0.01, 0.05)),
        iaa.Rain(speed=(0.1, 0.3))
    ])
],
                           random_order=True)


def get_color_augmentation(augment_prob):
config = process_config("./config.json")
args = Bunch(config.pretrained_model)
model = face_model.FaceModel(args)

sometimes = lambda aug: iaa.Sometimes(0.8, aug)
seq = iaa.Sequential([
    iaa.Fliplr(0.5),
    sometimes(
        iaa.OneOf([
            iaa.Grayscale(alpha=(0.0, 1.0)),
            iaa.AddToHueAndSaturation((-20, 20)),
            iaa.Add((-20, 20), per_channel=0.5),
            iaa.Multiply((0.5, 1.5), per_channel=0.5),
            iaa.GaussianBlur((0, 2.0)),
            iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5),
            iaa.Sharpen(alpha=(0, 0.5), lightness=(0.7, 1.3)),
            iaa.Emboss(alpha=(0, 0.5), strength=(0, 1.5))
        ]))
])

for mset in ['train', 'test', 'testcam']:
    output_dir = './data/embedding/%s/%s' % (
        args.model.split(',')[0].split('/')[-2], mset)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    for rdir, sdir, files in os.walk('./datasets/aligned/%s/112x112/' % mset):
        for file in tqdm(files):
            if file == '.' or file == '..':
                continue
            #fn, fe = os.path.splitext(file)
def _apply_aug_default():
    """
    Apply General augmentation in a single pipeline
    """
    sometimes = (lambda aug: iaa.Sometimes(0.5, aug))
    # Define our sequence of augmentation steps that will be applied to every image.
    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 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)
Exemplo n.º 5
0
import sys
import random
import numpy as np
import cv2
import skimage.io
import warnings
warnings.simplefilter('ignore')
import time

####################################################################
# Augmentations
####################################################################
# TGAR, add your own augmentations using the iaa package
# Sharpen and Emboss
SEaug = iaa.Sequential([
    iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
    iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5))
],
                       random_order=True)

# Gaussian Noise
GNaug = iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255))

# Color
Caug = iaa.Sequential([
    iaa.WithChannels(0, iaa.Add((10, 100))),
    iaa.WithChannels(1, iaa.Add((10, 100))),
    iaa.WithChannels(2, iaa.Add((10, 100)))
])

# Brightness and Contrast
Exemplo n.º 6
0
 def test_pickleable(self):
     aug = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(1, 3), seed=1)
     runtest_pickleable_uint8_img(aug, iterations=20)
Exemplo n.º 7
0
    def __init__(self,aug_level):
        
        frequently = lambda aug: iaa.Sometimes(0.9, aug)
        often = lambda aug: iaa.Sometimes(0.7, aug)
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)
        occasionally = lambda aug: iaa.Sometimes(0.3, aug)
        rarely = lambda aug: iaa.Sometimes(0.1, aug)
        
        if aug_level == 'high':
            self.aug = iaa.Sequential([
                
                sometimes(iaa.Crop(percent=(0, 0.1))),
                rarely(iaa.ContrastNormalization((0.75, 1.5))),
                occasionally(iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.1, 0.2))),

                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=(-20, 20),
                    shear=(-8, 8),
                    order=[0, 1],
                    cval=(0, 255),
                    mode=ia.ALL
                )),
                
                iaa.SomeOf((0, 5),
                [

                # 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)),

                # 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.03*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.01, 0.02),
                        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.0), per_channel=0.5),

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

                # 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)))
            ], random_order=True)

            ], random_order=True)
            
        elif aug_level == 'low':
            self.aug = iaa.Sequential([
                iaa.OneOf([
                iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=(0.0, 0.1*255))),
                iaa.Sometimes(0.1, iaa.AdditiveGaussianNoise(scale=(0.0, 0.3*255)))
                          ]),
                
                iaa.Sometimes(0.5, iaa.GammaContrast((0.5, 2.0))),
                iaa.Sometimes(0.5, iaa.imgcorruptlike.Brightness(severity=2)),
                iaa.Sometimes(0.5, iaa.Affine(
                    rotate=(-20, 20),
                )),
            ], random_order=True)
        else:
            raise NotImplementedError
Exemplo n.º 8
0
def sharpen(img):
    seq = iaa.Sequential([iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))])
    return seq.augment_image(img)
Exemplo n.º 9
0
from imgaug import augmenters as iaa
from keras.regularizers import l2

#Data augmentation

aug90 = iaa.Sometimes(
    1, iaa.Affine(rotate=(-90, -90), mode="constant", cval=(-1000, -1000)))
aug180 = iaa.Sometimes(
    1, iaa.Affine(rotate=(-180, -180), mode="constant", cval=(-1000, -1000)))
aug270 = iaa.Sometimes(
    1, iaa.Affine(rotate=(-270, -270), mode="constant", cval=(-1000, -1000)))
augFlip1 = iaa.Sometimes(1, iaa.Fliplr(1))
augFlip2 = iaa.Sometimes(1, iaa.Flipud(1))
augCon = iaa.Sometimes(1, iaa.ContrastNormalization(0.5, 0.9))
augBlur = iaa.Sometimes(1, iaa.GaussianBlur(sigma=(0.0, 0.8)))
augSharpen = iaa.Sometimes(1, iaa.Sharpen(alpha=0.1, lightness=0.7))


def augmentHK(image):
    w1 = image.reshape(image.shape[0], image.shape[1], image.shape[2])
    k = random.randrange(1, 11)
    global w2
    if k == 1:
        w2 = aug90.augment_image(w1)
    elif k == 2:
        w2 = aug180.augment_image(w1)
    elif k == 3:
        w2 = aug270.augment_image(w1)
    elif k == 4:
        w2 = augFlip1.augment_image(w1)
    elif k == 5:
class DetectorDataset(utils.Dataset):
    """Dataset class for training pneumonia detection on the RSNA pneumonia dataset.
    """
    augmentation_channel_1 = iaa.Sequential([iaa.Sharpen(alpha=0.5, lightness=2.0)])

    augmentation_channel_2 = iaa.Sequential([iaa.ContrastNormalization((2, 1))])

    #MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes", "Fliplr", "Flipud", "CropAndPad", "Affine", "PiecewiseAffine"]

    augmentation_mode = False

    def __init__(self, image_fps, image_annotations, orig_height, orig_width):
        super().__init__(self)

        # Add classes
        self.add_class('pneumonia', 1, 'Lung Opacity')

        # add images
        for i, fp in enumerate(image_fps):
            annotations = image_annotations[fp]
            self.add_image('pneumonia', image_id=i, path=fp,
                           annotations=annotations, orig_height=orig_height, orig_width=orig_width)

    def image_reference(self, image_id):
        info = self.image_info[image_id]
        return info['path']

    def load_image(self, image_id):
        info = self.image_info[image_id]
        fp = info['path']
        ds = pydicom.read_file(fp)
        image = ds.pixel_array

        if len(image.shape) != 3 or image.shape[2] != 3:

            if self.augmentation_mode:
            #add augmentation as 2 of 3 channels
            # Store shapes before augmentation to compare
                image_shape = image.shape
                channel_2 = self.augmentation_channel_1.augment_image(image)
                channel_3 = self.augmentation_channel_2.augment_image(image)
                image = np.stack((image, channel_2, channel_3), -1)
            else:
                image = np.stack((image,)*3, -1)
        return image

    def load_mask(self, image_id):
        info = self.image_info[image_id]
        annotations = info['annotations']
        count = len(annotations)
        if count == 0:
            mask = np.zeros((info['orig_height'], info['orig_width'], 1), dtype=np.uint8)
            class_ids = np.zeros((1,), dtype=np.int32)
        else:
            mask = np.zeros((info['orig_height'], info['orig_width'], count), dtype=np.uint8)
            class_ids = np.zeros((count,), dtype=np.int32)
            if self.augmentation_mode:
                def hook(images, augmenter, parents, default):
                    """Determines which augmenters to apply to masks."""
                    return augmenter.__class__.__name__ in self.MASK_AUGMENTERS
            for i, a in enumerate(annotations):
                if a['Target'] == 1:
                    x = int(a['x'])
                    y = int(a['y'])
                    w = int(a['width'])
                    h = int(a['height'])
                    mask_instance = mask[:, :, i].copy()
                    cv2.rectangle(mask_instance, (x, y), (x + w, y + h), 255, -1)
                    mask[:, :, i] = mask_instance
                    class_ids[i] = 1
        return mask.astype(np.bool), class_ids.astype(np.int32)
Exemplo n.º 11
0
FTR_CLR_HIST = True

COLORSPACE = 'YCrCb'
ORIENT = 9
PIXEL_PER_CELL = 8
CELL_PER_BLOCK = 2
HOG_CHNL = 'ALL'
SPATIAL_SIZE = (32, 32)
HISTO_BINS = 32
BINS_RANGE = (0, 256)

SEQ_RTT = iaa.Affine(rotate=(-20, 20))  # rotate by -20 to +20 degrees
SEQ_FLIP = iaa.Fliplr(1.0)  # always horizontally flip each input image
SEQ_BLR = iaa.GaussianBlur(sigma=(0,
                                  3.0))  # blur images with a sigma of 0 to 3.0
SEQ_SHRP = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))  # sharpen images
SEQ_BRT = iaa.Add(
    (-10, 10), per_channel=0.5
)  # change brightness of images (by -10 to 10 of original value)

VEHICLE_IMG_PATHS = 'C:\\Users\\Yiqun\Downloads\\training_images\\vehicles\\*\\*.png'
NON_VEHICLE_IMG_PATHS = 'C:\\Users\\Yiqun\Downloads\\training_images\\non-vehicles\\*\\*.png'


def extract_hog_features(img,
                         orient,
                         pix_per_cell,
                         cell_per_block,
                         vis=False,
                         feature_vec=True):
    #if call with two outputs then vis = True
Exemplo n.º 12
0
    def __init__(self,
                 ann_file,
                 root,
                 remove_images_without_annotations,
                 mask_type='polygon',
                 transforms=None):
        # "mask_type" = "polygon" or "image"

        self.mask_type = mask_type
        self.img_key_list = list()
        self.img_dict = dict()
        self.ann_info = dict()

        cls = RSNADataset.CLASSES
        self.class_to_ind = dict(zip(cls, range(len(cls))))

        for dirName, subdirList, fileList in os.walk(root):
            for filename in fileList:
                filename, ext = os.path.splitext(filename)
                if ext.lower() in [".png", ".jpg", ".jpeg"]:
                    self.img_dict[filename] = os.path.join(
                        dirName, filename + ext)
                    self.ann_info[filename] = list()

        # csv 용도 이며, mask 이미지인 경우 다르게 작업
        with open(ann_file, 'r') as ann_f:
            ann_cvf = csv.reader(ann_f)

            # patientId,x,y,width,height,Target
            for i, line in enumerate(ann_cvf):
                if i == 0:
                    continue

                filename, x, y, w, h, target = line
                target = int(target)

                if remove_images_without_annotations:
                    if target == 0:
                        continue

                    x1 = int(x)
                    y1 = int(y)
                    w = int(w)
                    h = int(h)

                    x2 = x1 + w
                    y2 = y1 + h

                    self.img_key_list.append(filename)
                else:
                    self.img_key_list.append(filename)
                    x1 = 0
                    y1 = 0
                    x2 = 0
                    y2 = 0

                try:
                    self.ann_info[filename].append([x1, y1, x2, y2, target])
                except KeyError:
                    continue

        self.augmentation = iaa.Sequential([
            iaa.OneOf([  ## geometric transform
                iaa.Affine(
                    scale={
                        "x": (0.98, 1.02),
                        "y": (0.98, 1.02)
                    },
                    translate_percent={
                        "x": (-0.02, 0.02),
                        "y": (-0.04, 0.04)
                    },
                    rotate=(-2, 2),
                    shear=(-1, 1),
                ),
                iaa.PiecewiseAffine(scale=(0.001, 0.025)),
            ]),
            iaa.OneOf([  ## brightness or contrast
                iaa.Multiply((0.9, 1.1)),
                iaa.ContrastNormalization((0.9, 1.1)),
            ]),
            iaa.OneOf([  ## blur or sharpen
                iaa.GaussianBlur(sigma=(0.0, 0.1)),
                iaa.Sharpen(alpha=(0.0, 0.1)),
            ]),
        ])

        # 중복 방지 RSNA csv 파일 참조
        self.img_key_list = list(set(self.img_key_list))
        self.transforms = transforms
Exemplo n.º 13
0
def transform(aug_type, magnitude, X):
    if aug_type == "crop":
        X_aug = iaa.Crop(px=(0, int(magnitude * 32))).augment_images(X)
    elif aug_type == "gaussian-blur":
        X_aug = iaa.GaussianBlur(sigma=(0, magnitude * 25.0)).augment_images(X)
    elif aug_type == "rotate":
        X_aug = iaa.Affine(rotate=(-180 * magnitude, 180 * magnitude)).augment_images(X)
    elif aug_type == "shear":
        X_aug = iaa.Affine(shear=(-90 * magnitude, 90 * magnitude)).augment_images(X)
    elif aug_type == "translate-x":
        X_aug = iaa.Affine(
            translate_percent={"x": (-magnitude, magnitude), "y": (0, 0)}
        ).augment_images(X)
    elif aug_type == "translate-y":
        X_aug = iaa.Affine(
            translate_percent={"x": (0, 0), "y": (-magnitude, magnitude)}
        ).augment_images(X)
    elif aug_type == "horizontal-flip":
        X_aug = iaa.Fliplr(magnitude).augment_images(X)
    elif aug_type == "vertical-flip":
        X_aug = iaa.Flipud(magnitude).augment_images(X)
    elif aug_type == "sharpen":
        X_aug = iaa.Sharpen(
            alpha=(0, 1.0), lightness=(0.50, 5 * magnitude)
        ).augment_images(X)
    elif aug_type == "emboss":
        X_aug = iaa.Emboss(
            alpha=(0, 1.0), strength=(0.0, 20.0 * magnitude)
        ).augment_images(X)
    elif aug_type == "additive-gaussian-noise":
        X_aug = iaa.AdditiveGaussianNoise(
            loc=0, scale=(0.0, magnitude * 255), per_channel=0.5
        ).augment_images(X)
    elif aug_type == "dropout":
        X_aug = iaa.Dropout(
            (0.01, max(0.011, magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # Dropout first argument should be smaller than second one
    elif aug_type == "coarse-dropout":
        X_aug = iaa.CoarseDropout(
            (0.03, 0.15), size_percent=(0.30, np.log10(magnitude * 3)), per_channel=0.2
        ).augment_images(X)
    elif aug_type == "gamma-contrast":
        X_norm = normalize(X)
        X_aug_norm = iaa.GammaContrast(magnitude * 1.75).augment_images(
            X_norm
        )  # needs 0-1 values
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "brighten":
        X_aug = iaa.Add(
            (int(-40 * magnitude), int(40 * magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # brighten
    elif aug_type == "invert":
        X_aug = iaa.Invert(1.0).augment_images(X)  # magnitude not used
    elif aug_type == "fog":
        X_aug = iaa.Fog().augment_images(X)  # magnitude not used
    elif aug_type == "clouds":
        X_aug = iaa.Clouds().augment_images(X)  # magnitude not used
    elif aug_type == "histogram-equalize":
        X_aug = iaa.AllChannelsHistogramEqualization().augment_images(
            X
        )  # magnitude not used
    elif aug_type == "super-pixels":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.Superpixels(
            p_replace=(0, magnitude), n_segments=(100, 100)
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "perspective-transform":
        X_norm = normalize(X)
        X_aug_norm = iaa.PerspectiveTransform(
            scale=(0.01, max(0.02, magnitude))
        ).augment_images(
            X_norm
        )  # first scale param must be larger
        np.clip(X_aug_norm, 0.0, 1.0, out=X_aug_norm)
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "elastic-transform":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.ElasticTransformation(
            alpha=(0.0, max(0.5, magnitude * 300)), sigma=5.0
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "add-to-hue-and-saturation":
        X_aug = iaa.AddToHueAndSaturation(
            (int(-45 * magnitude), int(45 * magnitude))
        ).augment_images(X)
    elif aug_type == "coarse-salt-pepper":
        X_aug = iaa.CoarseSaltAndPepper(p=0.2, size_percent=magnitude).augment_images(X)
    elif aug_type == "grayscale":
        X_aug = iaa.Grayscale(alpha=(0.0, magnitude)).augment_images(X)
    else:
        raise ValueError
    return X_aug
X_train = X_train.reshape(-1, 28, 28, 1)

#Single Training Image
img = images.iloc[0].as_matrix().reshape((28, 28))
plt.imshow(img, cmap='binary')
plt.savefig('real.png')
plt.show()

########################################################################################################
#Agumented Images

X_train_aug1 = iaa.Fliplr(1).augment_images(X_train)
X_train_aug2 = iaa.AverageBlur(k=(3, 4)).augment_images(X_train)
X_train_aug3 = iaa.Emboss(alpha=(0.8, 1.0),
                          strength=(0, 3.0)).augment_images(X_train)
X_train_aug4 = iaa.Sharpen(alpha=(0.8, 0.9),
                           lightness=(0.75, 1.5)).augment_images(X_train)
X_train_aug5 = iaa.AdditiveGaussianNoise(
    loc=0, scale=(5.0, 0.05 * 255)).augment_images(X_train)
X_train_aug6 = iaa.ContrastNormalization((2.0, 2.5)).augment_images(X_train)

for i in range(10):
    #visualize X_train[i], X_train_aug1[i], etc
    gs = gridspec.GridSpec(3, 3)

    ax = plt.subplot(gs[0, 0])
    imgplot = ax.imshow(X_train[i].reshape((28, 28)), cmap="binary")
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_title('Original')
    ax.title.set_size(10)
Exemplo n.º 15
0
 def test_alpha_075(self):
     aug = iaa.Sharpen(alpha=0.75, lightness=1)
     observed = aug.augment_image(self.base_img)
     expected = self._compute_sharpened_base_img(
         0.75 * 1, 0.25 * self.m_noop + 0.75 * self.m)
     assert np.allclose(observed, expected)
Exemplo n.º 16
0
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    image = misc.imresize(
        ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    #image = misc.imresize(data.chelsea()[0:300, 50:350, :], (128, 128))
    #image = misc.imresize(data.astronaut(), (128, 128))

    #keypoints = [ia.Keypoint(x=43, y=43), ia.Keypoint(x=78, y=40), ia.Keypoint(x=64, y=73)] # left eye, right eye, mouth
    keypoints = [
        ia.Keypoint(x=34, y=15),
        ia.Keypoint(x=85, y=13),
        ia.Keypoint(x=63, y=73)
    ]  # left ear, right ear, mouth
    keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]

    print("[draw_per_augmenter_images] Initializing...")
    rows_augmenters = [
        ("Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]),
        #("Crop", [iaa.Crop(px=vals) for vals in [(2, 4), (4, 8), (6, 16), (8, 32), (10, 64)]]),
        ("Crop\n(top, right,\nbottom, left)", [
            (str(vals), iaa.Crop(px=vals))
            for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16,
                                                      4), (8, 0, 0,
                                                           32), (32, 64, 0, 0)]
        ]),
        ("Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]),
        ("Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]),
        ("Superpixels\np_replace=1", [("n_segments=%d" % (n_segments, ),
                                       iaa.Superpixels(p_replace=1.0,
                                                       n_segments=n_segments))
                                      for n_segments in [25, 50, 75, 100, 125]
                                      ]),
        ("Superpixels\nn_segments=100",
         [("p_replace=%.2f" % (p_replace, ),
           iaa.Superpixels(p_replace=p_replace, n_segments=100))
          for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]),
        ("Invert", [("p=%d" % (p, ), iaa.Invert(p=p))
                    for p in [0, 0, 1, 1, 1]]),
        ("Invert\n(per_channel)", [("p=%.2f" % (p, ),
                                    iaa.Invert(p=p, per_channel=True))
                                   for p in [0.5, 0.5, 0.5, 0.5, 0.5]]),
        ("Add", [("value=%d" % (val, ), iaa.Add(val))
                 for val in [-45, -25, 0, 25, 45]]),
        ("Add\n(per channel)", [
            ("value=(%d, %d)" % (
                vals[0],
                vals[1],
            ), iaa.Add(vals, per_channel=True))
            for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]
        ]),
        ("Multiply", [("value=%.2f" % (val, ), iaa.Multiply(val))
                      for val in [0.25, 0.5, 1.0, 1.25, 1.5]]),
        ("Multiply\n(per channel)",
         [("value=(%.2f, %.2f)" % (
             vals[0],
             vals[1],
         ), iaa.Multiply(vals, per_channel=True))
          for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15,
                                                              1.35), (1.4,
                                                                      1.6)]]),
        ("GaussianBlur", [("sigma=%.2f" % (sigma, ),
                           iaa.GaussianBlur(sigma=sigma))
                          for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
        ("AverageBlur", [("k=%d" % (k, ), iaa.AverageBlur(k=k))
                         for k in [1, 3, 5, 7, 9]]),
        ("MedianBlur", [("k=%d" % (k, ), iaa.MedianBlur(k=k))
                        for k in [1, 3, 5, 7, 9]]),
        ("Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness, ),
                                 iaa.Sharpen(alpha=1, lightness=lightness))
                                for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
        ("Emboss\n(alpha=1)", [("strength=%.2f" % (strength, ),
                                iaa.Emboss(alpha=1, strength=strength))
                               for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
        ("EdgeDetect", [("alpha=%.2f" % (alpha, ), iaa.EdgeDetect(alpha=alpha))
                        for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        ("DirectedEdgeDetect\n(alpha=1)",
         [("direction=%.2f" % (direction, ),
           iaa.DirectedEdgeDetect(alpha=1, direction=direction))
          for direction in [
              0.0, 1 * (360 / 5) / 360, 2 * (360 / 5) / 360, 3 * (360 / 5) /
              360, 4 * (360 / 5) / 360
          ]]),
        ("AdditiveGaussianNoise",
         [("scale=%.2f*255" % (scale, ),
           iaa.AdditiveGaussianNoise(scale=scale * 255))
          for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        ("AdditiveGaussianNoise\n(per channel)",
         [("scale=%.2f*255" % (scale, ),
           iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True))
          for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        ("Dropout", [("p=%.2f" % (p, ), iaa.Dropout(p=p))
                     for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        ("Dropout\n(per channel)", [("p=%.2f" % (p, ),
                                     iaa.Dropout(p=p, per_channel=True))
                                    for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        ("CoarseDropout\n(p=0.2)",
         [("size_percent=%.2f" % (size_percent, ),
           iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2))
          for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        ("CoarseDropout\n(p=0.2, per channel)",
         [("size_percent=%.2f" % (size_percent, ),
           iaa.CoarseDropout(p=0.2,
                             size_percent=size_percent,
                             per_channel=True,
                             min_size=2))
          for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        ("ContrastNormalization", [("alpha=%.1f" % (alpha, ),
                                    iaa.ContrastNormalization(alpha=alpha))
                                   for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
        ("ContrastNormalization\n(per channel)",
         [("alpha=(%.2f, %.2f)" % (
             alphas[0],
             alphas[1],
         ), iaa.ContrastNormalization(alpha=alphas, per_channel=True))
          for alphas in [(0.4, 0.6), (0.65,
                                      0.85), (0.9, 1.1), (1.15,
                                                          1.35), (1.4, 1.6)]]),
        ("Grayscale", [("alpha=%.1f" % (alpha, ), iaa.Grayscale(alpha=alpha))
                       for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        ("PiecewiseAffine", [("scale=%.3f" % (scale, ),
                              iaa.PiecewiseAffine(scale=scale))
                             for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]),
        ("Affine: Scale", [("%.1fx" % (scale, ), iaa.Affine(scale=scale))
                           for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]),
        ("Affine: Translate",
         [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={
             "x": x,
             "y": y
         }))
          for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]),
        ("Affine: Rotate", [("%d deg" % (rotate, ), iaa.Affine(rotate=rotate))
                            for rotate in [-90, -45, 0, 45, 90]]),
        ("Affine: Shear", [("%d deg" % (shear, ), iaa.Affine(shear=shear))
                           for shear in [-45, -25, 0, 25, 45]]),
        ("Affine: Modes",
         [(mode, iaa.Affine(translate_px=-32, mode=mode))
          for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]),
        ("Affine: cval", [("%d" % (int(cval * 255), ),
                           iaa.Affine(translate_px=-32,
                                      cval=int(cval * 255),
                                      mode="constant"))
                          for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        ("Affine: all", [("",
                          iaa.Affine(scale={
                              "x": (0.5, 1.5),
                              "y": (0.5, 1.5)
                          },
                                     translate_px={
                                         "x": (-32, 32),
                                         "y": (-32, 32)
                                     },
                                     rotate=(-45, 45),
                                     shear=(-32, 32),
                                     mode=ia.ALL,
                                     cval=(0.0, 1.0))) for _ in sm.xrange(5)]),
        ("ElasticTransformation\n(sigma=0.2)",
         [("alpha=%.1f" % (alpha, ),
           iaa.ElasticTransformation(alpha=alpha, sigma=0.2))
          for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]])
    ]

    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_name, augmenters) in rows_augmenters:
        row_images = []
        row_keypoints = []
        row_titles = []
        for img_title, augmenter in augmenters:
            aug_det = augmenter.to_deterministic()
            row_images.append(aug_det.augment_image(image))
            row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
            row_titles.append(img_title)
        rows.append((row_name, row_images, row_keypoints, row_titles))

    # matplotlib drawin routine
    """
    print("[draw_per_augmenter_images] Plotting...")
    width = 8
    height = int(1.5 * len(rows_augmenters))
    fig = plt.figure(figsize=(width, height))
    grid_rows = len(rows)
    grid_cols = 1 + 5
    gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1])
    axes = []
    for i in sm.xrange(grid_rows):
        axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)])
    fig.tight_layout()
    #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22)
    #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02)
    fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02)

    for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows):
        axes_row = axes[row_idx]

        for col_idx in sm.xrange(grid_cols):
            ax = axes_row[col_idx]

            ax.cla()
            ax.axis("off")
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

            if col_idx == 0:
                ax.text(0, 0.5, row_name, color="black")
            else:
                cell_image = row_images[col_idx-1]
                cell_keypoints = row_keypoints[col_idx-1]
                cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5)
                ax.imshow(cell_image_kp)
                x = 0
                y = 145
                #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6)
                ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7)


    fig.savefig("examples.jpg", bbox_inches="tight")
    #plt.show()
    """

    # simpler and faster drawing routine
    output_image = ExamplesImage(128, 128, 128 + 64, 32)
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
    misc.imsave("examples.jpg", output_image.draw())
Exemplo n.º 17
0
 def test_alpha_1_lightness_3(self):
     aug = iaa.Sharpen(alpha=1.0, lightness=3)
     observed = aug.augment_image(self.base_img)
     expected = self._compute_sharpened_base_img(1.0 * 3, self.m)
     assert np.allclose(observed, expected)
Exemplo n.º 18
0
def draw_single_sequential_images():
    image = misc.imresize(
        ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    """
    rarely = lambda aug: iaa.Sometimes(0.1, aug)
    sometimes = lambda aug: iaa.Sometimes(0.25, aug)
    often = lambda aug: iaa.Sometimes(0.5, aug)
    seq = iaa.Sequential([
            iaa.Fliplr(0.5), # horizontally flip 50% of all images
            iaa.Flipud(0.5), # vertically flip 50% of all images
            rarely(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
            often(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
            rarely(iaa.GaussianBlur((0, 3.0))), # blur images with a sigma between 0 and 3.0
            rarely(iaa.AverageBlur(k=(2, 7))), # blur image using local means with kernel sizes between 2 and 7
            rarely(iaa.MedianBlur(k=(3, 11))), # blur image using local medians with kernel sizes between 2 and 7
            sometimes(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), # sharpen images
            sometimes(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), # emboss images
            # search either for all edges or for directed edges
            rarely(iaa.Sometimes(0.5,
                iaa.EdgeDetect(alpha=(0, 0.7)),
                iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
            )),
            often(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)), # add gaussian noise to images
            often(iaa.Dropout((0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels
            often(iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25), per_channel=0.5)),
            rarely(iaa.Invert(0.25, per_channel=True)), # invert color channels
            often(iaa.Add((-10, 10), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value)
            often(iaa.Multiply((0.5, 1.5), per_channel=0.5)), # change brightness of images (50-150% of original value)
            often(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast
            sometimes(iaa.Grayscale(alpha=(0.0, 1.0))),
            often(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)
            )),
            rarely(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) # move pixels locally around (with random strengths)
        ],
        random_order=True
    )
    """

    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.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=(-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
                    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)

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
Exemplo n.º 19
0
 def processor(self):
     return iaa.Sharpen(self.alpha, self.lightness)
Exemplo n.º 20
0
    # Change images to grayscale and overlay them with the original image by varying strengths,
    # effectively removing alpha_lo to alpha_hi of the color:
    "Grayscale": lambda alpha_lo, alpha_hi: iaa.Grayscale(alpha=(alpha_lo, alpha_hi)),

    # Blur each image with a gaussian kernel with a sigma between sigma_lo and sigma_hi:
    "Gaussian_Blur": lambda sigma_lo, sigma_hi: iaa.GaussianBlur(sigma=(sigma_lo, sigma_hi)),

    # Blur each image using a mean over neighbourhoods that have random sizes,
    # which can vary between h_lo and h_hi in height and w_lo and w_hi in width:
    "Average_Blur": lambda h_lo, h_hi, w_lo, w_hi: iaa.AverageBlur(k=((h_lo, h_hi), (w_lo, w_hi))),

    # Blur each image using a median over neighbourhoods that have a random size between lo x lo and hi x hi:
    "Median_Blur": lambda lo, hi: iaa.MedianBlur(k=(lo, hi)),

    # Sharpen an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi:
    "Sharpen": lambda alpha_lo, alpha_hi, lightness_lo, lightness_hi: iaa.Sharpen
    (alpha=(alpha_lo, alpha_hi), lightness=(lightness_lo, lightness_hi)),

    # Emboss an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi:
    "Emboss": lambda alpha_lo, alpha_hi, strength_lo, strength_hi:
    iaa.Emboss(alpha=(alpha_lo, alpha_hi), strength=(strength_lo, strength_hi)),

    # Detect edges in images, turning them into black and white images and
    # then overlay these with the original images using random alphas between alpha_lo and alpha_hi:
    "Detect_Edges": lambda alpha_lo, alpha_hi: iaa.EdgeDetect(alpha=(alpha_lo, alpha_hi)),

    # Detect edges having random directions between dir_lo and dir_hi (i.e (0.0, 1.0) = 0 to 360 degrees) in
    # images, turning the images into black and white versions and then overlay these with the original images
    # using random alphas between alpha_lo and alpha_hi:
    "Directed_edge_Detect": lambda alpha_lo, alpha_hi, dir_lo, dir_hi:
    iaa.DirectedEdgeDetect(alpha=(alpha_lo, alpha_hi), direction=(dir_lo, dir_hi)),
def _get_image_blob(roidb, im_scale, augment_en=False, mode='train'):
    """Builds an input blob from the images in the roidb at the specified
  scales.
  """
    num_images = len(roidb)
    processed_ims = []
    im_infos = []
    for i in range(num_images):
        #TODO: Probably can remove this conditional branch by just providing the filename instead of the image itself?
        if(mode == 'test'):
            #assert augment_en is False
            im          = cv2.imread(roidb[i])
            local_roidb = None
        else:
            im          = cv2.imread(roidb[i]['filename'])
            local_roidb = deepcopy(roidb)
        img_arr  = im
        mean     = 0
        sigma    = 2
        #scale


        if(augment_en and mode != 'test'):
            #print('augmenting image {}'.format(roidb[i]['filename']))
            #shape 0 -> height
            #shape 1 -> width
            np.random.seed(int.from_bytes(os.urandom(4), sys.byteorder))
            flip_frame  = np.random.choice([True,False],p=[0.5,0.5])
            if(local_roidb[i]['flipped'] is True):
                print('something wrong has happened')
            if(flip_frame):
                img_arr = img_arr[:, ::-1, :]
                local_roidb[i]['flipped'] = True
                oldx1 = local_roidb[i]['boxes'][:, 0].copy()
                oldx2 = local_roidb[i]['boxes'][:, 2].copy()
                local_roidb[i]['boxes'][:, 0] = im.shape[1] - oldx2 - 1
                local_roidb[i]['boxes'][:, 2] = im.shape[1] - oldx1 - 1
            else:
                local_roidb[i]['flipped'] = False
            #iaa.Sometimes(0.5,(iaa.CropAndPad(
            #    percent=(0, 0.1),
            #    pad_mode='constant',
            #    pad_cval=(0, 255),
            #    keep_size=True
            #))),
            seq = iaa.Sequential(
                [
                    iaa.SomeOf((0, 2),[
                        iaa.SomeOf((0,1),([
                            iaa.GaussianBlur((0.5, 2.5)),  # blur images with a sigma between 0 and 3.0
                            iaa.AverageBlur(k=(1, 3)),  # blur image using local means with kernel sizes between 2 and 7
                            iaa.MedianBlur(k=(1, 3)),  # blur image using local medians with kernel sizes between 2 and 7
                            iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))
                        ])),
                        iaa.AdditiveGaussianNoise(
                            loc=0,
                            scale=(0.0, 0.1*255),
                            per_channel=True),
                        iaa.AddToHueAndSaturation((-5, 5),per_channel=True),  # change hue and saturation
                    ], random_order=True),

                    iaa.Sometimes(0.3,(iaa.Affine(
                        scale={"x": (0.9, 1.2), "y": (0.9, 1.2)},  # scale images to 80-120% of their size, individually per axis
                        translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)},  # translate by -20 to +20 percent (per axis)
                        order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
                        cval=(0, 255),  # if mode is constant, use a cval between 0 and 255
                        shear=(-0.05, 0.05),
                        mode='constant'  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                    ))),
                    iaa.Sometimes(0.25,iaa.Dropout((0.01, 0.05), per_channel=0.5))
                    #iaa.SomeOf((0,1),[
                    #    iaa.Sometimes(0.25,iaa.Multiply((0.5, 1.5), per_channel=0.5)),
                    #    iaa.Invert(0.05, per_channel=True)
                    #]),
                    #iaa.OneOf([
                    #iaa.Sometimes(0.5,iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
                    #    iaa.PiecewiseAffine(scale=(0.01, 0.05))
                    #]),
                ], random_order=False
            )
            images_aug, bboxes_aug = seq(images=[img_arr],bounding_boxes=[local_roidb[i]['boxes']])
            img_arr = images_aug[0]
            local_roidb[i]['boxes'] = bboxes_aug[0]
            orig_height = img_arr.shape[0]
            orig_width  = img_arr.shape[1]
            img_arr     = np.minimum(img_arr,255)
            img_arr     = np.maximum(img_arr,0)
            img_arr     = img_arr.astype('uint8')
            #if(down_shift < 0):
            #    img_arr = np.pad(img_arr,((0,abs(down_shift)), (0,0), (0,0)), mode='constant',constant_values=(127))[abs(down_shift):,:,:]
            #elif(down_shift > 0):
            #    img_arr = np.pad(img_arr,((abs(down_shift),0), (0,0), (0,0)), mode='constant',constant_values=(127))[:-down_shift,:,:]
            #if(right_shift < 0):
            #    img_arr = np.pad(img_arr,((0,0), (0,abs(right_shift)), (0,0)), mode='constant',constant_values=(127))[:,abs(right_shift):,:]
            #elif(right_shift > 0):
            #    img_arr = np.pad(img_arr,((0,0), (abs(right_shift),0), (0,0)), mode='constant',constant_values=(127))[:,:-right_shift,:]
            for j, roi in enumerate(local_roidb[i]['boxes']):
                #boxes[ix, :] = [x1, y1, x2, y2]
                orig = roi
                h = roi[3] - roi[1]
                w = roi[2] - roi[0]
                roi[0] = np.minimum(np.maximum(roi[0],0),orig_width-1)
                roi[2] = np.minimum(np.maximum(roi[2],0),orig_width-1)
                roi[1] = np.minimum(np.maximum(roi[1],0),orig_height-1)
                roi[3] = np.minimum(np.maximum(roi[3],0),orig_height-1)
                #TODO: magic number
                if(roi[3] - roi[1] < 2):
                    #print('removing box y0 {} y1 {}'.format(roi[1],roi[3]))
                    local_roidb[i]['ignore'][j] = True
                #TODO: magic number
                if(roi[2] - roi[0] < 2):
                    #print('removing box  x0 {} x1 {}'.format(roi[0],roi[2]))
                    local_roidb[i]['ignore'][j] = True

                wc = roi[2] - roi[0]
                hc = roi[3] - roi[1]
                if(h != 0 and hc/h < 0.1):
                    local_roidb[i]['ignore'][j] = True
                elif(w != 0 and wc/w < 0.1):
                    local_roidb[i]['ignore'][j] = True

                if(local_roidb[i]['ignore'][j] is False and roi[2] < roi[0]):
                    print('x2 is smaller than x1')
                if(local_roidb[i]['ignore'][j] is False and roi[3] < roi[1]):
                    print('y2 is smaller than y1')
                if(local_roidb[i]['ignore'][j] is False and wc > w):
                    print('new x is larger than old x diff')
                if(local_roidb[i]['ignore'][j] is False and hc > h):
                    print('new y diff is larger than old y diff')
            im = img_arr
        elif(augment_en and mode == 'test'):
            seq = iaa.Sequential(
                [
                    #iaa.weather.Rain(speed=(0.15,0.25))
                    #iaa.Fog()
                    iaa.imgcorruptlike.Spatter(severity=5)
                    #iaa.Dropout((0.6))
                    #iaa.GaussianBlur(sigma=(4.0,4.0))
                ], random_order=False)
            images_aug = seq(images=[img_arr])
            img_arr = images_aug[0]
            orig_height = img_arr.shape[0]
            orig_width  = img_arr.shape[1]
            img_arr     = np.minimum(img_arr,255)
            img_arr     = np.maximum(img_arr,0)
            img_arr     = img_arr.astype('uint8')
            im = img_arr
        #minibatch(im,local_roidb[0])
        #draw_and_save_minibatch(im[:,:,cfg.PIXEL_ARRANGE_BGR],roidb[i])
        #TODO: Move scaling to be before imgaug, to save time
        im = prep_im_for_blob(im, cfg.PIXEL_MEANS, cfg.PIXEL_STDDEVS, cfg.PIXEL_ARRANGE, im_scale)
            #x_min, x_max, y_min, y_max, scale
        info = np.array([0, im.shape[1], 0, im.shape[0], 0, 0, im_scale], dtype=np.float32)
        im_infos.append(info)
        processed_ims.append(im)
    # Create a blob to hold the input images
    blob = im_list_to_blob(processed_ims)

    return im_infos, blob, local_roidb
Exemplo n.º 22
0
            translate_percent={
                "x": (-0.02, 0.02),
                "y": (-0.04, 0.04)
            },
            rotate=(-2, 2),
            shear=(-1, 1),
        ),
        iaa.PiecewiseAffine(scale=(0.001, 0.025)),
    ]),
    iaa.OneOf([  ## brightness or contrast
        iaa.Multiply((0.9, 1.1)),
        iaa.ContrastNormalization((0.9, 1.1)),
    ]),
    iaa.OneOf([  ## blur or sharpen
        iaa.GaussianBlur(sigma=(0.0, 0.1)),
        iaa.Sharpen(alpha=(0.0, 0.1)),
    ]),
])

# test on the same image as above
imggrid = augmentation.draw_grid(image[:, :, 0], cols=5, rows=2)
plt.figure(figsize=(30, 12))
_ = plt.imshow(imggrid[:, :, 0], cmap='gray')

model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR)

# Exclude the last layers because they require a matching
# number of classes
model.load_weights(COCO_WEIGHTS_PATH,
                   by_name=True,
                   exclude=[
Exemplo n.º 23
0
def _apply_aug_all():
    """ Load image augmentation model """

    sometimes = (lambda aug: 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.SimplexNoiseAlpha(
                        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.FrequencyNoiseAlpha(
                            exponent=(-4, 0),
                            first=iaa.Multiply((0.5, 1.5), per_channel=True),
                            second=iaa.ContrastNormalization((0.5, 2.0)))
                    ]),
                    # improve or worsen the contrast
                    iaa.ContrastNormalization((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 apply_transform_on_an_image(image, bbox=None):

    # 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)

    seq = iaa.Sequential(
        [
            # iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs
            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            # iaa.Sometimes(0.5,
            #               iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0))
            #               ),
            iaa.ContrastNormalization(
                iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            # iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0,
                         per_channel=0.2),
            # Apply affine transformations to each image.
            # - 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)
            # 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)},
            #     # translate_percent=0.1,
            #     rotate=iap.Normal(-45, 45),
            #     shear=(-8, 8),
            #     order=[0, 1],
            #     cval=(0, 255),
            #     # translate_px=iap.RandomSign(iap.Poisson(3)),
            #     # translate_px=3,
            #     # mode=["constant", "edge"]
            #     mode=ia.ALL
            # ),

            # iaa.AddElementwise(
            #     iap.Discretize(
            #         (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64
            #     )
            # ),
            #
            # 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, 0.5),
                    #         n_segments=(1, 4)
                    #     )
                    # ),

                    # 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, 0.3)),
                        iaa.AverageBlur(k=(1, 2)),
                        iaa.MedianBlur(k=(1, 3)),
                    ]),

                    # 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, 0.01), lightness=(0, 0.01)),

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

                    # 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.005)),
                            iaa.DirectedEdgeDetect(alpha=(0, 0.001),
                                                   direction=(0.0, 0.001)),
                        ])),

                    # 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.001 * 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, 0.05), per_channel=0.5),
                    #     iaa.CoarseDropout(
                    #         (0, 0.01), size_percent=(0.1, 0.2),
                    #         per_channel=0.2
                    #     ),
                    # ]),

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

                    # Add a value of -10 to 10 to each pixel.
                    #iaa.Add((-40, 40), 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.contrast.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, 1.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)
        ],
        random_order=True)

    # Augment BBs and images.
    aug_image = seq(image=image)
    return aug_image
Exemplo n.º 25
0
    augmentation = iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.33), # horizontally/vertically flip 50% of all images
            iaa.Flipud(0.33),
            iaa.Sometimes(0.33, iaa.Affine(rotate=(-180, 180))),
            # execute 0 to 5 of the following (less important) augmenters per image
            # don't execute all of them, as that would often be way too strong
            iaa.SomeOf((0, 5),
                [
                    iaa.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.CropAndPad(percent=(-0.25, 0)),
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                    iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
                    iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                    iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                ],
                random_order=True
            )
        ],
        random_order=True
    )

    #augmentation = iaa.Sometimes(0.75, iaa.SomeOf((1, None), [
    #   iaa.Fliplr(0.5),
    #   iaa.Flipud(0.5),
from imgaug import augmenters as iaa

aug = [
    iaa.LinearContrast(alpha=2),
    iaa.SigmoidContrast(gain=10),
    iaa.GammaContrast(gamma=2),
    iaa.CLAHE(clip_limit=(1, 5)),
    iaa.Grayscale(alpha=1.0),
    iaa.AddToHueAndSaturation((-20, 20), per_channel=True),
    iaa.BilateralBlur(d=6),
    iaa.MotionBlur(k=7),
    iaa.MedianBlur(k=3),
    iaa.AverageBlur(k=3),
    iaa.AdditiveGaussianNoise(loc=0.8, scale=(0.01, 0.08 * 255)),
    iaa.ContrastNormalization((0.3, 1.5)),
    iaa.Sharpen(alpha=0, lightness=1)
]


class Dataset(object):
    def __init__(self, args):
        self.args = args
        self.char = json.load(open(self.args.char, mode='r'))

    def get_file(self, data_path):
        temp = []
        txts = os.listdir(data_path + 'labels')
        for txt in txts:
            with open(data_path + 'labels/' + txt, 'r', encoding='utf-8') as f:
                lines = f.readlines()
                for line in lines:
Exemplo n.º 27
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.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)
Exemplo n.º 28
0
 def test_alpha_one(self):
     aug = iaa.Sharpen(alpha=1.0, lightness=1)
     observed = aug.augment_image(self.base_img)
     expected = self.base_img_sharpened
     assert np.allclose(observed, expected)
Exemplo n.º 29
0
    def __init__(self):
        # 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(.90, 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_.
        seq = iaa.Sequential(
            [
                sometimes(
                    iaa.CropAndPad(percent=(-0.03, 0.03),
                                   pad_mode=["constant", "edge"],
                                   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.1, 0.1), "y": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis)
                        rotate=(-3, 3),  # rotate by -45 to +45 degrees
                        shear=(-3, 3),  # 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)
                    )),
                iaa.SomeOf(
                    (0, 3),
                    [
                        #iaa.OneOf([
                        #    iaa.GaussianBlur((0, 0.8)), # blur images with a sigma between 0 and 3.0
                        #]),
                        iaa.GaussianBlur(
                            (0, 0.75)
                        ),  # blur images with a sigma between 0 and 3.0
                        iaa.Sharpen(alpha=(0, 0.5),
                                    lightness=(0.75, 1.5)),  # sharpen images
                        iaa.Emboss(alpha=(0, 0.5),
                                   strength=(0, 1.5)),  # emboss images
                        #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.Dropout(
                            (0.01, 0.05), per_channel=0.5
                        ),  # randomly remove up to 10% of the pixels
                        #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.05), size_percent=(0.02, 0.04), per_channel=0.2),
                        #]),
                        iaa.Invert(0.05,
                                   per_channel=True),  # invert color channels
                        iaa.Add(
                            (-5, 5), per_channel=0.5
                        ),  # change brightness of images (by -10 to 10 of original value)
                        iaa.AddToHueAndSaturation(
                            (-20, 20)),  # change hue and saturation
                        #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, 1.0),
                            per_channel=0.5),  # improve or worsen the contrast
                        iaa.Grayscale(alpha=(0.0, 1.0)),
                    ],
                    random_order=True)
            ],
            random_order=True)
        self.seq = seq
Exemplo n.º 30
0
    def __init__(self,
                 mean=(0.0, 0.0, 0.0, 0.0),
                 std=(1.0, 1.0, 1.0, 1.0),
                 crop_image_size=(224, 224)):
        if isinstance(crop_image_size, int):
            crop_image_size = (crop_image_size, crop_image_size)
        self._mean = mean
        self._std = std
        self.crop_image_size = crop_image_size

        self.seq = iaa.Sequential(
            children=[
                iaa.Sequential(
                    children=[
                        iaa.Fliplr(
                            p=0.5,
                            name="Fliplr"),
                        iaa.Flipud(
                            p=0.5,
                            name="Flipud"),
                        iaa.Sequential(
                            children=[
                                iaa.Affine(
                                    scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
                                    translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)},
                                    rotate=(-45, 45),
                                    shear=(-16, 16),
                                    order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                                    mode="reflect",
                                    name="Affine"),
                                iaa.Sometimes(
                                    p=0.01,
                                    then_list=iaa.PiecewiseAffine(
                                        scale=(0.0, 0.01),
                                        nb_rows=(4, 20),
                                        nb_cols=(4, 20),
                                        order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                                        mode="reflect",
                                        name="PiecewiseAffine"))],
                            random_order=True,
                            name="GeomTransform"),
                        iaa.Sequential(
                            children=[
                                iaa.Sometimes(
                                    p=0.75,
                                    then_list=iaa.Add(
                                        value=(-10, 10),
                                        per_channel=0.5,
                                        name="Brightness")),
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.Emboss(
                                        alpha=(0.0, 0.5),
                                        strength=(0.5, 1.2),
                                        name="Emboss")),
                                iaa.Sometimes(
                                    p=0.1,
                                    then_list=iaa.Sharpen(
                                        alpha=(0.0, 0.5),
                                        lightness=(0.5, 1.2),
                                        name="Sharpen")),
                                iaa.Sometimes(
                                    p=0.25,
                                    then_list=iaa.ContrastNormalization(
                                        alpha=(0.5, 1.5),
                                        per_channel=0.5,
                                        name="ContrastNormalization"))
                            ],
                            random_order=True,
                            name="ColorTransform"),
                        iaa.Sequential(
                            children=[
                                iaa.Sometimes(
                                    p=0.5,
                                    then_list=iaa.AdditiveGaussianNoise(
                                        loc=0,
                                        scale=(0.0, 10.0),
                                        per_channel=0.5,
                                        name="AdditiveGaussianNoise")),
                                iaa.Sometimes(
                                    p=0.1,
                                    then_list=iaa.SaltAndPepper(
                                        p=(0, 0.001),
                                        per_channel=0.5,
                                        name="SaltAndPepper"))],
                            random_order=True,
                            name="Noise"),
                        iaa.OneOf(
                            children=[
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.MedianBlur(
                                        k=3,
                                        name="MedianBlur")),
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.AverageBlur(
                                        k=(2, 4),
                                        name="AverageBlur")),
                                iaa.Sometimes(
                                    p=0.5,
                                    then_list=iaa.GaussianBlur(
                                        sigma=(0.0, 2.0),
                                        name="GaussianBlur"))],
                            name="Blur"),
                    ],
                    random_order=True,
                    name="MainProcess")])