예제 #1
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)))
예제 #2
0
def get_augmenter(name, c_val=255, vertical_flip=True):
    if name:
        alot = lambda aug: iaa.Sometimes(0.75, aug)
        alw = lambda aug: iaa.Sometimes(1, aug)
        sometimes = lambda aug: iaa.Sometimes(0.50, aug)
        few = lambda aug: iaa.Sometimes(0.10, aug)

        if 'rgb' in name:
            seq_rgb = iaa.Sequential([
                iaa.Fliplr(0.50),  # horizontally flip 50% of the images
                iaa.Flipud(0.20),  # vertically flip 50% of the images
                sometimes(iaa.Add((-30, 30))),
                sometimes(iaa.Multiply((0.80, 1.20), per_channel=False)),
                sometimes(iaa.GaussianBlur(sigma=(0, 0.10))),
                few(
                    iaa.CoarseDropout(p=(0.05, 0.15),
                                      size_percent=(0.15, 0.35),
                                      per_channel=True)),
                few(
                    iaa.CoarseDropout(p=(0.05, 0.15),
                                      size_percent=(0.15, 0.35),
                                      per_channel=False)),
                sometimes(iaa.ContrastNormalization((0.75, 1.35))),
                alot(
                    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
                        order=1,  #bilinear interpolation (fast)
                        cval=0,
                        mode=
                        "reflect"  # `edge`, `wrap`, `reflect` or `symmetric`
                        # 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)
                    ))
            ])
            return seq_rgb

        if 'coral' in name:
            seq_rgb = iaa.Sequential([
                iaa.Fliplr(0.50),  # horizontally flip 50% of the images
                iaa.Flipud(0.50),  # vertically flip 50% of the images
                sometimes(iaa.Add((-30, 30))),
                sometimes(iaa.Multiply((0.80, 1.20), per_channel=False)),
                sometimes(iaa.GaussianBlur(sigma=(0, 40))),
                sometimes(iaa.ContrastNormalization((0.75, 1.35))),
                alot(
                    iaa.Affine(
                        scale={
                            "x": (0.7, 1.3),
                            "y": (0.7, 1.3)
                        },
                        # 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
                        order=1,  #bilinear interpolation (fast)
                        cval=0,
                        mode=
                        "reflect"  # `edge`, `wrap`, `reflect` or `symmetric`
                        # 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)
                    ))
            ])
            return seq_rgb

        elif 'segmentation' in name:
            #create one per image. give iamge, label and mask to the pipeling

            value_flip = round(random.random())
            if value_flip > 0.5:
                value_flip = 1
            else:
                value_flip = 0

            value_flip2 = round(random.random())
            if value_flip2 > 0.5:
                value_flip2 = 1
            else:
                value_flip2 = 0

            value_add = random.uniform(-15, 15)
            value_Multiply = random.uniform(0.9, 1.1)
            value_GaussianBlur = random.uniform(0.0, 0.40)
            ContrastNormalization = random.uniform(0.79, 1.35)
            scale = random.uniform(0.8, 1.3)
            value_x2 = random.uniform(-0.30, 0.30)
            value_y2 = random.uniform(-0.20, 0.20)
            val_rotate = random.uniform(-13, 13)
            '''
            sometimes(iaa.Add((value_add, value_add))),
            sometimes(iaa.Multiply((value_Multiply, value_Multiply), per_channel=False)),
            sometimes(iaa.GaussianBlur(sigma=(value_GaussianBlur, value_GaussianBlur))),
            few(iaa.CoarseDropout(p=value_CoarseDropout, size_percent=(value_CoarseDropout3, value_CoarseDropout3), per_channel=True)),
            few(iaa.CoarseDropout(p=value_CoarseDropout2, size_percent=(value_CoarseDropout4, value_CoarseDropout4), per_channel=False)),
            sometimes(iaa.ContrastNormalization((ContrastNormalization, ContrastNormalization))),
            '''
            #uniform(-30, 30)

            seq_image = iaa.Sequential([
                iaa.Fliplr(value_flip),  # horizontally flip 50% of the images
                # iaa.Flipud(value_flip2),  # vertically flip 50% of the images
                iaa.Affine(
                    scale={
                        "x": (scale),
                        "y": (scale)
                    },
                    # scale images to 80-120% of their size, individually per axis
                    translate_percent={
                        "x": (value_x2),
                        "y": (value_y2)
                    },
                    # translate by -20 to +20 percent (per axis)
                    rotate=(val_rotate),  # rotate by -45 to +45 degrees
                    order=1,  #bilinear interpolation (fast)
                    cval=c_val,
                    mode="reflect",

                    # `edge`, `wrap`, `reflect` or `symmetric`
                    # cval=c_val,  # 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)
                )
            ])

            seq_image2 = iaa.Sequential([
                #sometimes(iaa.Add((value_add, value_add))),
                #sometimes(iaa.Multiply((value_Multiply, value_Multiply), per_channel=False)),
                sometimes(
                    iaa.GaussianBlur(sigma=(value_GaussianBlur,
                                            value_GaussianBlur))),
                sometimes(
                    iaa.ContrastNormalization(
                        (ContrastNormalization, ContrastNormalization)))
            ])

            seq_label = iaa.Sequential([
                iaa.Fliplr(value_flip),  # horizontally flip 50% of the images
                # iaa.Flipud(value_flip2),  # vertically flip 50% of the images
                iaa.Affine(
                    scale={
                        "x": (scale),
                        "y": (scale)
                    },
                    # scale images to 80-120% of their size, individually per axis
                    translate_percent={
                        "x": (value_x2),
                        "y": (value_y2)
                    },
                    # translate by -20 to +20 percent (per axis)
                    rotate=(val_rotate),  # rotate by -45 to +45 degrees
                    order=0,  #bilinear interpolation (fast)
                    cval=c_val,
                    mode="constant"  # `edge`, `wrap`, `reflect` or `symmetric`
                    # 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)
                )
            ])

            seq_mask = iaa.Sequential([
                iaa.Fliplr(value_flip),  # horizontally flip 50% of the images
                # iaa.Flipud(value_flip2),  # vertically flip 50% of the images
                iaa.Affine(
                    scale={
                        "x": (scale),
                        "y": (scale)
                    },
                    # scale images to 80-120% of their size, individually per axis
                    translate_percent={
                        "x": (value_x2),
                        "y": (value_y2)
                    },
                    # translate by -20 to +20 percent (per axis)
                    rotate=(val_rotate),  # rotate by -45 to +45 degrees
                    order=0,  #bilinear interpolation (fast)
                    cval=0,
                    mode="constant"  # `edge`, `wrap`, `reflect` or `symmetric`
                    # cval=c_val,  # 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)
                )
            ])

            return seq_image2, seq_image, seq_label, seq_mask

        elif 'caltech' in name:
            seq_multi = iaa.Sequential([
                iaa.Fliplr(0.25),  # horizontally flip 50% of the images
                iaa.Flipud(0.25),  # horizontally flip 50% of the images
                sometimes(
                    iaa.Affine(
                        # scale images to 80-120% of their size, individually per axis
                        # translate by -20 to +20 percent (per axis)
                        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.20, 0.2),
                            "y": (-0.2, 0.2)
                        },
                        # translate by -20 to +20 percent (per axis)
                        rotate=(-30, 30),  # rotate by -45 to +45 degrees
                        order=0,  # use nearest neighbour
                        cval=127.5,
                        mode="constant"))
            ])
            return seq_multi
        else:

            seq_multi = iaa.Sequential([
                sometimes(
                    iaa.Affine(
                        # scale images to 80-120% of their size, individually per axis
                        # translate by -20 to +20 percent (per axis)
                        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.20, 0.2),
                            "y": (-0.2, 0.2)
                        },
                        # translate by -20 to +20 percent (per axis)
                        rotate=(-45, 45),  # rotate by -45 to +45 degrees
                        order=0,  # use nearest neighbour
                        cval=127.5,
                        mode="constant"))
            ])
            return seq_multi
    else:
        return None
