Example #1
0
 def logic(self, image):
     for param in self.augmentation_params:
         self.augmentation_data.append([
             str(param.augmentation_value),
             iaa.Pepper(p=param.augmentation_value).to_deterministic().
             augment_image(image), param.detection_tag
         ])
Example #2
0
def generator(image_list):

    for name in image_list:
        fileName = name
        name = os.path.join(PATH, name)
        images = cv2.imread(name)

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

        seq = iaa.Sequential([
            iaa.Flipud(p=0.5),
            iaa.Fliplr(p=0.5),
            sometimes(iaa.Pepper(p=0.10)),
            sometimes(iaa.Salt(p=0.03)),
            sometimes(iaa.AdditivePoissonNoise(lam=8.0)),
            sometimes(iaa.JpegCompression(compression=50)),
            sometimes(iaa.PiecewiseAffine(scale=0.015)),
            sometimes(iaa.MotionBlur(k=7, angle=0)),
            sometimes(iaa.MotionBlur(k=5, angle=144))
        ],
                             random_order=False)

        for i in range(10):

            images_aug = seq.augment_image(images)
            name = 'aug_' + fileName.split('.')[0] + "-" + str(i) + '.jpg'
            name = os.path.join(PATH, name)
            cv2.imwrite(name, images_aug)
            print(name + " is saved.")
Example #3
0
def image_aug(image):
    """
    @param image:
    @return:
    """
    seq = iaa.SomeOf(
        (1, 3),
        [
            iaa.Crop(px=(0, 16)),  # 裁剪
            iaa.Multiply((0.7, 1.3)),  # 改变色调
            iaa.Affine(scale=(0.5, 0.7)),  # 放射变换
            iaa.GaussianBlur(sigma=(0, 1.5)),  # 高斯模糊
            iaa.AddToHueAndSaturation(value=(25, -25)),
            iaa.ChannelShuffle(1),  # RGB三通道随机交换
            iaa.ElasticTransformation(alpha=0.1),
            iaa.Grayscale(alpha=(0.2, 0.5)),
            iaa.Pepper(p=0.03),
            iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)),
            iaa.Dropout(p=(0.03, 0.05)),
            iaa.Salt(p=(0.03, 0.05)),
            iaa.AverageBlur(k=(1, 3)),
            iaa.Add((-10, 10)),
            iaa.CoarseSalt(size_percent=0.01)
        ])
    seq_det = seq.to_deterministic()
    image_aug = seq_det.augment_images([image])[0]

    return image_aug
Example #4
0
def train(model, dataset_dir):
    """Train the model."""
    # Training dataset.
    dataset_train = MangaDataset()
    dataset_train.load_Manga(dataset_dir, "train")
    dataset_train.prepare()  #dataset 클래스의 참조 필요

    # Validation dataset
    dataset_val = MangaDataset()
    dataset_val.load_Manga(dataset_dir, "val")
    dataset_val.prepare()

    # Image augmentation
    augmentation = iaa.SomeOf(
        (2, 4),
        [
            #iaa.Fliplr(0.5),
            #iaa.Flipud(0.5),
            iaa.OneOf([
                iaa.Affine(rotate=15),
                iaa.Affine(rotate=10),
                iaa.Affine(rotate=20),
                iaa.Affine(rotate=25),
                iaa.Affine(rotate=30),
                iaa.Affine(rotate=350),
                iaa.Affine(rotate=345),
                iaa.Affine(rotate=340),
                iaa.Affine(rotate=330)
            ]),
            #iaa.Multiply((0.8, 1.5)),
            #iaa.GaussianBlur(sigma=(0.0, 5.0)),
            iaa.Dropout(p=(0.15, 0.25)),
            iaa.Pepper(p=(0.2, 0.3)),
            iaa.CoarseDropout(p=(0.2, 0.6), size_percent=(0.02, 0.4)),
            iaa.AdditiveGaussianNoise(scale=0.05 * 255)
        ])
    # http://imgaug.readthedocs.io/en/latest/source/augmenters.html
    #이건 뭐하는걸까? image augmentation인걸 보니 전처리를 해주는가보다.

    # *** 수정이 필요하면 맞게 수정하세요 ***

    # If starting from imagenet, train heads only for a bit
    # since they have random weights
    print("Train network heads")
    model.train(
        dataset_train,
        dataset_val,
        learning_rate=config.LEARNING_RATE,  #config 참조 필요 혹은 직접 러닝레이트 지정
        epochs=150,
        augmentation=augmentation,
        layers='heads')

    print("Train all layers")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=2000,
                augmentation=augmentation,
                layers='all')
def imgaugRGB(img):

    print(img.shape)
    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((0, 2), [
                iaa.GaussianBlur((0.0, 2.0)),
                iaa.AverageBlur(k=(3, 7)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.BilateralBlur(d=(1, 7)),
                iaa.MotionBlur(k=(3, 7))
            ]),
            #color
            iaa.SomeOf(
                (0, 2),
                [
                    #iaa.WithColorspace(),
                    iaa.AddToHueAndSaturation((-20, 20)),
                    #iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                    iaa.Grayscale(alpha=(0.0, 0.2))
                ]),
            #brightness
            iaa.OneOf([
                iaa.Sequential([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.5, 1.5), per_channel=0.5)
                ]),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.Multiply((0.5, 1.5), per_channel=0.5),
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Multiply(
                                            (0.5, 1.5), per_channel=0.5),
                                        second=iaa.ContrastNormalization(
                                            (0.5, 2.0), per_channel=0.5))
            ]),
            #contrast
            iaa.SomeOf((0, 2), [
                iaa.GammaContrast((0.5, 1.5), per_channel=0.5),
                iaa.SigmoidContrast(
                    gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
            ]),
            #arithmetic
            iaa.SomeOf((0, 3), [
                iaa.AdditiveGaussianNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditiveLaplaceNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditivePoissonNoise(lam=(0, 8), per_channel=0.5),
                iaa.Dropout(p=(0, 0.05), per_channel=0.5),
                iaa.ImpulseNoise(p=(0, 0.05)),
                iaa.SaltAndPepper(p=(0, 0.05)),
                iaa.Salt(p=(0, 0.05)),
                iaa.Pepper(p=(0, 0.05))
            ]),
            #iaa.Sometimes(p=0.5, iaa.JpegCompression((0, 30)), None),
        ],
        random_order=True)
    return seq.augment_image(img)
Example #6
0
def main():
    # datapath为存放训练图片的地方
    datapath = '/home/zhex/data/OID_origin/train/Umbrella/'
    # original_file为需要被增强的
    original_file = '/home/zhex/data/OID_origin/tools/new_txt/Umbrella.txt'  # 需要被增强的训练真值txt
    # aug_file只记录了新增的增强后图片的box,要得到原始+增强的所有label:cat original_file augfile>finalfile(txt拼接)
    # aug_file输出是pdpd需要的格式,pytorch需要另行转换(可以拼接得到finalfile后直接将finalfile转换)
    aug_file = 'augfile_Umbrella.txt'
    dict_before = readlist(original_file)
    new_fp = open(aug_file, 'w')
    # augscene = {'Umbrellad':10,'hat':2}  # 需要哪些场景,新增几倍数量的新数据
    augscene = {'Umbrella': 5}
    for scene in augscene:  # scene = Umbrella img_id = scene
        for i in range(augscene[scene]):
            for img_id in dict_before.keys():
                img = Image.open(datapath + img_id)
                img = np.array(img)
                bbs = ia.BoundingBoxesOnImage([
                    ia.BoundingBox(x1=x, y1=y, x2=x + w, y2=y + h)
                    for [x, y, w, h] in dict_before[img_id]
                ],
                                              shape=img.shape)

                # 设置数据增强方式
                seq = iaa.SomeOf(
                    (1, 3),
                    [
                        iaa.Crop(px=(0, 16)),  #裁剪
                        iaa.Multiply((0.7, 1.3)),  #改变色调
                        iaa.Affine(scale=(0.5, 0.7)),  #放射变换
                        iaa.GaussianBlur(sigma=(0, 1.5)),  #高斯模糊
                        # iaa.AddToHueAndSaturation(value=(25,-25)),
                        iaa.ChannelShuffle(1),  # RGB三通道随机交换
                        iaa.ElasticTransformation(alpha=0.1),
                        # iaa.Grayscale(alpha=(0.2, 0.5)),
                        iaa.Pepper(p=0.03),
                        iaa.AdditiveGaussianNoise(scale=(0.03 * 255,
                                                         0.05 * 255)),
                        iaa.Dropout(p=(0.03, 0.05)),
                        iaa.Salt(p=(0.03, 0.05)),
                        iaa.AverageBlur(k=(1, 3)),
                        iaa.Add((-10, 10)),
                        iaa.CoarseSalt(size_percent=0.01)
                    ])
                seq_det = seq.to_deterministic(
                )  # 保持坐标和图像同步改变,每个batch都要调用一次,不然每次的增强都是一样的
                image_aug = seq_det.augment_images([img])[0]
                bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

                pic_name = img_id.split('.')[0]

                # datapath = '/home/zhex/OID/train/Umbrella'
                if not os.path.exists(datapath + 'myaug/'):
                    os.makedirs(datapath + 'myaug/')
                new_img_id = 'myaug/' + pic_name + '_{}'.format(i) + '.jpg'
                print('new_img_id = ', new_img_id)
                Image.fromarray(image_aug).save(datapath + new_img_id)
                new_fp = writelist(new_fp, new_img_id, bbs_aug.bounding_boxes)
def chapter_augmenters_pepper():
    fn_start = "arithmetic/pepper"

    aug = iaa.Pepper(0.1)
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2,
                        quality=95)
Example #8
0
	def __init__(self):
		#print('[INFO] Applying data augmentation...')
		sometimes = lambda aug: iaa.Sometimes(0.5, aug)
		rare = lambda aug: iaa.Sometimes(0.25, aug)
		self.seq = iaa.Sequential([sometimes(iaa.Affine(
								   rotate=(-5,5), 
								   translate_percent={'x': (-0.05, 0.05), 'y':(-0.05,0.05)}, 
								   shear=(-10,10))),
							  	   rare(iaa.Pepper(0.05))])
 def aug_before_prepare(self, img, bboxes, masks, polys):
     aug = iaa.SomeOf((1, 3), [
         iaa.Multiply(((0.7, 1.3))),
         iaa.GaussianBlur(sigma=(0, 1.5)),
         iaa.ChannelShuffle(1),
         iaa.Pepper(p=0.03),
         iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)),
         iaa.Dropout(p=(0.03, 0.05)),
         iaa.Salt(p=(0.03, 0.05)),
         iaa.Add((-10, 10)),
         iaa.AverageBlur(k=(1, 3))
     ])
     if (random.random() > 0.5):
         img, bboxes, masks = self.random_rotate(img, np.array(polys), masks, 90)
     img = aug.augment_image(img)
     return img, bboxes, masks
Example #10
0
def do_all_aug(image):
    do_aug(image, iaa.Noop(name="origin"))
    do_aug(image, iaa.Crop((0, 10)))  # 切边
    do_aug(image, iaa.GaussianBlur((0, 3)))
    do_aug(image, iaa.AverageBlur(1, 7))
    do_aug(image, iaa.MedianBlur(1, 7))
    do_aug(image, iaa.Sharpen())
    do_aug(image, iaa.BilateralBlur())  # 既噪音又模糊,叫双边
    do_aug(image, iaa.MotionBlur())
    do_aug(image, iaa.MeanShiftBlur())
    do_aug(image, iaa.GammaContrast())
    do_aug(image, iaa.SigmoidContrast())
    do_aug(image,
           iaa.Affine(shear={
               'x': (-10, 10),
               'y': (-10, 10)
           }, mode="edge"))  # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一
    do_aug(image,
           iaa.Affine(shear={
               'x': (-10, 10),
               'y': (-10, 10)
           }, mode="edge"))  # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一
    do_aug(image, iaa.Rotate(rotate=(-10, 10), mode="edge"))
    do_aug(image, iaa.PiecewiseAffine())  # 局部点变形
    do_aug(image, iaa.Fog())
    do_aug(image, iaa.Clouds())
    do_aug(image, iaa.Snowflakes(flake_size=(0.1, 0.2),
                                 density=(0.005, 0.025)))
    do_aug(
        image,
        iaa.Rain(
            nb_iterations=1,
            drop_size=(0.05, 0.1),
            speed=(0.04, 0.08),
        ))
    do_aug(
        image,
        iaa.ElasticTransformation(alpha=(0.0, 20.0),
                                  sigma=(3.0, 5.0),
                                  mode="nearest"))
    do_aug(image, iaa.AdditiveGaussianNoise(scale=(0, 10)))
    do_aug(image, iaa.AdditiveLaplaceNoise(scale=(0, 10)))
    do_aug(image, iaa.AdditivePoissonNoise(lam=(0, 10)))
    do_aug(image, iaa.Salt((0, 0.02)))
    do_aug(image, iaa.Pepper((0, 0.02)))
Example #11
0
 def __init__(self, input_size=(512, 512), features_pixel=8, aug=False):
     self.input_size = input_size
     assert input_size[0] % features_pixel == 0
     assert input_size[1] % features_pixel == 0
     self.feature_pixel = features_pixel
     self.feature_size = (
         input_size[0] // features_pixel,
         input_size[1] // features_pixel,
     )
     if aug:
         self.aug = iaa.Sequential(
             [
                 iaa.ContrastNormalization((0.75, 1.5)),
                 iaa.AdditiveGaussianNoise(
                     loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5
                 ),
                 iaa.Multiply((0.8, 1.2), per_channel=0.2),
                 iaa.Add((-10, 10), per_channel=0.5),
                 iaa.Pepper((0, 0.05), per_channel=0.2),
                 iaa.GaussianBlur((0, 2.0)),
             ]
         )
     else:
         self.aug = None
Example #12
0
    augmenters = [
        # blur images with a sigma between 0 and 3.0
        iaa.Noop(),
        iaa.GaussianBlur(sigma=(0.5, 2.0)),
        iaa.Add((-50.0, 50.0), per_channel=False),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.07 * 255, 0.07 * 255),
                                  per_channel=False),
        iaa.Dropout(p=0.07, per_channel=False),
        iaa.CoarseDropout(p=(0.05, 0.15),
                          size_percent=(0.1, 0.9),
                          per_channel=False),
        iaa.SaltAndPepper(p=(0.05, 0.15), per_channel=False),
        iaa.Salt(p=(0.05, 0.15), per_channel=False),
        iaa.Pepper(p=(0.05, 0.15), per_channel=False),
        iaa.ContrastNormalization(alpha=(iap.Uniform(0.02, 0.03),
                                         iap.Uniform(1.7, 2.1))),
        iaa.ElasticTransformation(alpha=(0.5, 2.0)),
    ]

    seq = iaa.Sequential(iaa.OneOf(augmenters), )

    def get_data_from_tip(tip, batch_size):
        features = []
        labels = []
        descriptions = []
        for i in range(batch_size):
            data = tip.get()
            d, f, l = data
            features.append(f.reshape((224, 224, 1)))
Example #13
0
def main():
    # datapath为存放训练图片的地方
    datapath = '/home/zhex/data/yuncong/'
    # original_file为需要被增强的
    original_file = '/home/zhex/data/yuncong/Mall_train.txt'  # 需要被增强的训练真值txt
    # aug_file只记录了新增的增强后图片的box,要得到原始+增强的所有label:cat original_file augfile>finalfile(txt拼接)
    # aug_file输出是pdpd需要的格式,pytorch需要另行转换(可以拼接得到finalfile后直接将finalfile转换)
    aug_file = 'augfile_Mall.txt'
    dict_before = readlist(original_file)
    new_fp = open(aug_file, 'w')
    # augscene = {'Mall': 3, 'Part_B': 10, 'Part_A': 13}  # 需要哪些场景,新增几倍数量的新数据
    augscene = {'Mall': 3}
    for scene in augscene:
        for i in range(augscene[scene]):
            for img_id in dict_before.keys():
                if scene in img_id:
                    print(img_id)
                    img = Image.open(
                        datapath +
                        img_id)  # img.convert('RGB')->img.save('filename.jpg')
                    img = np.array(img)
                    bbs = ia.BoundingBoxesOnImage([
                        ia.BoundingBox(x1=x, y1=y, x2=x + w, y2=y + h)
                        for [x, y, w, h] in dict_before[img_id]
                    ],
                                                  shape=img.shape)

                    # 设置数据增强方式
                    # import imgaug.augmenters as iaa
                    # List augmenter that applies only some of its children to images
                    '''
                    iaa.SomeOf(n=None,
                        children=None,
                        random_order=False,
                        name=None,
                        deterministic=False,
                        random_state=None)
                        n: 从总的Augmenters中选择多少个来处理图片,类型可以是int,tuple,list,或者随机值
                        random_order: 是否每次顺序一样,默认值False(即每次顺序一样)
                    '''
                    seq = iaa.SomeOf(
                        (1, 3),
                        [  #每次使用1~3个Augmenter来处理图片,每个batch的顺序一样
                            iaa.Crop(px=(0, 16)),  #裁剪
                            iaa.Multiply((0.7, 1.3)),  #改变色调
                            iaa.Affine(scale=(0.5, 0.7)),  #仿射变换
                            iaa.GaussianBlur(sigma=(0, 1.5)),  #高斯模糊
                            iaa.AddToHueAndSaturation(value=(25, -25)),
                            iaa.ChannelShuffle(1),  # RGB三通道随机交换
                            iaa.ElasticTransformation(alpha=0.1),
                            iaa.Grayscale(alpha=(0.2, 0.5)),
                            iaa.Pepper(p=0.03),
                            iaa.AdditiveGaussianNoise(scale=(0.03 * 255,
                                                             0.05 * 255)),
                            iaa.Dropout(p=(0.03, 0.05)),
                            iaa.Salt(p=(0.03, 0.05)),
                            iaa.AverageBlur(k=(1, 3)),
                            iaa.Add((-10, 10)),
                            iaa.CoarseSalt(size_percent=0.01)
                        ],
                        random_order=False)
                    seq_det = seq.to_deterministic(
                    )  # 保持坐标和图像同步改变,每个batch都要调用一次,不然每次的增强都是一样的
                    image_aug = seq_det.augment_images([img])[0]
                    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

                    pic_name = img_id.split('/')[-1].split('.')[0]
                    pic_dir = img_id.split(pic_name)[0]
                    if not os.path.exists(datapath + 'myaug/' + pic_dir):
                        os.makedirs(datapath + 'myaug/' + pic_dir)
                    new_img_id = 'myaug/' + pic_dir + pic_name + '_{}'.format(
                        i) + '.jpg'
                    Image.fromarray(image_aug).save(datapath + new_img_id)

                    new_fp = writelist(new_fp, new_img_id,
                                       bbs_aug.bounding_boxes)