예제 #3
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.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 __init__(self, path='/'):
        configMain.__init__(self)

        st = lambda aug: iaa.Sometimes(0.2, aug)
        oc = lambda aug: iaa.Sometimes(0.1, aug)
        rl = lambda aug: iaa.Sometimes(0.05, aug)
        self.augment = [
            iaa.Sequential(
                [
                    rl(iaa.GaussianBlur(
                        (0,
                         1.3))),  # blur images with a sigma between 0 and 1.5
                    rl(
                        iaa.AdditiveGaussianNoise(
                            loc=0, scale=(0.0, 0.05),
                            per_channel=0.5)),  # add gaussian noise to images
                    rl(iaa.Dropout((0.0, 0.10), per_channel=0.5)
                       ),  # randomly remove up to X% of the pixels
                    oc(
                        iaa.Add((-20, 20), per_channel=0.5)
                    ),  # change brightness of images (by -X to Y of original value)
                    st(iaa.Multiply(
                        (0.25, 2.5), per_channel=0.2
                    )),  # change brightness of images (X-Y% of original value)
                    rl(iaa.ContrastNormalization(
                        (0.5, 1.5),
                        per_channel=0.5)),  # improve or worsen the contrast
                    rl(iaa.Grayscale((0.0, 1))),  # put grayscale
                ],
                random_order=True  # do all of the above in random order
            ),
            iaa.Sequential(
                [  # AUGMENTATION Labels
                    rl(iaa.Dropout(
                        (0.0,
                         0.03))),  # randomly remove up to X% of the pixels
                    rl(iaa.CoarseDropout((0.0, 0.03), size_percent=(
                        0.2, 0.3))),  # randomly remove up to X% of the pixels
                ],
                random_order=True  # do all of the above in random order
            )
        ]

        # self.augment = [None]

        # there are files with data, 200 images each, and here we select which ones to use

        self.dataset_name = 'Carla'

        train_path = os.path.join(path, 'RC28_wpz_M')
        val_path = os.path.join(path, 'RC025Val')
        print(train_path)

        self.train_db_path = [
            os.path.join(train_path, f)
            for f in glob.glob1(train_path, "data_*.h5")
        ]
        self.val_db_path = [
            os.path.join(val_path, f)
            for f in glob.glob1(val_path, "data_*.h5")
        ]

        # Speed Divide Factor

        # TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time.
        self.speed_factor = 40.0  # In KM/H FOR GTA it should be maximun 30.0

        # The division is made by three diferent data kinds
        # in every mini-batch there will be equal number of samples with labels from each group
        # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2
        self.labels_per_division = [[0, 2, 5], [3], [4]]

        self.dataset_names = ['targets']

        self.queue_capacity = 20 * self.batch_size
예제 #5
0
    def __init__(self):

        configMain.__init__(self)

        st = lambda aug: iaa.Sometimes(0.4, aug)
        oc = lambda aug: iaa.Sometimes(0.3, aug)
        rl = lambda aug: iaa.Sometimes(0.09, aug)
        self.augment = iaa.Sequential(
            [
                rl(iaa.GaussianBlur(
                    (0, 1.5))),  # blur images with a sigma between 0 and 1.5
                rl(
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05),
                        per_channel=0.5)),  # add gaussian noise to images
                rl(iaa.Dropout((0.0, 0.03), per_channel=0.5)
                   ),  # randomly remove up to X% of the pixels
                oc(
                    iaa.Add((-20, 30), per_channel=0.5)
                ),  # change brightness of images (by -X to Y of original value)
                st(iaa.Multiply((0.25, 2.5), per_channel=0.2)
                   ),  # change brightness of images (X-Y% of original value)
                rl(iaa.ContrastNormalization(
                    (0.5, 1.5),
                    per_channel=0.5)),  # improve or worsen the contrast
                rl(iaa.Grayscale((0.0, 1))),  # put grayscale
            ],
            random_order=True  # do all of the above in random order
        )

        # there are files with data, 200 images each, and here we select which ones to use
        self.valid_pos_train = range(0, 993)

        self.valid_pos_val = range(
            0, 101)  # validation data is in a different folder

        self.save_data_stats = 'data_stats/'  # the 'data_stats/path' file points to the location of the data

        with open(self.save_data_stats + 'path', 'r') as f:
            path = f.read()

        self.train_db_path = [
            path[:-1] + 'SeqTrain/data_' + str(i).zfill(5) + '.h5'
            for i in self.valid_pos_train
        ]
        self.val_db_path = [
            path[:-1] + 'SeqVal/data_' + str(i).zfill(5) + '.h5'
            for i in self.valid_pos_val
        ]  #+ ['/media/adas/DISCODADOS/DeepDriveData/train_'+str(i).zfill(4)+'_M'+'.h5' for i in valid_pos_train]

        # When using data with noise, remove the recording during the first half of the noise impulse
        # TODO Felipe: change to noise percentage.
        self.remove_noise = False

        # Speed Divide Factor

        #TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time.
        self.speed_factor = 50.0  # In KM/H

        # The division is made by three diferent data kinds
        # in every mini-batch there will be equal number of samples with labels from each group
        # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2
        self.labels_per_division = [[0, 2, 5], [3], [4]]

        self.dataset_names = ['targets']

        self.queue_capacity = 20 * self.batch_size

        # TODO NOT IMPLEMENTED Felipe: True/False switches to turn data balancing on or off
        self.balances_val = True
        self.balances_train = True