Example #14
0
import shutil
import cv2
from imgaug import augmenters as iaa

PATH = "aug_data2"

for path, dirs, files in os.walk(PATH):
    for filename in files:
        fullpath = os.path.join(path, filename)
        image = cv2.imread(fullpath)

        idx = 10

        noise = iaa.AdditiveGaussianNoise(scale=(30, 30))
        image_aug = noise.augment_image(image)
        newpath = fullpath[:-4] + str(idx) + fullpath[-4:]
        cv2.imwrite(newpath, image_aug)
        idx += 1

        noise = iaa.AdditivePoissonNoise(lam=30)
        image_aug = noise.augment_image(image)
        newpath = fullpath[:-4] + str(idx) + fullpath[-4:]
        cv2.imwrite(newpath, image_aug)
        idx += 1

        noise = iaa.Pepper(p=0.05)
        image_aug = noise.augment_image(image)
        newpath = fullpath[:-4] + str(idx) + fullpath[-4:]
        cv2.imwrite(newpath, image_aug)
        idx += 1
def create_augmenters(height, width, height_augmentable, width_augmentable,
                      only_augmenters):
    def lambda_func_images(images, random_state, parents, hooks):
        return images

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

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

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

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

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

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

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

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

    return augmenters
Example #16
0
        transformed_image = transform(image=image)['image']

    elif augmentation == 'grid_dropout':
        transform = GridDropout(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'salt':
        transform = iaa.Salt(0.1)
        transformed_image = transform(image=image)

    elif augmentation == 'coarse_salt':
        transform = iaa.CoarseSalt(0.05, size_percent=(0.01, 0.1))
        transformed_image = transform(image=image)

    elif augmentation == 'pepper':
        transform = iaa.Pepper(0.1)
        transformed_image = transform(image=image)

    elif augmentation == 'coarse_pepper':
        transform = iaa.CoarsePepper(0.05, size_percent=(0.01, 0.1))
        transformed_image = transform(image=image)

    elif augmentation == 'salt_and_papper':
        transform = iaa.SaltAndPepper(0.1)
        transformed_image = transform(image=image)

    elif augmentation == 'coarse_salt_and_papper':
        transform = iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1))
        transformed_image = transform(image=image)

    elif augmentation == 'impulse_noise':
Example #17
0
                   rotate=(-90, 90),
                   order=1),
        iaa.Fliplr(0.5),  # horizontally flip 50% of the images
    ],
    random_order=True)  # apply augmenters in random order

CBLN = iaa.Sequential(
    [
        # Normalize contrast by a factor of 0.5 to 1.5, sampled randomly per image. It maight change the color.
        iaa.ContrastNormalization((0.9, 1.2)),
        # 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=(1.0, 1.5))),
        # endark the image
        EnBrightOrEnDark,
        # Add SaltAndPepper noise. For 50% of all images, we sample the noise once per pixel.
        iaa.Pepper((0.01, 0.02)),
        # Drop 0 to 5% of all pixels by converting them to black pixels, but do that on
        # a lower-resolution version of the image that has 5% to 50% of the original size, leading to large rectangular areas being dropped:
        iaa.CoarseDropout((0.0, 0.02), size_percent=(0.1, 0.35)),
    ],
    random_order=True)  # apply augmenters in random order

CBLN1 = iaa.Sequential(
    [
        # Normalize contrast by a factor of 0.5 to 1.5, sampled randomly per image. It maight change the color.
        iaa.ContrastNormalization(0.9),
        # Small gaussian blur with random sigma between 0 and 0.5, But we only blur about 50% of all images.
        iaa.GaussianBlur(1.0),
        # endark the image
        EnBrightOrEnDark,
        # Add SaltAndPepper noise. For 50% of all images, we sample the noise once per pixel.
def train(tip, iters=None, learning_rate=0.001, batch_norm=False):
    import tensorflow as tf

    random.seed(datetime.datetime.now())
    tf.set_random_seed(seed())

    default_device = '/gpu:0'
    # default_device = '/cpu:0'

    # hyperparams
    batch_size = 94
    training = True
    batch_norms_training = False
    # steps
    light_summary_steps = 25
    heavy_summary_steps = 250
    checkpoint_steps = 500
    # stats / logging
    model_version = 1
    time_str = datetime.datetime.now().strftime('%m-%d--%H%M%S')
    best_loss = 0.6
    batch_num = 0
    best_acc = 0
    models_to_keep = 3
    # glob vars
    heavy_sum = []
    light_sum = []
    models_history = []
    train_only_dense = False
    dense_size = 64
    dropout = True
    learning_rate = 1e-6
    augmentation = True    

    with tf.device(default_device):
        # config = tf.ConfigProto()
        config = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)    
        # please do not use the totality of the GPU memory
        config.gpu_options.per_process_gpu_memory_fraction = 0.98
        # config = tf.ConfigProto(device_count = {'GPU': 0})
        config.gpu_options.allow_growth = True
        with tf.Session(graph=tf.Graph(), config=config) as sess:
            with tf.name_scope("inputs"):
                # _images = tf.placeholder(tf.float32, [None, 224, 224, 1])
                _images = tf.placeholder(tf.float32, [batch_size, 224, 224, 1])
                _is_training = tf.placeholder(tf.bool, name='is_training')
            model = vggish.Vggish(_images,
                                  classes=2,
                                  trainable=training,
                                  batch_norm=batch_norm,
                                  dropout=dropout,
                                  only_dense=train_only_dense,
                                  dense_size=dense_size,
                                  bn_trainable=batch_norms_training)

            with tf.name_scope("targets"):
                _labels = tf.placeholder(tf.float32, shape=(None, 2), name='labels')

            with tf.name_scope("outputs"):
                logits = model.fc3l
                # predictions = tf.nn.softmax(logits, name='predictions')
                predictions = tf.nn.sigmoid(logits, name='predictions')
                
                tvars = tf.trainable_variables()
                for v in tvars:
                    print(v)
                    if 'weights' in v.name:
                        heavy_sum.append(tf.summary.histogram(v.name, v))
                        # if 'conv1_1' in v.name:
                        #     light_sum.append(tf.summary.histogram(v.name, v))
                for v in tvars:
                    if 'bias' in v.name:
                        heavy_sum.append(tf.summary.histogram(v.name, v))
                        # if 'conv1_1' in v.name:
                        #     light_sum.append(tf.summary.histogram(v.name, v))
                
                light_sum.append(tf.summary.histogram("predictions", predictions))
                light_sum.extend(model.summaries)
                heavy_sum.extend(model.heavy_summaries)

            with tf.name_scope("0_cost"):
                cross_entropy = tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=logits,
                    labels=_labels,
                    name='cross_entropy'
                )

                tvars = tf.trainable_variables() 
                L2 = [tf.nn.l2_loss(v) for v in tvars if 'bias' not in v.name]
                lossL2 = tf.add_n(L2) * 0.01

                cost = tf.reduce_mean(cross_entropy, name='cost') + lossL2
                light_sum.append(tf.summary.scalar("cost", cost))

            def my_capper(t):
                print(t)
                # return t
                if t is None:
                    return None
                
                return tf.clip_by_value(t, -5., 5.)
            
            log_string = 'logs/{}-vggish/{}-lr-{:.8f}'.format(model_version,
                                                              time_str,
                                                              learning_rate)
            with tf.name_scope("0_train"):
                with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):         
                    vars_to_train = model.get_vars_to_train()

                    global_step = tf.Variable(0, name='global_step')
                    learning_rate = tf.train.exponential_decay(learning_rate, global_step, 100000, 0.96, staircase=True)
                    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
                    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
                    grads_and_vars = optimizer.compute_gradients(cost, var_list=vars_to_train)
                    grads_and_vars = [(my_capper(gv[0]), gv[1]) for gv in grads_and_vars]
                    optimizer = optimizer.apply_gradients(grads_and_vars, global_step=global_step)
                    
                    correct_predictions = tf.equal(tf.argmax(predictions, 1),
                                                   tf.argmax(_labels, 1),
                                                   name='correct_predictions')
                    accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32),
                                              name='accuracy')

                    grad_norm = tf.norm(grads_and_vars[0][0])
                    light_sum.append(tf.summary.scalar("accuracy", accuracy))
                    light_sum.append(tf.summary.scalar("gradient", grad_norm))

            light_summary = tf.summary.merge(light_sum)
            heavy_summary = tf.summary.merge(light_sum + heavy_sum)

            sess.run(tf.global_variables_initializer())

            saver = tf.train.Saver(tf.global_variables(), max_to_keep=None)
            iteration = 0

            try:
                print('[+] loading startup.json')
                startup = json.load(open('startup.vggish.json', 'r'))
                print('[+] loading path:', startup['path'])
                state = json.load(open(startup['path'], 'r'))
                print('[+] loading checkpoint:', state['checkpoint_path'])
                last_checkpoint = os.path.dirname(state['checkpoint_path'])
                
                weights_to_load = vggish.conv_vars + vggish.fc_vars
                if train_only_dense:
                    weights_to_load = vggish.conv_vars
                model.load_weights(last_checkpoint, vars_names=weights_to_load)
                
                iteration = state['iteration']
                best_loss = state['best_loss']
                if 'train_loss' in state:
                    best_loss = state['best_loss']

                checkpoint_path = state['checkpoint_path']
            except:
                print('[!] no models to checkpoint from..')
                raise
            writer = tf.summary.FileWriter(log_string)


            augmenters = [
                # blur images with a sigma between 0 and 3.0
                iaa.Noop(),
                iaa.GaussianBlur(sigma=(0.5, 2.0)),
                iaa.Add((-50.0, 50.0), per_channel=False),
                iaa.AdditiveGaussianNoise(loc=0,
                                            scale=(0.07*255, 0.07*255),
                                            per_channel=False),
                iaa.Dropout(p=0.07, per_channel=False),
                iaa.CoarseDropout(p=(0.05, 0.15),
                                    size_percent=(0.1, 0.9),
                                    per_channel=False),
                iaa.SaltAndPepper(p=(0.05, 0.15), per_channel=False),
                iaa.Salt(p=(0.05, 0.15), per_channel=False),
                iaa.Pepper(p=(0.05, 0.15), per_channel=False),
                iaa.ContrastNormalization(alpha=(iap.Uniform(0.02, 0.03),
                                            iap.Uniform(1.7, 2.1))),
                iaa.ElasticTransformation(alpha=(0.5, 2.0)),
            ]

            if not augmentation:
                augmenters = [
                    iaa.Noop(),
                ]
            
            seq = iaa.Sequential(
                iaa.OneOf(augmenters),
            )

            while True:
                if iters and iteration >= iters:
                    return
                
                seq.reseed(seed())
                np.random.seed(seed())

                descriptions, features, labels = get_data_from_tip(tip, batch_size)
                iteration += 1
                # import pdb; pdb.set_trace()
                mean = 0.172840994091
                std = 0.206961060284
                merged_summaries = light_summary
                if iteration % heavy_summary_steps == 0:
                    merged_summaries = heavy_summary

                try:
                    with np.errstate(all='raise'):
                        for i in range(5):
                            newfeatures = seq.augment_images(features * 255) / 255
                            if not np.isnan(newfeatures).any():
                                break
                            print('[!] has nan in newfeatures, retrying', i)
                        if np.isnan(newfeatures).any():
                            print('[!] could not get rid of nan.. skipping this batch')
                            iteration -= 1
                            continue
                        features = newfeatures
                except Exception:
                    print("[!] Warning detected augmenting, skipping..")
                    tb = traceback.format_exc()
                    open("numpy_warns.log", 'ab').write(str(descriptions).encode('utf-8'))
                    open("numpy_warns.log", 'ab').write(str(tb).encode('utf-8'))
                    open("numpy_warns.log", 'a').write('------------------------------------')
                    # import pdb; pdb.set_trace()
                    continue

                feed_dict = {
                    _images: features,
                    _labels: labels,
                    _is_training: training
                }

                train_loss, val_acc, _, p, summary, grad, _corr_pred = sess.run(
                    [cost,
                     accuracy,
                     optimizer,
                     logits,
                     merged_summaries,
                     grad_norm,
                     correct_predictions],
                    feed_dict=feed_dict
                )

                print('[+] iteration {}'.format(iteration))
                if iteration % light_summary_steps == 0:
                    if os.path.isfile('nomix_pdb'):
                        import pdb
                        pdb.set_trace()

                    print('[+] writing summary')
                    writer.add_summary(summary, iteration)

                    print('\tIteration {} Accuracy: {} Loss: {}/{}'.format(iteration,
                                                                           val_acc,
                                                                           train_loss,
                                                                           best_loss))
                # if train_loss < best_loss:
                if iteration % checkpoint_steps == 0:
                    # print('\t\tNew Best Loss!')
                    best_loss = train_loss
                    timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
                    
                    checkpoint_dir = os.path.join('D:\\checkpoint', timestamp)
                    checkpoint_path = os.path.join('D:\\checkpoint', timestamp, 'model.ckpt')
                    print('\t\tSaving model to:' + checkpoint_path)
                    saver.save(sess, checkpoint_path, global_step=batch_num)
                    state = {
                        'iteration': iteration,
                        'best_acc': float(best_acc),
                        'best_loss': float(best_loss),
                        'val_acc': float(val_acc),
                        'train_loss': float(train_loss),
                        'checkpoint_path': checkpoint_path,
                        'log_string': log_string,
                    }
                    # state_path = os.path.join('save', timestamp, 'state.json')
                    state_path = os.path.join('D:\\checkpoint', timestamp, 'state.json')
                    open(state_path, 'w').write(json.dumps(state))
                    startup = {
                        'path': state_path,
                    }
                    open('startup.vggish.json', 'w').write(json.dumps(startup))
                    models_history.append(checkpoint_dir)
                    while len(models_history) > models_to_keep:
                        try:
                            path_to_del = models_history.pop(0)
                            print('[+] deleting model', path_to_del)
                            shutil.rmtree(path_to_del)
                        except:
                            print('[+] failed to delete')
                            traceback.print_exc()
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

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

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

    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_seed, row_name, augmenters) in rows_augmenters:
        ia.seed(row_seed)
        #for img_title, augmenter in augmenters:
        #    #aug.reseed(1000)
        #    pass

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

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

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

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

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

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


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

    # simpler and faster drawing routine
    """
    output_image = ExamplesImage(128, 128, 128+64, 32)
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
    misc.imsave("examples.jpg", output_image.draw())
    """

    # routine to draw many single files
    seen = defaultdict(lambda: 0)
    markups = []
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        output_image = ExamplesImage(128, 128, 128+64, 32)
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
        if "\n" in row_name:
            row_name_clean = row_name[0:row_name.find("\n")+1]
        else:
            row_name_clean = row_name
        row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower())
        row_name_clean = row_name_clean.strip("_")
        if seen[row_name_clean] > 0:
            row_name_clean = "%s_%d" % (row_name_clean, seen[row_name_clean] + 1)
        fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean,))
        #misc.imsave(fp, output_image.draw())
        save(fp, output_image.draw())
        seen[row_name_clean] += 1

        markup_descr = row_name.replace('"', '') \
                               .replace("\n", " ") \
                               .replace("(", "") \
                               .replace(")", "")
        markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr)
        markups.append(markup)

    for markup in markups:
        print(markup)
Example #20
0
    def augument(self, image, bbox_list):
        seq = iaa.Sequential([
            # 变形
            iaa.Sometimes(
                0.6,
                [
                    iaa.OneOf([
                        iaa.Affine(shear={
                            'x': (-1.5, 1.5),
                            'y': (-1.5, 1.5)
                        },
                                   mode="edge"),  # 仿射变化程度,单位像素
                        iaa.Rotate(rotate=(-1, 1), mode="edge"),  # 旋转,单位度
                    ])
                ]),
            # 扭曲
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.PiecewiseAffine(
                            scale=(0, 0.02), nb_rows=2, nb_cols=2),  # 局部仿射
                        iaa.ElasticTransformation(  # distort扭曲变形
                            alpha=(0, 3),  # 扭曲程度
                            sigma=(0.8, 1),  # 扭曲后的平滑程度
                            mode="nearest"),
                    ]),
                ]),
            # 模糊
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(sigma=(0, 0.7)),
                        iaa.AverageBlur(k=(1, 3)),
                        iaa.MedianBlur(k=(1, 3)),
                        iaa.BilateralBlur(
                            d=(1, 5),
                            sigma_color=(10, 200),
                            sigma_space=(10, 200)),  # 既噪音又模糊,叫双边,
                        iaa.MotionBlur(k=(3, 5)),
                        iaa.Snowflakes(flake_size=(0.1, 0.2),
                                       density=(0.005, 0.025)),
                        iaa.Rain(nb_iterations=1,
                                 drop_size=(0.05, 0.1),
                                 speed=(0.04, 0.08)),
                    ])
                ]),
            # 锐化
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.Sharpen(),
                    iaa.GammaContrast(),
                    iaa.SigmoidContrast()
                ])
            ]),
            # 噪音
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(scale=(1, 5)),
                    iaa.AdditiveLaplaceNoise(scale=(1, 5)),
                    iaa.AdditivePoissonNoise(lam=(1, 5)),
                    iaa.Salt((0, 0.02)),
                    iaa.Pepper((0, 0.02))
                ])
            ]),
            # 剪切
            iaa.Sometimes(
                0.8,
                [
                    iaa.OneOf([
                        iaa.Crop((0, 2)),  # 切边, (0到10个像素采样)
                    ])
                ]),
        ])

        assert bbox_list is None or type(bbox_list) == list

        if bbox_list is None or len(bbox_list) == 0:
            polys = None
        else:
            polys = [ia.Polygon(pos) for pos in bbox_list]
            polys = ia.PolygonsOnImage(polys, shape=image.shape)

        # 处理部分或者整体出了图像的范围的多边形,参考:https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html
        polys = polys.remove_out_of_image().clip_out_of_image()
        images_aug, polygons_aug = seq(images=[image], polygons=polys)

        image = images_aug[0]

        if polygons_aug is None:
            polys = None
        else:
            polys = []
            for p in polygons_aug.polygons:
                polys.append(p.coords)
            polys = np.array(polys, np.int32).tolist()  # (N,2)

        return image, polys
Example #21
0
X_train_all = np.load('x_train_all_64.npy')
y_train_all = np.load('x_label_all_64.npy')

#define augmnetation
aug1 = iaa.GaussianBlur(sigma=(0, 2.0))
aug2 = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)
aug3 = iaa.Multiply((0.8, 1.2), per_channel=0.2)
aug4 = 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))

aug5 = iaa.CoarseDropout(p=0.2, size_percent = 0.15)
aug6 = iaa.ContrastNormalization((0.75, 1.5))
aug7 = iaa.Pepper(p=0.05)