예제 #6
0
                                   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)
예제 #7
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)
예제 #8
0
    def __init__(self,
                 path="../mocap_syndata/multiview_data",
                 num_camera=4,
                 load_joints=17,
                 invalid_joints=(),
                 bbox=None,
                 image_shape=[384, 384],
                 train=False,
                 test=False,
                 with_aug=False):
        """
        invalid_joints: tuple of indices for invalid joints; associated joints will not be used in evaluation.
        bbox: [upper_left_x, upper_left_y, lower_right-x, lower_right_y].
        image_size: [width, height].
        with_aug: use or not image augmentation.
        """
        assert train or test

        train_subj = ['S0map', 'S0rand', 'S0real', \
                      'S1map', 'S1rand', 'S1real', \
                      'S2map', 'S2rand', 'S2real', \
                      'S3map', 'S3rand', 'S3real']

        test_subj = ['S4map', 'S4rand', 'S4real', \
                     'S5map', 'S5rand', 'S5real']

        if train:
            self.subj = train_subj
        elif test:
            self.subj = test_subj

        self.basepath = path
        # self.subj = sorted(os.listdir(self.basepath))
        self.framelist = []
        self.camera_names = []
        self.cameras = {}
        self.len = 0

        self.invalid_jnts = () if invalid_joints is None else invalid_joints

        self.image_shape = image_shape

        # torchvision transforms
        # self.transform = tv.transforms.Compose([
        #     tv.transforms.ToTensor(),
        #     tv.transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)
        #     ])

        # joint names
        self.joints_name = datasets_utils.get_joints_name(load_joints)

        self.num_jnts = len(self.joints_name)
        # assert self.num_jnts == load_joints

        for camera_idx in range(num_camera):
            self.camera_names.append("%04d" % camera_idx)

        for (subj_idx, subj_name) in enumerate(self.subj):
            subj_path = os.path.join(self.basepath, subj_name)
            anims = sorted(os.listdir(subj_path))  # animation list
            cams_subj = {
            }  # dict to store camera params for each subject, where keys are anim indices
            for anim_name in anims:
                anim_idx = int(anim_name.split('_')[1])  # animation index
                anim_path = os.path.join(subj_path, anim_name)
                files = sorted(os.listdir(anim_path))
                cams = {
                }  # dict to store camera params for each subject with each animations
                for f in files:
                    f_path = os.path.join(anim_path, f)
                    if not os.path.isdir(f_path):
                        f_fn = os.path.splitext(f)[
                            0]  # filename without extension
                        if ('camera' in f_fn) and (f_fn.split('_')[1]
                                                   in self.camera_names):
                            # load cameras
                            camera_idx = self.camera_names.index(
                                f_fn.split('_')[1])
                            cam = datasets_utils.load_camera(f_path)
                            cams[camera_idx] = cam
                        elif 'skeleton' in f_fn:
                            # record frame list
                            frame_idx = f_fn.split('_')[1]
                            self.framelist.append(
                                [subj_idx, anim_idx,
                                 int(frame_idx), bbox])
                            self.len += 1

                    cams_subj[anim_idx] = cams
                self.cameras[subj_idx] = cams_subj

        # augmentation
        self.aug = None
        if train and with_aug:
            sometimes = lambda aug: iaa.Sometimes(0.5, aug)
            self.aug = iaa.Sequential(
                [
                    sometimes(
                        iaa.Affine(
                            scale={
                                "x": (0.75, 1.25),
                                "y": (0.75, 1.25)
                            },  # scale to 75-125% of image height/width (each axis independently)
                            translate_percent={
                                "x": (-0.05, 0.05),
                                "y": (-0.05, 0.05)
                            },  # translate by -5 to +5 relative to height/width (per axis)
                            rotate=(-30, 30),  # rotate by -30 to +30 degrees
                            shear=(-20, 20),  # shear by -20 to +20 degrees
                            order=[
                                0, 1
                            ],  # use nearest neighbour or bilinear interpolation (fast)
                            cval=(
                                0, 255
                            ),  # if the mode is constant, then use a random brightness for the newly created pixels
                            mode=
                            'constant'  # use any available mode to fill newly created pixels, see API or scikit-image for which modes are available
                        )),
                    sometimes(
                        iaa.AdditiveGaussianNoise(scale=0.5 * 2,
                                                  per_channel=0.5)),
                    sometimes(iaa.GaussianBlur(sigma=(1.0, 5.0))),
                    sometimes(
                        iaa.ContrastNormalization(
                            (0.8, 1.25),
                            per_channel=0.5))  # improve or worsen the contrast
                ],
                random_order=True)