def augment_img(img):
    
    i = np.random.randint(0,9)

    if i==0:
        img_adapteq = img

    elif i==1:
        img_adapteq = aug4.augment_image(img)
        img_adapteq = aug1.augment_image(img_adapteq)

    elif i==2:
        img_adapteq = aug2.augment_image(img)
Example #22
0
    def get_aug(self):
        #sometimes_bg = lambda aug: iaa.Sometimes(0.3, aug)
        sometimes_contrast = lambda aug: iaa.Sometimes(0.3, aug)
        sometimes_noise = lambda aug: iaa.Sometimes(0.6, aug)
        sometimes_blur = lambda aug: iaa.Sometimes(0.6, aug)
        sometimes_degrade_quality = lambda aug: iaa.Sometimes(0.9, aug)
        sometimes_blend = lambda aug: iaa.Sometimes(0.2, aug)

        seq = iaa.Sequential(
                [
                # crop some of the images by 0-30% of their height/width
                # Execute 0 to 4 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, 4),
    #                     [ 
                # change the background color of some of the images chosing any one technique
#                sometimes_bg(iaa.OneOf([
#                            iaa.AddToHueAndSaturation((-60, 60)),
#                            iaa.Multiply((0.6, 1), per_channel=True),
#                            ])),
                #change the contrast of the input images chosing any one technique    
                sometimes_contrast(iaa.OneOf([
                            iaa.LinearContrast((0.5,1.5)),
                            iaa.SigmoidContrast(gain=(3, 5), cutoff=(0.4, 0.6)),
                            iaa.CLAHE(tile_grid_size_px=(3, 21)),
                            iaa.GammaContrast((0.5,1.0))
                            ])),

                #add noise to the input images chosing any one technique 
                sometimes_noise(iaa.OneOf([
                    iaa.AdditiveGaussianNoise(scale=(3,8)),
                    iaa.CoarseDropout((0.001,0.01), size_percent=0.5),
                    iaa.AdditiveLaplaceNoise(scale=(3,10)),
                    iaa.CoarsePepper((0.001,0.01), size_percent=(0.5)),
                    iaa.AdditivePoissonNoise(lam=(3.0,10.0)),
                    iaa.Pepper((0.001,0.01)),
                    iaa.Snowflakes(),
                    iaa.Dropout(0.01,0.01),
                    ])),

                #add blurring techniques to the input image
                sometimes_blur(iaa.OneOf([
                    iaa.AverageBlur(k=(3)),
                    iaa.GaussianBlur(sigma=(1.0)),
                    ])),

                # add techniques to degrade the iamge quality
                sometimes_degrade_quality(iaa.OneOf([
                            iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
                            iaa.Sharpen(alpha=(0.5), lightness=(0.75,1.5)),
                            iaa.BlendAlphaSimplexNoise(
                            foreground=iaa.Multiply(iap.Choice([1.5]), per_channel=False)
                            )
                            ])),

                # blend some patterns in the background    
                sometimes_blend(iaa.OneOf([
                            iaa.BlendAlpha(
                            factor=(0.6,0.8),
                            foreground=iaa.Sharpen(1.0, lightness=1),

                            background=iaa.CoarseDropout(p=0.1, size_px=np.random.randint(30))),

                            iaa.BlendAlphaFrequencyNoise(exponent=(-4),
                                       foreground=iaa.Multiply(iap.Choice([0.5]), per_channel=False)
                                       ),
                            iaa.BlendAlphaSimplexNoise(
                            foreground=iaa.Multiply(iap.Choice([0.5]), per_channel=True)
                            )
                      ])), 

                    ])
        return seq
Example #23
0
def main():
    parser = argparse.ArgumentParser(description="Check augmenters visually.")
    parser.add_argument(
        "--only",
        default=None,
        help=
        "If this is set, then only the results of an augmenter with this name will be shown. "
        "Optionally, comma-separated list.",
        required=False)
    args = parser.parse_args()

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

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

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

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

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

    for augmenter in augmenters:
        if args.only is None or augmenter.name in [
                v.strip() for v in args.only.split(",")
        ]:
            print("Augmenter: %s" % (augmenter.name, ))
            grid = []
            for image, kps, bbs in zip(images, keypoints, bounding_boxes):
                aug_det = augmenter.to_deterministic()
                imgs_aug = aug_det.augment_images(
                    np.tile(image[np.newaxis, ...], (16, 1, 1, 1)))
                kps_aug = aug_det.augment_keypoints([kps] * 16)
                bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16)
                imgs_aug_drawn = [
                    kps_aug_one.draw_on_image(img_aug)
                    for img_aug, kps_aug_one in zip(imgs_aug, kps_aug)
                ]
                imgs_aug_drawn = [
                    bbs_aug_one.draw_on_image(img_aug)
                    for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug)
                ]
                grid.append(np.hstack(imgs_aug_drawn))
            ia.imshow(np.vstack(grid))
Example #24
0
def train():
    with tf.device(default_device):
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with tf.Session(graph=tf.Graph(), config=config) as sess:
            with tf.name_scope("inputs"):
                # _images = tf.placeholder(tf.float32, shape=(None, 4096), name='images')
                _images = tf.placeholder(tf.float32, [None, 224, 224, 1])
                _is_training = tf.placeholder(tf.bool, name='is_training')
                _keep_prob = tf.placeholder(tf.float32,
                                            name='keep_probability')
            # imgs = tf.placeholder(tf.float32, [None, 224, 224, 1])
            model = vgg16.Vgg16(_images,
                                '../vgg16_weights.npz',
                                classes=2,
                                mean=[0.343388929118],
                                trainable=training)

            with tf.name_scope("targets"):
                _labels = tf.placeholder(tf.float32,
                                         shape=(None, 2),
                                         name='labels')

            with tf.name_scope("outputs"):
                output_weights = tf.Variable(initial_value=tf.truncated_normal(
                    shape=(hidden_layer_size, 2), mean=0.0, stddev=0.01),
                                             dtype=tf.float32,
                                             name="output_weights")

                output_bias = tf.Variable(initial_value=tf.zeros(2),
                                          dtype=tf.float32,
                                          name="output_bias")

                logits = model.fc3l
                predictions = tf.nn.softmax(logits, name='predictions')

                tf.summary.histogram("output_weights", output_weights)
                tf.summary.histogram("output_bias", output_bias)
                tf.summary.histogram("predictions", predictions)

            with tf.name_scope("cost"):
                cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                    logits=logits, labels=_labels, name='cross_entropy')
                cost = tf.reduce_mean(cross_entropy, name='cost')

                tf.summary.scalar("cost", cost)

            with tf.name_scope("train"):
                with tf.control_dependencies(
                        tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
                    starter_learning_rate = learning_rate
                    # global_step = tf.Variable(0, trainable=False)
                    # learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,
                    #                                         100000, 0.96, staircase=True)
                    optimizer = tf.train.AdamOptimizer(
                        learning_rate=learning_rate).minimize(cost)
                    correct_predictions = tf.equal(tf.argmax(predictions, 1),
                                                   tf.argmax(_labels, 1),
                                                   name='correct_predictions')
                    accuracy = tf.reduce_mean(tf.cast(correct_predictions,
                                                      tf.float32),
                                              name='accuracy')

                    tf.summary.scalar("accuracy", accuracy)

            merged_summaries = tf.summary.merge_all()
            sess.run(tf.global_variables_initializer())

            iteration = 0
            best_loss = 9999999999
            batch_num = 0
            best_acc = 0
            # test_batches = get_batches('TEST', batch_size=batch_size, repeat=True)

            log_string = 'logs/{}/{}'.format(model_version, time_str)
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=None)
            try:
                print('[+] loading startup.json')
                startup = json.load(open('startup.json', 'r'))
                print('[+] loading path:', startup['path'])
                state = json.load(open(startup['path'], 'r'))
                print('[+] loading checkpoint:', state['checkpoint_path'])
                saver.restore(
                    sess,
                    tf.train.latest_checkpoint(
                        os.path.dirname(state['checkpoint_path'])))
                # iteration = state['iteration']
                # best_acc = state['best_acc']
                # best_loss = state['best_loss']
                best_acc = state['best_acc']
                if 'val_acc' in state:
                    best_acc = state['val_acc']

                best_loss = state['best_loss']
                if 'train_loss' in state:
                    best_loss = state['train_loss']

                checkpoint_path = state['checkpoint_path']
                # log_string = 'next-' + state['log_string']
            except:
                print('[!] no models to checkpoint from..')
            writer = tf.summary.FileWriter(log_string)
            # import numpy as np
            # vcol = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
            # for v in vcol:
            #     res = sess.run([v])
            #     np.save(v.name.replace('/', '_').replace(':', '_')+'.npy', res)

            seq = iaa.Sequential(
                iaa.OneOf([
                    iaa.GaussianBlur(
                        (0.45)),  # blur images with a sigma between 0 and 3.0
                    iaa.Add((-90.0, 90.0), per_channel=False),
                    iaa.Multiply((0.5, 1.5), per_channel=False),
                    iaa.AdditiveGaussianNoise(loc=0,
                                              scale=(0.07 * 255, 0.07 * 255),
                                              per_channel=False),
                    iaa.Dropout(p=0.07, per_channel=False),
                    iaa.CoarseDropout(p=0.05,
                                      size_percent=(0.2, 0.9),
                                      per_channel=False),
                    iaa.SaltAndPepper(p=0.07, per_channel=False),
                    iaa.Salt(p=0.07, per_channel=False),
                    iaa.Pepper(p=0.07, per_channel=False),
                    iaa.ContrastNormalization(alpha=(1.2, 1.5)),
                    iaa.ElasticTransformation(alpha=(0.7)),
                ]), )

            cc = cache_file.CacheCollection({
                'filename': 'T:\\cache\\AudioToImage',
                'seek_policy': 'ONE_SHOT',
                'max_size': 2147483648,
                'max_split': 50
            })
            while True:
                train = cc.random_iterator(batch_size, test=False)
                test = cc.random_iterator(batch_size * 2, test=True)
                # train_dataset.shuffle()
                # test_dataset.shuffle()
                # test_batches = test_dataset.get_batches(batch_size)
                # for batch_train_images, batch_train_labels in train_dataset.get_batches(batch_size):
                for features, labels in train:
                    train_loss, _, p, summary = sess.run(
                        [cost, optimizer, logits, merged_summaries],
                        feed_dict={
                            _images: seq.augment_images(features * 255) / 255,
                            _labels: labels,
                            _keep_prob: keep_prob,
                            _is_training: training
                        })

                    iteration += 1
                    print('[+] iteration {}'.format(iteration))

                    if iteration % accuracy_print_steps == 0:
                        if not writer == None:
                            writer.add_summary(summary, iteration)

                        val_features, val_labels = next(test)

                        val_acc, val_summary = sess.run(
                            [accuracy, merged_summaries],
                            feed_dict={
                                _images:
                                seq.augment_images(val_features * 255) / 255,
                                _labels: val_labels,
                                _keep_prob: 1.,
                                _is_training: False
                            })

                        print('\tIteration {} Accuracy: {} Loss: {}'.format(
                            iteration, val_acc, train_loss))
                        print('\t\t Best Accuracy: {} Best Loss: {}'.format(
                            iteration, best_acc, best_loss))
                        if val_acc >= best_acc or train_loss <= best_loss:
                            if train_loss <= best_loss:
                                best_loss = train_loss
                            if val_acc >= best_acc:
                                best_acc = val_acc
                            timestamp = datetime.datetime.now().strftime(
                                "%Y-%m-%d_%H%M%S")
                            checkpoint_path = os.path.join(
                                'save', timestamp, 'model.ckpt')
                            print('\t\tSaving model to:' + checkpoint_path)
                            saver.save(sess,
                                       checkpoint_path,
                                       global_step=batch_num)
                            state = {
                                'iteration': iteration,
                                'best_acc': float(best_acc),
                                'best_loss': float(best_loss),
                                'val_acc': float(val_acc),
                                'train_loss': float(train_loss),
                                'checkpoint_path': checkpoint_path,
                                'log_string': log_string,
                            }
                            state_path = os.path.join('save', timestamp,
                                                      'state.json')
                            open(state_path, 'w').write(json.dumps(state))
                            startup = {
                                'path': state_path,
                            }
                            open('startup.json',
                                 'w').write(json.dumps(startup))

                    batch_num += 1
            if saved_model_path:
                ### Save graph and trained variables
                builder = saved_model_builder.SavedModelBuilder(
                    saved_model_path)
                builder.add_meta_graph_and_variables(
                    sess, [SERVING],
                    signature_def_map={
                        DEFAULT_SERVING_SIGNATURE_DEF_KEY:
                        predict_signature_def(
                            inputs={PREDICT_INPUTS: _images},
                            outputs={PREDICT_OUTPUTS: predictions})
                    })

                builder.save()