def main():
    IN_JSON = 'dataset_descriptions/iamondb_generated_dates_10k.json'
    OUT_JSON = 'dataset_descriptions/iamondb_generated_dates_resized_aug2_10k.json'
    IMG_IN_DIR = 'datasets/tars'
    IMG_OUT_DIR = 'datasets/'
    SCALE_FACTOR = 2  # How much bigger the new dataset should be
    SAVE_ORIGINAL = False
    TARGET_DIMENSIONS = (216, 64)

    new_image_infos = []

    with open(IN_JSON, 'r') as json_file:
        json_images = json.load(json_file)

    image_infos = copy.deepcopy(json_images)
    for idx in range(0, len(image_infos)):
        image_infos[idx]['path'] = os.path.join(
            IMG_IN_DIR, os.path.basename(image_infos[idx]['path']))

    seq = iaa.Sequential(
        [
            iaa.Sometimes(0.5, iaa.GaussianBlur(
                sigma=(0, 0.5))),  # Small blur on half of the images
            iaa.ContrastNormalization((0.75, 1.5)),  # Change in contrast
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255),
                per_channel=0.5),  # Gaussian Noise can change colour
            iaa.Multiply((0.8, 1.2), per_channel=0.2)  #,  # brighter/darker
            # iaa.Affine(
            #     rotate=(-10, 10),
            #     shear=(-20, 20),
            #     fit_output=True,
            # ),
            # iaa.PiecewiseAffine(scale=(0.01, 0.05), mode='edge')
        ],
        random_order=True)  # apply augmenters in random order

    images = np.array(
        [np.array(Image.open(img['path'])) for img in image_infos])

    images = np.repeat(images, SCALE_FACTOR, axis=0)
    image_infos = np.repeat(np.array(image_infos), SCALE_FACTOR, axis=0)

    # Make batches out of single images because processing multiple images at once leads to errors because the resulting
    # images have different shapes and therefore can't be organised in a numpy array -> leads to an internal error
    for idx, img in enumerate(images):
        old_filename = os.path.splitext(
            os.path.basename(image_infos[idx]['path']))[0]
        new_filename = old_filename + '-' + str(idx % SCALE_FACTOR +
                                                1) + '.png'
        new_img_path = os.path.join(IMG_OUT_DIR, new_filename)

        if SAVE_ORIGINAL and idx % SCALE_FACTOR == 0:
            with open(os.path.join(IMG_OUT_DIR, old_filename + '.png'),
                      'wb+') as orig_img_file:
                resize_img(Image.fromarray(img),
                           TARGET_DIMENSIONS).save(orig_img_file, format='PNG')

        padded_img = resize_img(Image.fromarray(img),
                                TARGET_DIMENSIONS,
                                padding_color=255)
        img_aug = seq(images=np.expand_dims(np.asarray(padded_img), axis=0))
        pil_img = Image.fromarray(np.squeeze(img_aug)).convert('L')

        with open(new_img_path, 'wb+') as img_file:
            pil_img.save(img_file, format='PNG')

        new_image_infos.append({
            "string": image_infos[idx]['string'],
            "type": image_infos[idx]['type'],
            "path": new_img_path
        })

    with open(OUT_JSON, 'w') as out_json:
        if SAVE_ORIGINAL:
            assert os.path.split(json_images[0]['path'])[0] == os.path.split(new_image_infos[0]['path'])[0],\
                "Original path differs from new path"
            json.dump(json_images + new_image_infos,
                      out_json,
                      ensure_ascii=False,
                      indent=4)
        else:
            json.dump(new_image_infos, out_json, ensure_ascii=False, indent=4)

    print(
        f'Created {len(new_image_infos)} new images (original: {len(json_images)})'
    )
예제 #10
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
예제 #11
0
def soft(image_iteration):

    iteration = image_iteration / (120 * 1.5)
    frequency_factor = 0.05 + float(iteration) / 1200000.0
    color_factor = float(iteration) / 1200000.0
    dropout_factor = 0.198667 + (0.03856658 -
                                 0.198667) / (1 +
                                              (iteration / 196416.6)**1.863486)

    blur_factor = 0.5 + (0.5 * iteration / 120000.0)

    add_factor = 10 + 10 * iteration / 170000.0

    multiply_factor_pos = 1 + (2.5 * iteration / 800000.0)
    multiply_factor_neg = 1 - (0.91 * iteration / 800000.0)

    contrast_factor_pos = 1 + (0.5 * iteration / 800000.0)
    contrast_factor_neg = 1 - (0.5 * iteration / 800000.0)

    #print ('iteration',iteration,'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor,
    #    multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg)

    augmenter = iaa.Sequential(
        [
            iaa.Sometimes(frequency_factor, iaa.GaussianBlur(
                (0, blur_factor))),
            # blur images with a sigma between 0 and 1.5
            iaa.Sometimes(
                frequency_factor,
                iaa.AdditiveGaussianNoise(loc=0,
                                          scale=(0.0, dropout_factor),
                                          per_channel=color_factor)),
            # add gaussian noise to images
            iaa.Sometimes(
                frequency_factor,
                iaa.CoarseDropout((0.0, dropout_factor),
                                  size_percent=(0.08, 0.2),
                                  per_channel=color_factor)),
            # randomly remove up to X% of the pixels
            iaa.Sometimes(
                frequency_factor,
                iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)),
            # randomly remove up to X% of the pixels
            iaa.Sometimes(
                frequency_factor,
                iaa.Add((-add_factor, add_factor), per_channel=color_factor)),
            # change brightness of images (by -X to Y of original value)
            iaa.Sometimes(
                frequency_factor,
                iaa.Multiply((multiply_factor_neg, multiply_factor_pos),
                             per_channel=color_factor)),
            # change brightness of images (X-Y% of original value)
            iaa.Sometimes(
                frequency_factor,
                iaa.ContrastNormalization(
                    (contrast_factor_neg, contrast_factor_pos),
                    per_channel=color_factor)),
            # improve or worsen the contrast
            iaa.Sometimes(frequency_factor, iaa.Grayscale(
                (0.0, 1))),  # put grayscale
        ],
        random_order=True  # do all of the above in random order
    )

    return augmenter
예제 #12
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,)
예제 #13
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])
예제 #14
0
def example_heavy_augmentations():
    print("Example: Heavy Augmentations")
    import imgaug as ia
    from imgaug import augmenters as iaa

    # random example images
    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

    # 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.
    st = 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_.
    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5),  # vertically flip 50% of all images
            st(iaa.Crop(
                percent=(0,
                         0.1))),  # crop images by 0-10% of their height/width
            st(iaa.GaussianBlur(
                (0, 3.0))),  # blur images with a sigma between 0 and 3.0
            st(
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255),
                    per_channel=0.5)),  # add gaussian noise to images
            st(iaa.Dropout(
                (0.0, 0.1),
                per_channel=0.5)),  # randomly remove up to 10% of the pixels
            st(iaa.Add(
                (-10, 10), per_channel=0.5
            )),  # change brightness of images (by -10 to 10 of original value)
            st(iaa.Multiply((0.5, 1.5), per_channel=0.5)
               ),  # change brightness of images (50-150% of original value)
            st(iaa.ContrastNormalization(
                (0.5, 2.0),
                per_channel=0.5)),  # improve or worsen the contrast
            st(iaa.Grayscale((0.0, 1.0))),  # blend with grayscale image
            st(
                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_px={
                        "x": (-16, 16),
                        "y": (-16, 16)
                    },  # translate by -16 to +16 pixels (per axis)
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    order=[
                        0,
                        1
                    ],  # use scikit-image's interpolation orders 0 (nearest neighbour) and 1 (bilinear)
                    cval=(
                        0, 1.0
                    ),  # if mode is constant, use a cval between 0 and 1.0
                    mode=ia.
                    ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
               )  # apply elastic transformations with random strengths
        ],
        random_order=True  # do all of the above in random order
    )

    images_aug = seq.augment_images(images)

    # -----
    # Make sure that the example really does something
    assert not np.array_equal(images, images_aug)
예제 #15
0
 def getSeq(self):
     self.sometimes = lambda aug: iaa.Sometimes(0.5, aug)
     self.seq = iaa.Sequential(
         [
             # apply the following augmenters to most images
             # iaa.Fliplr(0.5), # horizontally flip 50% of all images
             # iaa.Flipud(0.5), # vertically flip 20% of all images
             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)),
                 ],
                 random_order=True)
         ],
         random_order=True)
예제 #16
0
def gen_batch_function(data_folder, image_shape, train=True):
    """
    Generate function to create batches of training data
    :param data_folder: Path to folder that contains all the datasets
    :param image_shape: Tuple - Shape of image
    :return:
    """
    _train = train
    seq = iaa.Sequential([
        iaa.Fliplr(0.5), # horizontal flips
        iaa.Crop(percent=(0, 0.1)), # random crops
        # Small gaussian blur with random sigma between 0 and 0.5.
        # But we only blur about 50% of all images.
        # iaa.Sometimes(0.5,
        #     iaa.GaussianBlur(sigma=(0, 0.5))
        # ),
        # Strengthen or weaken the contrast in each image.
        iaa.ContrastNormalization((0.75, 1.5)),
        # Add gaussian noise.
        # For 50% of all images, we sample the noise once per pixel.
        # For the other 50% of all images, we sample the noise per pixel AND
        # channel. This can change the color (not only brightness) of the
        # pixels.
        # iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
        # Make some images brighter and some darker.
        # In 20% of all cases, we sample the multiplier once per channel,
        # which can end up changing the color of the images.
        # iaa.Multiply((0.8, 1.2), per_channel=0.2),
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.
        iaa.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=(-15, 15)
           # shear=(-8, 8)
        )
    ], random_order=True) # apply augmenters in random order

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

        random.shuffle(image_paths)
        for batch_i in range(0, len(image_paths), batch_size):
            images = []
            gt_images = []
            for image_file in image_paths[batch_i:batch_i+batch_size]:
                gt_image_file = label_paths[os.path.basename(image_file)]
                image = scipy.misc.imresize(scipy.misc.imread(image_file), image_shape)
                gt_image = scipy.misc.imresize(scipy.misc.imread(gt_image_file), image_shape)
                if _train:
                    seq_det = seq.to_deterministic()
                    image = seq_det.augment_image(image)
                    gt_image = seq_det.augment_image(gt_image)
                gt_bg = np.all(gt_image == background_color, axis=2)
                gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
                gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)
                images.append(image)
                gt_images.append(gt_image)
            yield np.array(images), np.array(gt_images)
    return get_batches_fn
예제 #17
0
def train(config):
    # Just in case if we need a on-the-fly augmentator
    augmentator = iaa.SomeOf(
        (0, None),
        [
            iaa.Add((-40, 40)),
            iaa.Affine(scale=(0.7, 1.3),
                       rotate=iap.Choice([0, 90, 180, -90]),
                       mode='reflect'),
            iaa.Fliplr(0.25),  # horizontally flip 25% of the images
            iaa.Flipud(0.25),
            # Strengthen or weaken the contrast in each image.
            iaa.ContrastNormalization((0.8, 1.2)),
            iaa.GaussianBlur(
                sigma=(0, 0.8)),  # blur images with a sigma of 0 to 3.0
        ])

    # set a default class mapping:
    if not config['class_mapping']:
        config['class_mapping'] = {k: k for k in config['classes']}

    preprocess_input = getattr(models, config['model'] + '_preprocess_input')

    train_images, train_labels = get_patches(config['training_folds'],
                                             config['class_mapping'])
    train_loader = ImageLoader(
        images=train_images,
        labels=train_labels,
        augmentator=augmentator if config['augmentation'] else None,
        balance=config['balance_training_data'],
        preprocess_input=preprocess_input,
        classes=config['classes'],
        label_encoding='binary',
        batch_size=config['batch_size'],
    )
    val_images, val_labels = get_patches(config['validation_folds'],
                                         config['class_mapping'])
    val_loader = ImageLoader(
        images=val_images,
        labels=val_labels,
        preprocess_input=preprocess_input,
        classes=config['classes'],
        label_encoding='binary',
        batch_size=config['batch_size'],
    )

    print('Training samples:', len(train_images))
    print('Validation samples:', len(val_images))

    os.makedirs(config['results_dir'], exist_ok=True)

    # Load the model architecture:
    print("Load model...")
    model_loader = getattr(models, config['model'])
    model = model_loader(**config['model_options'])

    optimizer = getattr(optimizers,
                        config['optimizer'])(**config['optimizer_options'])

    if len(config['classes']) == 2:
        model.compile(loss='binary_crossentropy',
                      metrics=['binary_accuracy'],
                      optimizer=optimizer)
    else:
        model.compile(loss='categorical_crossentropy',
                      metrics=['categorical_accuracy'],
                      optimizer=optimizer)
    print(model.summary())

    model_dir = join(config['results_dir'], 'models', config['name'])
    os.makedirs(model_dir, exist_ok=True)
    with open(join(model_dir, 'config.yml'), 'w') as outfile:
        yaml.dump(config, outfile, default_flow_style=False)
    with open(join(model_dir, 'model.json'), 'w') as outfile:
        json.dump(model.to_json(), outfile)

    callback_list = [
        callbacks.EarlyStopping(
            monitor='val_loss',
            patience=15,
        ),
        callbacks.TensorBoard(
            log_dir=join(config['results_dir'], 'tb_logs', config['name']),
            histogram_freq=0,
            write_graph=False,
            write_images=False,
        ),
        callbacks.ReduceLROnPlateau(monitor='val_loss',
                                    factor=0.5,
                                    patience=5,
                                    verbose=1,
                                    mode='auto',
                                    min_delta=0.00002,
                                    cooldown=0,
                                    min_lr=0.00002),
        callbacks.ModelCheckpoint(join(model_dir, 'checkpoint'),
                                  monitor='val_loss',
                                  verbose=0,
                                  save_best_only=True,
                                  save_weights_only=True,
                                  mode='auto',
                                  save_freq='epoch'),
        AdvancedMetrics(logdir=join(config['results_dir'], 'tb_logs',
                                    config['name']),
                        plotdir=join(config['results_dir'], 'plots',
                                     config['name']),
                        data_generator=val_loader,
                        classes=config['classes'])
    ]

    print("Train model...")
    model.fit_generator(
        train_loader,
        steps_per_epoch=len(train_loader)
        if config['training_steps'] is None else config['training_steps'],
        validation_data=val_loader,
        validation_steps=len(val_loader)
        if config['validation_steps'] is None else config['validation_steps'],
        epochs=config['training_epochs'],
        use_multiprocessing=True,
        workers=2,
        callbacks=callback_list,
        #     class_weight=class_weights,
        verbose=config['verbose'],
    )