Example #25
0
    # Replaces percent of all pixels with salt/pepper in an image that has lo to hi percent of the input image size,
    # then upscales the results to the input image size, leading to large rectangular areas being replaced.
    "Coarse_Salt_And_Pepper": lambda percent, lo, hi: iaa.CoarseSaltAndPepper(percent, size_percent=(lo, hi)),

    # Adds salt noise to an image, i.e white-ish pixels
    # Replaces percent of all pixels with salt noise
    "Salt": lambda percent: iaa.Salt(percent),

    # Adds coarse salt noise to image, i.e. rectangles that contain noisy white-ish pixels
    # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size,
    # then upscales the results to the input image size, leading to large rectangular areas being replaced.
    "Coarse_Salt": lambda percent, lo, hi: iaa.CoarseSalt(percent, size_percent=(lo, hi)),

    # Adds Pepper noise to an image, i.e Black-ish pixels
    # Replaces percent of all pixels with Pepper noise
    "Pepper": lambda percent: iaa.Pepper(percent),

    # Adds coarse pepper noise to image, i.e. rectangles that contain noisy black-ish pixels
    # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size,
    # then upscales the results to the input image size, leading to large rectangular areas being replaced.
    "Coarse_Pepper": lambda percent, lo, hi: iaa.CoarsePepper(percent, size_percent=(lo, hi)),

    # In an alpha blending, two images are naively mixed. E.g. Let A be the foreground image, B be the background
    # image and a is the alpha value. Each pixel intensity is then computed as a * A_ij + (1-a) * B_ij.
    # Images passed in must be a numpy array of type (height, width, channel)
    "Blend_Alpha": lambda image_fg, image_bg, alpha: iaa.blend_alpha(image_fg, image_bg, alpha),

    # Blur/Denoise an image using a bilateral filter.
    # Bilateral filters blur homogeneous and textured areas, while trying to preserve edges.
    # Blurs all images using a bilateral filter with max distance d_lo to d_hi with ranges for sigma_colour
    # and sigma space being define by sc_lo/sc_hi and ss_lo/ss_hi
Example #26
0
import numpy as np
from document_read_files import load_documents_files
from imgaug import augmenters as iaa

_rotation_plus = iaa.Affine(rotate=3)
_rotation_minus = iaa.Affine(rotate=-3)
_translate_plus = iaa.Affine(translate_percent={"x": 0, "y": 0.2})
_translate_minus = iaa.Affine(translate_percent={"x": 0, "y": -0.2})
_scale_up = iaa.Affine(scale=1.02, order=[0, 1])
_scale_down = iaa.Affine(scale=0.8, order=[0, 1])

_brightness = iaa.Multiply(0.6)
_dropout = iaa.Dropout(p=0.09, per_channel=True)
_gaussian_noise = iaa.AdditiveGaussianNoise(scale=30, per_channel=True)
_gaussian_blur = iaa.GaussianBlur(sigma=(1.5, 1.6))
_pepper = iaa.Pepper(0.05)
_hue_and_saturation = iaa.AddToHueAndSaturation((-25, 15))
_contrast = iaa.ContrastNormalization((0.4, 0.7))

def _apply_perspective(img):
    rows, cols, ch = img.shape

    param = 0.10 * rows

    pts1 = np.float32([[0, 0], [cols, 0], [cols, rows], [0, rows]])
    pts2 = np.float32([[param, param], [cols - param, param], [cols, rows - param], [0, rows - param]])

    M = cv2.getPerspectiveTransform(pts1, pts2)
    dst = cv2.warpPerspective(img, M, (cols, rows))

    return dst
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)
            filename = output_dir + '/' + str(ucode) + '_' + str(
                index) + '.jpg'
            select = random.randint(0, 20)
            # print('select: ', select)
            # 加入白色的椒盐噪声
            if select >= 1 and select < 3:
                add_salt_pepper(region)
                # print('white salt peper: ', filename)

            # 加入黑色的椒盐噪声
            if select > 7 and select < 10:
                # add_salt_pepper(region, (0, 0, 0))
                numpy_region = np.asarray(region)
                noise = iaa.Pepper(0.3)
                numpy_region = noise.augment_image(numpy_region)
                region = Image.fromarray(numpy_region)
                # print('black salt pepper: ', filename)
            # 是否添加高斯噪声或扫描件噪声
            if select >= 3 and select <= 6:
                # 添加高斯背景噪声
                background_gauss = np.ones(
                    (region.size[1], region.size[0])) * 255
                cv2.randn(background_gauss, 235, 10)
                background_gauss = Image.fromarray(background_gauss).convert(
                    'L')
                region = region.convert('L')
                mask = region.point(lambda x: 0
                                    if x == 255 or x == 0 else 255, '1')
                # mx, my = mask.size
Example #28
0
        def __init__(
                self,
                inputdata,
                inputlabels,
                augs="basic",  #["all","basic","form","valalt","pxlalt","imgalt"]
                num_outs=5,
                og_out=True,
                mode='G',
                em=0,
                intensity=1.0,
                rescaledata=None,
                formatd='NCHW',
                min_augs=0,
                max_augs=5):
            if self.mode.lower() == 'g':
                self.NM = self.rung
            elif self.mode.lower() == 'i':
                self.NM = self.runi()
            elif self.mode.lower() == 'i2':
                self.NM = self.runi2()
            else:
                print(
                    "invalid mode, use 'g' for generator or 'i' for iterator or 'i2'"
                )
                exit()
            self.minaug = min_augs
            self.maxaug = max_augs
            #self.affineopt=["scale","translate_percent","translate_px","rotate","shear"]
            #self.chnlopt=[{"per_channel":True},{"per_channel":False}]
            if len(inputdata.shape) == 4:
                self.D = 4
            elif len(inputdata.shape) == 3:
                self.D = 3
            elif len(inputdata.shape) == 2:
                self.D = 2
            if formatd == "NCHW":
                if self.D == 4:
                    self.inputd = np.transpose(inputdata, [0, 2, 3, 1])
                elif self.D == 3:
                    self.inputd = np.transpose(inputdata, [1, 2, 0])
            else:
                self.inputd = inputdata
            self.Y = inputlabels
            leninten = 8
            if isinstance(intensity, (float, int)):
                itensity = [intensity for _ in range(leninten)]
            else:
                assert len(intensity) == leninten
            self.datashape = np.array(inputdata.shape)  #inputdata[0].shape
            if self.datashape.min() == self.datashape[-1]:
                self.pixls = self.datashape[:-1]
            elif self.datashape.min() == self.datashape[1]:
                self.pixls = np.delete(self.datashape, 1)
            elif self.datashape.shape == (3, ):
                self.pixls = self.datashape[1:]
            else:
                print("error cannot fin the shape of images")
                exit()
            # can use "keep-aspect-ratio" for an arg to have a relative and absolute scale
            #or can also use list for randomization between options
            self.scalevals = (0.5 / (2 * intensity), 1.0)  #use % of image
            self.augs = augs
            self.Pchances = 0.44 * itensity[0]
            self.intrange = ((ceil(10 * intensity[1]),
                              ceil(10 + 140 * itensity[1])))
            self.windowrange = (ceil(2 * intensity[2]),
                                ceil((min(self.pixls) / 5) - 8) * intensity[2]
                                )  #mean/median things
            self.relatrange = (0.1 * intensity[3], 0.95 * intensity[3]
                               )  #normalisation,invert
            self.bigfloat = (
                0.085 * intensity[4], 1.75 * intensity[4]
            )  #some scale values,multiply,contrastnorm,elasti trans,(sigman&alpha)
            self.smallfloat = (0.001 * intensity[5], 0.45 * intensity[5]
                               )  #coarse dropout/droput(p)
            self.addrange = (ceil(-140 * intensity[6]),
                             ceil(140 * intensity[6]))
            self.multrange = (-2.0 * intensity[7], 2.0 * intensity[7])
            self.perchannelsplit = 0.75 * intensity[
                8]  #used for per_channel on the mult
            self.allaugs = {
                "add":
                IAGA.Add(value=self.addrange, per_channel=0.75 * intensity),
                "scale":
                IAGA.Scale(size=self.scalevals),
                "adde":
                IAGA.AddElementwise(value=self.addrange,
                                    per_channel=0.75 * intensity),
                "addg":
                IAGA.AdditiveGaussianNoise(scale=(0, self.smallfloat[1] * 255),
                                           per_channel=0.75 * intensity),
                "addh":
                IAGA.AddToHueAndSaturation(value=self.addrange,
                                           per_channel=0.75 * intensity),
                "mult":
                IAGA.Multiply(mul=self.bigfloat, per_channel=0.75 * intensity),
                "mule":
                IAGA.MultiplyElementwise(mul=self.bigfloat,
                                         per_channel=0.75 * intensity),
                "drop":
                IAGA.Dropout(p=self.smallfloat, per_channel=0.75 * intensity),
                "cdrop":
                IAGA.CoarseDropout(p=self.smallfloat,
                                   size_px=None,
                                   size_percent=self.smallfloat,
                                   per_channel=True,
                                   min_size=3),
                "inv":
                IAGA.Invert(p=self.Pchances,
                            per_channel=0.75 * intensity,
                            min_value=-255,
                            max_value=255),
                "cont":
                IAGA.ContrastNormalization(alpha=self.bigfloat,
                                           per_channel=0.75 * intensity),
                "aff":
                IAGA.Affine(
                    scale=self.bigfloat,
                    translate_percent={
                        'x': (-40 * intensity, 40 * intensity),
                        'y': (-40 * intensity, 40 * intensity)
                    },
                    translate_px=None,  #moving functions
                    rotate=(-360 * intensity, 360 * intensity),
                    shear=(-360 * intensity, 360 * intensity),
                    order=[0, 1]  #2,3,4,5 may be too much
                    ,
                    cval=0,  #for filling
                    mode=["constant", "edge", "reflect", "symmetric",
                          "wrap"][em],  #filling method
                    deterministic=False,
                    random_state=None),
                "paff":
                IAGA.PiecewiseAffine(
                    scale=(-0.075 * intensity, 0.075 * intensity),
                    nb_rows=(ceil(2 * intensity), ceil(7 * intensity)),
                    nb_cols=(ceil(2 * intensity), ceil(7 * intensity)),
                    order=[0, 1],
                    cval=0,
                    mode=["constant", "edge", "reflect", "symmetric",
                          "wrap"][em],
                    deterministic=False,
                    random_state=None),
                "elas":
                IAGA.ElasticTransformation(alpha=self.bigfloat,
                                           sigma=self.relatrange),
                "noop":
                IAGA.Noop(name="nope"),
                #IAGA.Lambda:{},
                "cropad":
                IAGA.CropAndPad(
                    px=None,
                    percent=(-0.65 * intensity[7], 0.65 * intensity[7]),
                    pad_mode=[
                        "constant", "edge", "reflect", "symmetric", "wrap"
                    ][em],
                    pad_cval=0,
                    keep_size=True,
                    sample_independently=True,
                ),
                "fliplr":
                IAGA.Fliplr(p=self.Pchances),
                "flipud":
                IAGA.Flipud(p=self.Pchances),
                "spixel":
                IAGA.Superpixels(p_replace=self.Pchances,
                                 n_segments=self.intrange),
                #IAGA.ChangeColorspace:,
                "gray":
                IAGA.Grayscale(alpha=self.relatrange),
                "gblur":
                IAGA.GaussianBlur(sigma=self.bigfloat),
                "ablur":
                IAGA.AverageBlur(k=self.windowrange),
                "mblur":
                IAGA.MedianBlur(k=self.windowrange),
                #IAGA.BilateralBlur,
                #IAGA.Convolve:,
                "sharp":
                IAGA.Sharpen(alpha=self.relatrange, lightness=self.bigfloat),
                "embo":
                IAGA.Emboss(alpha=self.relatrange, strenght=self.bigfloat),
                "edge":
                IAGA.EdgeDetect(alpha=self.relatrange),
                "dedge":
                IAGA.DirectedEdgeDetect(alpha=self.bigfloat,
                                        direction=(-1.0 * intensity,
                                                   1.0 * intensity)),
                "pert":
                IAGA.PerspectiveTransform(scale=self.smallfloat),
                "salt":
                IAGA.Salt(p=self.Pchances, per_channel=0.75 * intensity),
                #IAGA.CoarseSalt(p=, size_px=None, size_percent=None,per_channel=False, min_size=4),
                #IAGA.CoarsePepper(p=, size_px=None, size_percent=None,"per_channel=False, min_size=4),
                #IAGA.CoarseSaltAndPepper(p=, size_px=None, size_percent=None,per_channel=False, min_size=4),
                "pep":
                IAGA.Pepper(p=self.Pchances, per_channel=0.75 * intensity),
                "salpep":
                IAGA.SaltAndPepper(p=self.Pchances,
                                   per_channel=0.75 * intensity),
                #"alph":IAGA.Alpha(factor=,first=,second=,per_channel=0.75*intensity,),
                #"aplhe":IAGA.AlphaElementwise(factor=,first=,second=,per_channel=0.75*intensity,),
                #IAGA.FrequencyNoiseAlpha(exponent=(-4, 4),first=None, second=None, per_channel=False,size_px_max=(4, 16), upscale_method=None,iterations=(1, 3), aggregation_method=["avg", "max"],sigmoid=0.5, sigmoid_thresh=None,),
                #IAGA.SimplexNoiseAlpha(first=None, second=None, per_channel=False,size_px_max=(2, 16), upscale_method=None,iterations=(1, 3), aggregation_method="max",sigmoid=True, sigmoid_thresh=None,),
            }
            ["all", "basic", "form", "valalt", "pxlalt", "imgalt"]
            self.augs = []
            if (augs == "all") or ("all" in augs):
                self.augs = [
                    "add",
                    "scale",
                    "adde",
                    "addg",
                    "addh",
                    "mult",
                    "mule",
                    "drop",
                    "cdrop",
                    "inv",
                    "cont",
                    "aff",
                    "paff",
                    "elas",
                    "noop",
                    "cropad",
                    "fliplr",
                    "flipud",
                    "spixel",
                    "gray",
                    "gblur",
                    "ablur",
                    "mblur",
                    "sharp",
                    "embo",
                    "edge",
                    "dedge",
                    "pert",
                    "salt",
                    "pep",
                    "salpep",
                ]  #"alph", "aplhe",]
            else:
                if (augs == "basic") or ("basic" in augs):
                    self.augs.append([
                        "add", "scale", "addh", "mult", "drop", "cont", "noop"
                    ])
                if (augs == "form") or ("form" in augs):
                    self.augs + [
                        "scale", "aff", "paff", "elas", "noop", "pert"
                    ]
                if (augs == "valalt") or ("valalt" in augs):
                    self.augs + [
                        "mult", "mule", "inv", "fliplr", "flipud", "cropad",
                        "noop"
                    ]
                if (augs == "pxlalt") or ("pxlalt" in augs):
                    self.augs + [
                        "addg", "drop", "salt", "pep", "salpep", "noop"
                    ]
                if (augs == "imgalt") or ("imgalt" in augs):
                    self.augs + [
                        "elas",
                        "noop",
                        "spixel",
                        "gblur",
                        "ablur",
                        "mblur",
                        "sharp",
                        "embo",
                        "edge",
                        "dedge",
                    ]
                if len(augs) == 0:
                    self.augs + [
                        "add",
                        "scale",
                        "addh",
                        "drop",
                        "cont",
                        "aff",
                        "elas",
                        "noop",
                        "cropad",
                        "gray",
                        "ablur",
                        "sharp",
                        "salpep",
                    ]
            self.AUG = IAGA.SomeOf((self.minaug, self.maxaug),
                                   self.augs,
                                   random_order=True)
            """self.affineopts={"scale":self.biglfoat,
                              "translate_percent":{'x':(-40*intensity,40*intensity),'y':(-40*intensity,40*intensity)}, "translate_px":None,#moving functions
                     "rotate":(-360*intensity,360*intensity), "shear":(0*intensity,360*intensity),
                      "order":[0,1]#2,3,4,5 may be too much
                     , "cval":0,#for filling
                      "mode":"constant",#filling method
                      "deterministic":False,
                       "random_state":None}
            self.pieceaffinev={"scale"=(-0.075*intensity,0.075*intensity), "nb_rows"=(ceil(2*intensity),ceil(7*intensity)), "nb_cols"=(ceil(2*intensity),ceil(7*intensity)),
                                "order"=[0,1], "cval"=0, "mode"="constant",
                      "deterministic"=False, "random_state"=None}"""
            self.num_outs = num_outs - og_out
            self.og_out = og_out
            self.mode = mode
            self.iimg = -1
            self.iout = 0
            try:
                self.len = inputdata.shape[0]
            except:
                self.len = len(inputdata)

            def __iter__(self):
                return self

            def __next__(self):
                return (self.NM())

            def next(self):
                return (self.NM())

            def runi(self):
                if self.iimg == self.len:
                    raise StopIteration
                self.iimg += 1
                img = self.inputd[self.iimg]
                y = self.Y[self.iimg]
                out = np.broadcast_to(img, (self.num_out, *img.shape[-3:]))
                out = self.AUG.augment_images(out[self.og_out:])
                if self.og_out:
                    if len(img.shape) == 3:
                        out = np.concatenate(out, np.expand_dims(img, 0))
                    else:
                        out = np.concatenate(out, img)
                if self.format == "NCHW":
                    out = np.transpose(out, [0, 3, 1, 2])
                return ([(outi, y) for outi in out])

            def runi2(self):
                if self.iimg == self.len:
                    raise StopIteration
                if (self.iout == self.num_outs) or (self.iimg == -1):
                    self.iimg += 1
                    self.iout = 0
                    img = self.inputd[self.iimg]
                    y = self.Y[self.iimg]
                    out = np.broadcast_to(img, (self.num_out, *img.shape[-3:]))
                    self.out = self.AUG.augment_images(out[self.og_out:])
                    if self.og_out:
                        if len(img.shape) == 3:
                            self.out = np.concatenate(out,
                                                      np.expand_dims(img, 0))
                        else:
                            self.out = np.concatenate(out, img)
                    if self.format == "NCHW":
                        self.out = np.transpose(out, [0, 3, 1, 2])
                    outp = (self.out[self.iout], y)
                else:
                    self.iout += 1
                    outp = (self.out[self.iout], self.Y[self.iimg])
                return (outp)

            def rung(self):
                for ix, img in enumerate(self.inputd):
                    out = np.broadcast_to(img, (self.num_out, img.shape[-3:]))
                    out = self.AUG.augment_images(out[self.og_out:])
                    y = self.Y[ix]
                    if self.og_out:
                        if len(img.shape) == 3:
                            out = np.concatenate(out, np.expand_dims(img, 0))
                        else:
                            out = np.concatenate(out, img)
                    if self.format == "NCHW":
                        out = (np.transpose(out, [0, 3, 1, 2]))
                    for sout in out:
                        yield (sout, y)