예제 #18
0
FILENAME_CSV_TRAIN = os.path.join(os.path.abspath('..'),
              'datafiles', DATFILE_TYPE, TRAIN_TYPE + '_split_patid_train.csv')
FILENAME_CSV_VALID = os.path.join(os.path.abspath('..'),
              'datafiles', DATFILE_TYPE, TRAIN_TYPE + '_split_patid_valid.csv')
FILENAME_CSV_TEST = os.path.join(os.path.abspath('..'),
              'datafiles', DATFILE_TYPE, TRAIN_TYPE + '_split_patid_test.csv')
MODEL_SAVE_BASEDIR = os.path.join('/media/ubuntu/data1/tmp5/Plus_step_two_2020_3_14/', TRAIN_TYPE)

from imgaug import augmenters as iaa
sometimes = lambda aug: iaa.Sometimes(0.96, aug)
IMGAUG_TRAIN_SEQ = iaa.Sequential([
    # iaa.CropAndPad(percent=(-0.04, 0.04)),
    iaa.Fliplr(0.5),  # horizontally flip 50% of the images
    iaa.Flipud(0.2),  # horizontally flip 50% of the images

    sometimes(iaa.ContrastNormalization((0.9, 1.1))),
    iaa.Sometimes(0.9, iaa.Add((-6, 6))),
    sometimes(iaa.Affine(
        scale=(0.98, 1.02),
        translate_percent={"x": (-0.06, 0.06), "y": (-0.06, 0.06)},
        rotate=(-15, 15),  # rotate by -10 to +10 degrees
    )),
])


'''
3212
0 2560
1 652
381
0 296
예제 #19
0
    def __init__(self):

        configMain.__init__(self)

        st = lambda aug: iaa.Sometimes(0.4, aug)
        oc = lambda aug: iaa.Sometimes(0.3, aug)
        rl = lambda aug: iaa.Sometimes(0.09, aug)
        self.augment = iaa.Sequential(
            [
                rl(iaa.GaussianBlur(
                    (0, 1.5))),  # blur images with a sigma between 0 and 1.5
                rl(
                    iaa.AdditiveGaussianNoise(
                        loc=0, scale=(0.0, 0.05),
                        per_channel=0.5)),  # add gaussian noise to images
                oc(iaa.Dropout((0.0, 0.10), per_channel=0.5)
                   ),  # randomly remove up to X% of the pixels
                oc(
                    iaa.CoarseDropout(
                        (0.0, 0.10), size_percent=(0.08, 0.2), per_channel=0.5)
                ),  # randomly remove up to X% of the pixels
                oc(
                    iaa.Add((-40, 40), per_channel=0.5)
                ),  # change brightness of images (by -X to Y of original value)
                st(iaa.Multiply((0.10, 2.5), per_channel=0.2)
                   ),  # change brightness of images (X-Y% of original value)
                rl(iaa.ContrastNormalization(
                    (0.5, 1.5),
                    per_channel=0.5)),  # improve or worsen the contrast
                rl(iaa.Grayscale((0.0, 1))),  # put grayscale
            ],
            random_order=True  # do all of the above in random order
        )
        self.augment_labels = False

        # there are files with data, 200 images each, and here we select which ones to use

        #5self.dataset_name = 'Carla'
        #with open(os.path.join(self.save_data_stats, 'path'),'r') as f:
        #	path = f.read().strip()

        path = '../ElektraData4'

        train_path = os.path.join(path, 'SeqTrain')
        val_path = os.path.join(path, 'SeqVal')

        print train_path, val_path

        self.train_db_path = [
            os.path.join(train_path, f)
            for f in glob.glob1(train_path, "data_*.h5")
        ]
        self.val_db_path = [
            os.path.join(val_path, f)
            for f in glob.glob1(val_path, "data_*.h5")
        ]

        # When using data with noise, remove the recording during the first half of the noise impulse
        # TODO Felipe: change to noise percentage.
        self.remove_noise = False

        # Speed Divide Factor

        #TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time.
        self.speed_factor = 1.0  # In KM/H FOR GTA it should be maximun 30.0

        # The division is made by three diferent data kinds
        # in every mini-batch there will be equal number of samples with labels from each group
        # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2
        self.labels_per_division = [[2], [2], [2]]

        self.dataset_names = ['targets']

        self.queue_capacity = 20 * self.batch_size

        # TODO NOT IMPLEMENTED Felipe: True/False switches to turn data balancing on or off
        self.balances_val = True
        self.balances_train = True
        self.saturated_factor = True
예제 #20
0
def main():
    augmenters = [
        iaa.Identity(name="Identity"),
        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.Grayscale((0.0, 1.0), name="Grayscale"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1),
                                  name="AdditiveGaussianNoise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation"),
        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=0,
                   cval=(0, 255),
                   mode="constant",
                   name="AffineOrder0ModeConstant")
    ]

    for order in [0, 1]:
        augmenters.append(
            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=order,
                       cval=(0, 255),
                       mode=ia.ALL,
                       name="AffineOrder%d" % (order, )))

    augmenters.append(
        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="AffineAll"))

    kps = []
    for _ in sm.xrange(20):
        x = random.randint(0, 31)
        y = random.randint(0, 31)
        kps.append(ia.Keypoint(x=x, y=y))
    kps = ia.KeypointsOnImage(kps, shape=(32, 32, 3))
    small_keypoints_one = kps.on((4, 4, 3))
    medium_keypoints_one = kps.on((32, 32, 3))
    large_keypoints_one = kps.on((256, 256, 3))
    small_keypoints = [small_keypoints_one.deepcopy() for _ in sm.xrange(16)]
    medium_keypoints = [medium_keypoints_one.deepcopy() for _ in sm.xrange(16)]
    large_keypoints = [large_keypoints_one.deepcopy() for _ in sm.xrange(16)]

    small_images = np.random.randint(0, 255, (16, 4, 4, 3)).astype(np.uint8)
    medium_images = np.random.randint(0, 255, (16, 32, 32, 3)).astype(np.uint8)
    large_images = np.random.randint(0, 255,
                                     (16, 256, 256, 3)).astype(np.uint8)

    print("---------------------------")
    print("Keypoints")
    print("---------------------------")
    for augmenter in augmenters:
        print("[Augmenter: %s]" % (augmenter.name, ))
        for keypoints in [small_keypoints, medium_keypoints, large_keypoints]:
            times = []
            for i in sm.xrange(100):
                time_start = time.time()
                _img_aug = augmenter.augment_keypoints(keypoints)
                time_end = time.time()
                times.append(time_end - time_start)
            times = np.array(times)
            img_str = "{:20s}".format(keypoints[0].shape)
            print("%s | SUM %.5fs | PER ITER avg %.5fs, min %.5fs, max %.5fs" %
                  (img_str, float(np.sum(times)), np.average(times),
                   np.min(times), np.max(times)))

    print("---------------------------")
    print("Images")
    print("---------------------------")
    for augmenter in augmenters:
        print("[Augmenter: %s]" % (augmenter.name, ))
        for images in [small_images, medium_images, large_images]:
            times = []
            for i in sm.xrange(100):
                time_start = time.time()
                _img_aug = augmenter.augment_images(images)
                time_end = time.time()
                times.append(time_end - time_start)
            times = np.array(times)
            img_str = "{:20s}".format(images.shape)
            print("%s | SUM %.5fs | PER ITER avg %.5fs, min %.5fs, max %.5fs" %
                  (img_str, float(np.sum(times)), np.average(times),
                   np.min(times), np.max(times)))
예제 #21
0
파일: train.py 프로젝트: itdxer/deeplab
from data import (read_data, subtract_channel_mean, crop_image,
                  add_zero_paddings, make_annotation_one_hot_encoded,
                  TRAIN_SET, VALIDATION_SET)
from model import create_deeplab_model, download_resnet50_weights
from utils import get_confusion_matrix, segmentation_metrics

parser = argparse.ArgumentParser()
parser.add_argument('--storage-folder', '-s', required=True)
parser.add_argument('--image-size', '-i', type=int, default=513)
parser.add_argument('--batch-size', '-b', type=int, default=10)
parser.add_argument('--epochs', '-e', type=int, default=30)

image_augmentation = iaa.Sequential([
    iaa.Add((-32, 32)),  # change brightness
    iaa.AddToHueAndSaturation((-20, 20)),  # change hue and saturation
    iaa.ContrastNormalization((0.5, 1.5)),  # improve or worsen the contrast
])
affine = iaa.Affine(scale=(0.5, 2))  # zoom-in and zoom-out on image
flip = iaa.Fliplr(0.5)  # left-right flip with 50% probability


def create_data_iterator(datapath,
                         crop_size,
                         batch_size,
                         use_augmentation=True):
    images, annotations = read_data(datapath)
    n_samples = len(images)

    def iter_batches():
        indices = np.arange(n_samples)
        shuffle(indices)  # randomize the order
예제 #22
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)}
print(class_ids)

# Image Augmentation. Try finetuning some variables to custom values
# Image augmentation (light but constant)
augmentation = iaa.Sequential([
    iaa.OneOf([  ## rotate
        iaa.Affine(rotate=0),
        iaa.Affine(rotate=90),
        iaa.Affine(rotate=180),
        iaa.Affine(rotate=270),
    ]),
    iaa.Fliplr(0.5),
    iaa.Flipud(0.5),
    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, cols=5, rows=2)
plt.figure(figsize=(30, 12))
_ = plt.imshow(imggrid.astype(int))

# =============================================================================
# #  TRAIN
##   Now it's time to train the model. Note that training even a basic model can take a few hours.
예제 #24
0
def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = WhaleDataset()
    dataset_train.load_whale(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = WhaleDataset()
    dataset_val.load_whale(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.

    # first run
    #layers_training='heads'
    # second run
    #layers_training='3+'
    # third run
    layers_training = 'all'
    epochs_to_train = 250

    # adding image augmentation parameters

    augmentation = iaa.Sometimes(
        .667,
        iaa.Sequential(
            [
                iaa.Fliplr(0.5),  # horizontal flips
                iaa.Crop(percent=(0, 0.1)),  # random crops
                # Small gaussian blur with random sigma between 0 and 0.25.
                # But we only blur about 50% of all images.
                iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.25))),
                # Strengthen or weaken the contrast in each image.
                iaa.ContrastNormalization((0.75, 1.5)),
                # Add gaussian noise.
                # For 50% of all images, we sample the noise once per pixel.
                # For the other 50% of all images, we sample the noise per pixel AND
                # channel. This can change the color (not only brightness) of the
                # pixels.
                iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255)),
                # 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((0.8, 1.2)),
                # Apply affine transformations to each image.
                # Scale/zoom them, translate/move them, rotate them and shear them.
                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=(-180, 180),
                    #shear=(-8, 8)
                )
            ],
            random_order=True))  # apply augmenters in random order

    # old image aug parameters
    """
    augmentation = iaa.Sometimes(0.9, [
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Multiply((0.8, 1.2)),
        iaa.GaussianBlur(sigma=(0.0, 5.0))
        ])
    """

    print("Training 5+ layerss with augmentation.")
    print("*****Beginning training*****")
    print("config.LEARNING_RATE", config.LEARNING_RATE)
    print("layers_training:", layers_training)
    print("epochs_to_train:", epochs_to_train)
    print("augmentation: ", augmentation)
    print("---")
    print("Images: {}\nClasses: {}".format(len(dataset_train.image_ids),
                                           dataset_train.class_names))
    #print("Training network in its entirety with augmentation")

    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=epochs_to_train,
                layers=layers_training,
                augmentation=augmentation)
예제 #25
0
파일: paint_words.py 프로젝트: Lego1st/MOCR
import numpy as np 
import cv2
import os
from imgaug import augmenters as iaa
from boundingDetect import fit_contours

(winW, winH) = (5, 10)
folder = '/home/tailongnguyen/aeh16/text'
if not os.path.isdir(folder):
    os.mkdir(folder)

labels = list('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')

contrast2 = iaa.ContrastNormalization((0.65, 0.85), per_channel=0.5)
add = iaa.Add(-40, per_channel=0.5)
noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.08*255), per_channel=0.4)

def get_fols(di):
	l = os.listdir(di)
	l.sort()
	l = [di + "/" + x for x in l]
	return l
# stop = 0
contrast = iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)
for i, fol in enumerate(get_fols(folder)):
	print "Processing " + fol
	image_files = os.listdir(fol) 
	# stop += 1
	for j, image in enumerate(image_files):
		image_file = os.path.join(fol, image)
		im = cv2.imread(image_file)
예제 #26
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
예제 #27
0
import imgaug as ia
from imgaug import augmenters as iaa

source = "darkflow/Dataset"
output_dir = "darkflow/Data"
classes = ['small-car', 'big-car', 'bus', 'truck', 'three-wheeler', 'two-wheeler', 'lcv', 'bicycle', 'people']
seq = iaa.Sequential([
    iaa.Fliplr(0.5),  # horizontal flips
    iaa.Crop(percent=(0, 0.1)),  # random crops
    # Small gaussian blur with random sigma between 0 and 0.5.
    # But we only blur about 50% of all images.
    iaa.Sometimes(0.5,
                  iaa.GaussianBlur(sigma=(0, 0.5))
                  ),
    # Strengthen or weaken the contrast in each image.
    iaa.ContrastNormalization((0.75, 1.5)),
    # Add gaussian noise.
    # For 50% of all images, we sample the noise once per pixel.
    # For the other 50% of all images, we sample the noise per pixel AND
    # channel. This can change the color (not only brightness) of the
    # pixels.
    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
    # Make some images brighter and some darker.
    # In 20% of all cases, we sample the multiplier once per channel,
    # which can end up changing the color of the images.
    iaa.Multiply((0.8, 1.2), per_channel=0.2),
], random_order=True)
dir_skip = 3
image_skip = 352
flag = True
for dir_count, dir in enumerate(os.listdir(source)):
예제 #28
0
def augmentation_generator(data, batch_size):
    '''
    Performs data augmentation on training set. Practice data gen.
    '''
    n = len(data)
    batches_to_make = n // batch_size

    # Containers for batch data and labels.
    batch_data = np.zeros((batch_size, 224, 224, 3), dtype=np.float32)
    batch_labels = np.zeros((batch_size, 2), dtype=np.float32)

    # Grab indices.
    indices = np.arange(n)

    # Define transformations.
    transformations = iaa.OneOf([
        iaa.Fliplr(0.5),  # Flip.
        iaa.ContrastNormalization((0.75, 1.5)),  # Play around with contrast.
        iaa.GaussianBlur(sigma=(0, 0.5)),  # Randomly blur.
        iaa.Multiply((0.8, 1.2))  # Change brightness.
    ])

    # Start a counter.
    batches_made = 0
    while True:

        # Make batch.
        np.random.shuffle(indices)
        count = 0
        next_batch = indices[(batches_made * batch_size):(batches_made + 1) *
                             batch_size]
        for foo, idx in enumerate(next_batch):
            img_name = data.iloc[idx]['image_path']
            img_label = data.iloc[idx]['label']

            # Encode label.
            encoded_label = to_categorical(img_label, num_classes=2)

            # Read image and resize.
            img = cv2.imread(str(img_name))
            img = cv2.resize(img, (224, 224))

            # Check for greyscale.
            if img.shape[2] == 1:
                img = np.dstack([img, img, img])

            # Change from BGR to RGB and normalize.
            orig_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            orig_img = orig_img.astype(np.float32) / 255

            batch_data[count] = orig_img
            batch_labels[count] = encoded_label

            # Data augment for under-sampled class.
            if img_label == 0 and count < batch_size - 2:
                # Image 1.
                aug_img1 = transformations.augment(images=img)
                aug_img1 = cv2.cvtColor(aug_img1, cv2.COLOR_BGR2RGB)
                aug_img1 = aug_img1.astype(np.float32) / 255
                batch_data[count + 1] = aug_img1
                batch_labels[count + 1] = encoded_label

                # Image 2.
                aug_img2 = transformations.augment(images=img)
                aug_img2 = cv2.cvtColor(aug_img2, cv2.COLOR_BGR2RGB)
                aug_img2 = aug_img2.astype(np.float32) / 255
                batch_data[count + 2] = aug_img2
                batch_labels[count + 2] = encoded_label

            else:
                count += 1

            # Can't add anymore.
            if count == batch_size - 1:
                break

        batches_made += 1
        yield batch_data, batch_labels

        # Reset for the next go.
        if batches_made >= batches_to_make:
            batches_made = 0
예제 #29
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.Scale(0.5, name="Scale"),
        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))
예제 #30
0
    def __init__(self, image_root, txt_path, is_train, transform=None):
        super(Single_Dataset, self).__init__()
        self.image_root = image_root
        self.txt_path = txt_path
        self.is_train = is_train
        self.transform = transform
        self.image_list = []
        self.label_list = []

        lines = open(self.txt_path, 'r').readlines()
        lines = [line.strip() for line in lines]
        for line in lines:
            self.image_list.append(line.split('   ')[0])
            self.label_list.append(int(line.split('   ')[1]))

        self.seq = iaa.SomeOf(
            (3, 11),
            {
                # self.seq = iaa.SomeOf((0, 5), {
                # iaa.Fliplr(0.5),
                iaa.Flipud(0.5),
                # iaa.Crop(percent=(0, 0.1)),
                # 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=(0, 0.5))),
                # Strengthen or weaken the contrast in each image.
                iaa.ContrastNormalization((0.75, 1.5)),
                # 先将图片从RGB变换到HSV,然后将H值增加10,然后再变换回RGB
                iaa.WithColorspace(to_colorspace="HSV",
                                   from_colorspace="RGB",
                                   children=iaa.WithChannels(
                                       2, iaa.Add((10, 50)))),
                iaa.AverageBlur(k=((2, 5), (1, 3))),
                iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect((0.0, 0.2)),
                                      second=iaa.ContrastNormalization(
                                          (0.5, 2.0)),
                                      per_channel=True),
                # 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.ImpulseNoise(p=0.02),
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.02 * 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((0.8, 1.2), per_channel=0.2),
                iaa.PerspectiveTransform(scale=0.06),
                # # 图像扭曲
                # iaa.PiecewiseAffine(scale=(0.01, 0.05)),
                # Apply affine transformations to each image.
                # Scale/zoom them, translate/move them, rotate them and shear them.
                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=(-8, 8))
            },
            random_order=True)