Example #29
0
    def next(self):
        if not self.is_init:
            self.reset()
            self.is_init = True
        """Returns the next batch of data."""
        #print('in next', self.cur, self.labelcur)
        self.nbatch += 1
        batch_size = self.batch_size
        c, h, w = self.data_shape
        batch_data = nd.empty((batch_size, c, h, w))
        if self.provide_label is not None:
            batch_label = nd.empty(self.provide_label[0][1])
        i = 0
        try:
            while i < batch_size:
                label, s, bbox, landmark = self.next_sample()
                _data = self.imdecode(s)
                if _data.shape[0] != self.data_shape[1]:
                    _data = mx.image.resize_short(_data, self.data_shape[1])
                if self.rand_mirror:
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = mx.ndarray.flip(data=_data, axis=1)
                if self.blur:
                    aug_blur = iaa.Sequential([
                        iaa.OneOf([
                            iaa.GaussianBlur(sigma=(0.5, 2.5)),
                            iaa.AverageBlur(k=(2, 5)),
                            iaa.MotionBlur(k=(5, 7)),
                            iaa.BilateralBlur(d=(3, 4),
                                              sigma_color=(10, 250),
                                              sigma_space=(10, 250)),
                            iaa.imgcorruptlike.DefocusBlur(severity=1),
                            iaa.imgcorruptlike.GlassBlur(severity=1),
                            iaa.imgcorruptlike.Pixelate(severity=(1, 3)),
                            iaa.Pepper(0.01),
                            iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255),
                                                      per_channel=True),
                            iaa.imgcorruptlike.SpeckleNoise(severity=1),
                            iaa.imgcorruptlike.JpegCompression(severity=(1,
                                                                         4)),
                        ])
                    ])
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = aug_blur(images=_data)

                if self.maxpooling:
                    maxpool_aug = iaa.MaxPooling(2)
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = maxpool_aug(images=_data)

                if self.color_jittering > 0:
                    if self.color_jittering > 1:
                        _rd = random.randint(0, 1)
                        if _rd == 1:
                            _data = self.compress_aug(_data)
                    #print('do color aug')
                    _data = _data.astype('float32', copy=False)
                    #print(_data.__class__)
                    _data = self.color_aug(_data, 0.125)
                if self.nd_mean is not None:
                    _data = _data.astype('float32', copy=False)
                    _data -= self.nd_mean
                    _data *= 0.0078125
                if self.cutoff > 0:
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        #print('do cutoff aug', self.cutoff)
                        centerh = random.randint(0, _data.shape[0] - 1)
                        centerw = random.randint(0, _data.shape[1] - 1)
                        half = self.cutoff // 2
                        starth = max(0, centerh - half)
                        endh = min(_data.shape[0], centerh + half)
                        startw = max(0, centerw - half)
                        endw = min(_data.shape[1], centerw + half)
                        #print(starth, endh, startw, endw, _data.shape)
                        _data[starth:endh, startw:endw, :] = 128
                data = [_data]
                try:
                    self.check_valid_image(data)
                except RuntimeError as e:
                    logging.debug('Invalid image, skipping:  %s', str(e))
                    continue
                #print('aa',data[0].shape)
                #data = self.augmentation_transform(data)
                #print('bb',data[0].shape)
                for datum in data:
                    assert i < batch_size, 'Batch size must be multiples of augmenter output length'
                    #print(datum.shape)
                    batch_data[i][:] = self.postprocess_data(datum)
                    batch_label[i][:] = label
                    i += 1
        except StopIteration:
            if i < batch_size:
                raise StopIteration

        return io.DataBatch([batch_data], [batch_label], batch_size - i)
Example #30
0
def do_random(image, pos_list):
    # 1.先任选5种影响位置的效果之一做位置变换
    seq = iaa.Sequential([
        iaa.Sometimes(
            0.5,
            [
                iaa.Crop((0, 10)),  # 切边, (0到10个像素采样)
            ]),
        iaa.Sometimes(
            0.5,
            [
                iaa.Affine(shear={
                    'x': (-10, 10),
                    'y': (-10, 10)
                }, mode="edge"),
                iaa.Rotate(rotate=(-10, 10), mode="edge"),  # 旋转
            ]),
        iaa.Sometimes(
            0.5,
            [
                iaa.PiecewiseAffine(),  # 局部仿射
                iaa.ElasticTransformation(  # distort扭曲变形
                    alpha=(0.0, 20.0),
                    sigma=(3.0, 5.0),
                    mode="nearest"),
            ]),
        # 18种位置不变的效果
        iaa.SomeOf(
            (1, 3),
            [
                iaa.GaussianBlur(),
                iaa.AverageBlur(),
                iaa.MedianBlur(),
                iaa.Sharpen(),
                iaa.BilateralBlur(),  # 既噪音又模糊,叫双边,
                iaa.MotionBlur(),
                iaa.MeanShiftBlur(),
                iaa.GammaContrast(),
                iaa.SigmoidContrast(),
                iaa.Fog(),
                iaa.Clouds(),
                iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025)),
                iaa.Rain(nb_iterations=1,
                         drop_size=(0.05, 0.1),
                         speed=(0.04, 0.08)),
                iaa.AdditiveGaussianNoise(scale=(0, 10)),
                iaa.AdditiveLaplaceNoise(scale=(0, 10)),
                iaa.AdditivePoissonNoise(lam=(0, 10)),
                iaa.Salt((0, 0.02)),
                iaa.Pepper((0, 0.02))
            ])
    ])

    polys = [ia.Polygon(pos) for pos in pos_list]
    polygons = ia.PolygonsOnImage(polys, shape=image.shape)
    images_aug, polygons_aug = seq(images=[image], polygons=polygons)
    image = images_aug[0]
    image = polygons_aug.draw_on_image(image, size=2)

    new_polys = []
    for p in polygons_aug.polygons:
        new_polys.append(p.coords)
    polys = np.array(new_polys, np.int32).tolist()

    return image, polys