Ejemplo n.º 1
0
def chapter_augmenters_channelshuffle():
    fn_start = "meta/channelshuffle"

    aug = iaa.ChannelShuffle(0.35)
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(64, 64)) for _ in range(8 * 3)],
                        cols=8,
                        rows=3)

    aug = iaa.ChannelShuffle(0.35, channels=[0, 1])
    run_and_save_augseq(fn_start + "_limited_channels.jpg",
                        aug, [ia.quokka(size=(64, 64)) for _ in range(8 * 3)],
                        cols=8,
                        rows=3)
Ejemplo n.º 2
0
def data_gen(train_data: str, seg_data: str, batch_size: int,
             n_classes: int) -> (np.ndarray, np.ndarray):
    inputs = np.array(os.listdir(train_data))
    batch = len(inputs) // batch_size
    identity = np.identity(n_classes, dtype=np.int16)

    while True:
        shuffle = np.random.permutation(len(inputs))
        for b in np.array_split(inputs[shuffle], batch):
            imgs = []
            segs = []
            for img_file in b:
                img = Image.open(os.path.join(train_data,
                                              img_file)).convert("RGB")
                img = img.resize((224, 224))
                img = np.asarray(img)

                seq = iaa.Sequential([
                    iaa.GaussianBlur(sigma=(0, 3.0)),
                    iaa.LogContrast(gain=(0.5, 1.0)),
                    iaa.ChannelShuffle(p=1.0),
                ])
                img = seq.augment_image(img)
                img = img / 255.0
                imgs.append(img)

                seg = Image.open(
                    os.path.join(seg_data,
                                 img_file.split(".")[0] + "-seg.png"))
                seg = seg.resize((224, 224))
                seg = np.asarray(seg)
                seg = identity[seg].astype(np.float32)
                segs.append(seg)

            yield np.array(imgs), np.array(segs)
Ejemplo n.º 3
0
    def augmentation(self, pil_img, pil_mask):
        input_img = np.expand_dims(pil_img, axis=0)
        input_mask = np.expand_dims(pil_mask, axis=0)
        input_mask = np.expand_dims(input_mask, axis=3)

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

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



        return output_img, output_mask
def augment(img, steering_angle):
  # Flip - odbicie lustrzane
  if random.random() > 0.5:
    img = img[:, ::-1, :]
    steering_angle = -steering_angle
  #blur - rozmazanie
  blurer = iaa.GaussianBlur(iap.Uniform(0.1, 1.0))
  img = blurer.augment_image(img)
  #shuffle
  ColorShuffle = iaa.ChannelShuffle(p=0.7)
  img = ColorShuffle.augment_image(img)
  #SuperPixels
  superpixel = iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))
  img = superpixel.augment_image(img)
  #Fog
  Clouds = iaa.Clouds()
  img = Clouds.augment_image(img)
  #Snowflakes
  # Snowflakes = iaa.Snowflakes(flake_size=(0.1, 0.4), speed=(0.01, 0.05))
  # img = Snowflakes.augment_image(img)
  #Translate
  tx = random.randint(-20,20)
  translater = iaa.Affine(translate_px = {"x":tx}, mode = 'edge')
  img = translater.augment_image(img)
  steering_angle += tx*0.02
  
  return img, steering_angle
Ejemplo n.º 5
0
    def augmentations(self, images):
        seq1 = iaa.Sequential(
            [iaa.AverageBlur(k=(2, 7)),
             iaa.MedianBlur(k=(3, 7))])
        seq2 = iaa.ChannelShuffle(p=1.0)
        seq3 = iaa.Dropout((0.05, 0.1), per_channel=0.5)
        seq4 = iaa.Sequential([iaa.Add((-15, 15)), iaa.Multiply((0.3, 1.5))])
        seq5 = iaa.Sequential([
            iaa.Crop(px=(
                0, 60
            )),  # crop images from each side by 0 to 16px (randomly chosen)
            iaa.GaussianBlur(
                sigma=(0, 1.5))  # blur images with a sigma of 0 to 3.0
        ])

        print("image augmentation beginning")
        img1 = seq1.augment_images(images)
        print("sequence 1 completed......")
        img2 = seq2.augment_images(images)
        print("sequence 2 completed......")
        img3 = seq3.augment_images(images)
        print("sequence 3 completed......")
        img4 = seq4.augment_images(images)
        print("sequence 4 completed......")
        img5 = seq5.augment_images(images)
        print("sequence 5 completed......")
        print("proceed to next augmentations")
        list = [img1, img2, img3, img4, img5]
        return list
Ejemplo n.º 6
0
 def __init__(self):
     sometimes = lambda aug: iaa.Sometimes(0.5, aug)
     self.seq = iaa.Sequential([
         iaa.OneOf([
             iaa.Multiply((0.6, 1.0), per_channel=0.5),
             iaa.Multiply((1.0, 2.0), per_channel=0.5),
         ]),
         sometimes(
             iaa.OneOf([
                 iaa.Dropout((0.02, 0.03)),
                 iaa.Salt((0.02, 0.03))
             ])
         ),
         sometimes(iaa.ChannelShuffle(1.0)),
         #sometimes(iaa.Invert(1.0, per_channel=True)),
         #sometimes(iaa.Invert(1.0)),
         #sometimes(iaa.CropAndPad(
         #    percent=(-0.1, 0.1), pad_cval=(0,255)
         #)),
         iaa.Affine(
             #scale={"x": (0.8, 1.1), "y": (0.8, 1.1)}, # scale images to 80-120% of their size, individually per axis
             rotate=(-10, 10), # rotate by -45 to +45 degrees
             mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
         ),
         sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.04))),
         sometimes(iaa.AdditiveGaussianNoise((0.02, 0.1))),
         sometimes(iaa.AdditivePoissonNoise((0.02,0.1))),
         #sometimes(iaa.Pad(
         #    percent=(0, 0.15), pad_mode=["edge"]
         #))
     ])
Ejemplo n.º 7
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
Ejemplo n.º 8
0
    def flow_from_directory(
        self,
        directory: str,
        target_size: Tuple[int, int],
        batch_size: int,
        class_mode: str,
        train: bool = True,
    ):
        batches = super().flow_from_directory(
            directory,
            target_size=target_size,
            batch_size=batch_size,
            class_mode=class_mode,
        )
        seq = iaa.Sequential([
            iaa.GaussianBlur(sigma=(0, 1)),
            iaa.ChannelShuffle(p=0.5),
            iaa.Add(value=(-0.3, 0.3)),
        ])

        while True:
            inputs, outputs = next(batches)
            if train:
                inputs = seq.augment_images(inputs)

            yield inputs, outputs
Ejemplo n.º 9
0
 def __init__(self):
     self.seq = iaa.Sequential(
         [
             iaa.ChannelShuffle(0.5),
             iaa.Sometimes(
                 0.5,
                 iaa.OneOf([
                     iaa.GaussianBlur(
                         (0, 3.0
                          )),  # blur images with a sigma between 0 and 3.0
                     iaa.AverageBlur(
                         k=(2, 7)
                     ),  # blur image using local means with kernel sizes between 2 and 7
                     iaa.MedianBlur(
                         k=(3, 11)
                     ),  # blur image using local medians with kernel sizes between 2 and 7
                 ])),
             iaa.Sometimes(
                 0.5,
                 iaa.AdditiveGaussianNoise(
                     loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)),
             iaa.Sometimes(
                 0.5,
                 iaa.BlendAlphaFrequencyNoise(
                     exponent=(-4, 0),
                     foreground=iaa.Multiply((0.5, 1.5), per_channel=True),
                     background=iaa.LinearContrast((0.5, 2.0)))),
             # iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))),
             # iaa.Sometimes(0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1)))
         ],
         random_order=True)
Ejemplo n.º 10
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 build_augmentor(self):
     self.augmentor = iaa.Sometimes(
         0.5,
         iaa.OneOf([
             iaa.ChannelShuffle(p=1.0),
             iaa.Invert(p=1.0, per_channel=True),
             iaa.AddToHueAndSaturation((-45, 45), per_channel=True),
             iaa.Emboss(alpha=1, strength=(0.5, 1.0))
         ]))
    def get_ill_seq(self):
        light_change = 50
        seq = iaa.Sequential([
            # 全局调整,含有颜色空间调整
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.WithColorspace(
                        to_colorspace="HSV",
                        from_colorspace="RGB",
                        children=iaa.OneOf([
                            iaa.WithChannels(0, iaa.Add((-5, 5))),
                            iaa.WithChannels(1, iaa.Add((-20, 20))),
                            iaa.WithChannels(
                                2, iaa.Add((-light_change, light_change))),
                        ])),
                    iaa.Grayscale((0.2, 0.6)),
                    iaa.ChannelShuffle(1),
                    iaa.Add((-light_change, light_change)),
                    iaa.Multiply((0.5, 1.5)),
                ])),

            # # dropout阴影模仿,暂时不使用,转而使用了自定义的阴影模仿
            # iaa.Sometimes(0.5, iaa.OneOf([
            #     iaa.Alpha((0.2, 0.7), iaa.CoarseDropout(p=0.2, size_percent=(0.02, 0.005)))
            # ])),

            # 椒盐噪声
            iaa.Sometimes(
                0.5,
                iaa.OneOf(
                    [iaa.Alpha((0.2, 0.6), iaa.SaltAndPepper((0.01, 0.03)))])),

            # 图像反转
            iaa.Sometimes(0.5, iaa.OneOf([
                iaa.Invert(1),
            ])),

            # 对比度调整
            iaa.Sometimes(0.5,
                          iaa.OneOf([
                              iaa.ContrastNormalization((0.5, 1.5)),
                          ])),
            iaa.Sometimes(
                0.5,
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(0, (3, 6)),
                    iaa.AdditivePoissonNoise((3, 6)),
                    iaa.JpegCompression((30, 60)),
                    iaa.GaussianBlur(sigma=1),
                    iaa.AverageBlur((1, 3)),
                    iaa.MedianBlur((1, 3)),
                ])),
        ])
        return seq
Ejemplo n.º 13
0
 def __init__(self, iaalist=None):
     if iaalist is None:
         iaalist = iaa.Sequential([
             iaa.Sometimes(0.5, iaa.ChannelShuffle(0.3)),
             iaa.Sometimes(0.5, iaa.MultiplyHue((0.5, 1.5))),
             iaa.Sometimes(0.5, iaa.AddToHueAndSaturation((-50, 50), per_channel=True)),
             iaa.Sometimes(0.5, iaa.Fliplr(0.5)),
             iaa.Sometimes(0.5, iaa.Flipud(0.5)),
             iaa.Sometimes(0.5, iaa.Rotate((-50, 50)))
         ], random_order=True)
     self.transformSet = iaalist
     self.outscale = random.choice([0.8, 0.85, 0.9, 0.95])
def main():
    img = ia.quokka(size=(128, 128), extract="square")

    aug = iaa.ChannelShuffle()
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))

    aug = iaa.ChannelShuffle(p=0.1)
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))

    aug = iaa.ChannelShuffle(p=1.0, channels=[0, 1])
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))

    aug = iaa.ChannelShuffle(p=1.0, channels=[1, 2])
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))

    aug = iaa.ChannelShuffle(p=1.0, channels=[1, 1, 2])
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))

    aug = iaa.ChannelShuffle(p=1.0, channels=ia.ALL)
    imgs_aug = aug.augment_images([img] * 64)
    ia.imshow(ia.draw_grid(imgs_aug))
Ejemplo n.º 15
0
def getAugPipe():
    aug = iaa.Sequential(
        [
            iaa.SomeOf((1, 3), [
                iaa.PiecewiseAffine(scale=(0.02, 0.03)),
                iaa.Add(value=(-40, 40), per_channel=0.5),
                iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255), per_channel=True),
                iaa.Multiply((0.5, 1.5), per_channel=0.5)
            ], random_order=True),
            iaa.ChannelShuffle()
        ],
        random_order=True
    )
    return aug
Ejemplo n.º 16
0
    def __init__(self, args, samples_directory, basemodel_preprocess, generator_type, shuffle):
        self.samples_directory = samples_directory
        self.model_type = args["type"]
        self.base_model = args["base_model"]
        self.basemodel_preprocess = basemodel_preprocess
        self.batch_size = args["batch_size"]
        self.sample_files = []
        self.img_dims = (args["img_dim"], args["img_dim"])  # dimensions that images get resized into when loaded
        self.age_deviation = args["age_deviation"]
        self.predict_gender = args["predict_gender"] if "predict_gender" in args else False
        self.range_mode = args["range_mode"] if "range_mode" in args else False
        self.age_classes_number = age_ranges_number() if self.range_mode else AGES_NUMBER
        self.dataset_size = None
        self.generator_type = generator_type
        self.shuffle = shuffle

        sometimes = lambda aug: iaa.Sometimes(0.5, aug)
        self.seq = iaa.Sequential(
            [
               iaa.Fliplr(0.5),
               iaa.ChannelShuffle(0.25),
               sometimes(
                 iaa.OneOf([
                    iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                    iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.2), per_channel=True)
                 ])
               ),
               iaa.OneOf([
                 iaa.GaussianBlur((0, 0.4)),
                 iaa.MedianBlur((1,3)),
                 iaa.MotionBlur(k=(3,5), angle=(0,360))
               ]),
               iaa.JpegCompression((0,50)),
               iaa.OneOf([
                 iaa.Multiply((0.7, 1.4), per_channel=0.5),
                 iaa.GammaContrast((0.7, 1.4), per_channel=0.5)
               ]),
               iaa.Grayscale(alpha=(0.0, 1.0)),
               sometimes(iaa.CropAndPad(
                 percent=(-0.05, 0.1),
                 pad_mode=imgaug.ALL,
                 pad_cval=(0, 255)
               ))
            ], random_order=True  # horizontally flip 50% of all images
        )
        self.load_sample_files()
        self.indexes = np.arange(self.dataset_size)

        self.on_epoch_end()  # for training data: call ensures that samples are shuffled in first epoch if shuffle is set to True
 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
Ejemplo n.º 18
0
 def flow_from_directory(self, directory, target_size, batch_size,
                         class_mode):
     batches = super().flow_from_directory(directory,
                                           target_size=target_size,
                                           batch_size=batch_size,
                                           class_mode=class_mode)
     seq = iaa.Sequential([
         iaa.GaussianBlur(sigma=(0, 3.0)),
         #iaa.MotionBlur(k=(3, 10)),
         iaa.ChannelShuffle(p=0.5),
         iaa.Add(value=(-0.3, 0.3))
     ])
     while True:
         inputs, outputs = next(batches)
         inputs = seq.augment_images(inputs)
         yield [inputs, outputs], outputs
Ejemplo n.º 19
0
 def __init__(self, agumentation={}):
     try:
         self.aug = iaa.Sequential([
             iaa.Resize((224, 224)),
             iaa.Sometimes(
                 float(agumentation['GaussianBlur_P']),
                 iaa.GaussianBlur(
                     sigma=(agumentation["GaussianBlur_sigma"]))),
             iaa.Sometimes(
                 float(agumentation['MedianBlur_P']),
                 iaa.imgcorruptlike.MotionBlur(
                     severity=agumentation['MedianBlur_Severity'])),
             iaa.Sometimes(
                 agumentation['DefocusBlur_P'],
                 iaa.imgcorruptlike.DefocusBlur(
                     severity=agumentation['DefocusBlur_Severity'])),
             iaa.Sometimes(
                 agumentation['Cutout_P'],
                 iaa.Cutout(
                     nb_iterations=agumentation['Cutout_cum_of_cuts']),
             ),
             iaa.ChannelShuffle(agumentation['ChannelSuffle_P']),
             iaa.Sometimes(
                 agumentation['ElasticTransformation_P'],
                 iaa.ElasticTransformation(
                     alpha=agumentation['ElasticTransformation_alpha'],
                     sigma=agumentation['ElasticTransformation_sigma']),
             ),
             iaa.Sometimes(
                 agumentation['PerspectiveTransform_P'],
                 iaa.PerspectiveTransform(
                     scale=agumentation['PerspectiveTransform_scale']),
             ),
         ],
                                   random_order=True)
     except:
         self.aug = iaa.Sequential([
             iaa.Resize((224, 224)),
             iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))),
             iaa.Fliplr(0.5),
             iaa.Affine(rotate=(-20, 20), mode='symmetric'),
             iaa.Dropout2d(p=0.5),
             iaa.Sometimes(0.5, iaa.Cutout(nb_iterations=2)),
             iaa.Sometimes(0.25, iaa.Invert(0.25, per_channel=0.5)),
             iaa.Sometimes(0.25, iaa.Add((-40, 40))),
             iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)
         ])
Ejemplo n.º 20
0
    def augmentation(self, pil_img, pil_mask):
        input_img = np.expand_dims(pil_img, axis=0)
        input_mask = np.expand_dims(pil_mask, axis=0)
        input_mask = np.expand_dims(input_mask, axis=3)

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

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

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

        return output_img, output_mask
Ejemplo n.º 21
0
def generate_augmentation(aug_pad, aug_affine, aug_ch_suffle, aug_dropout,
                          aug_AGN, aug_fliplr, aug_flipud, aug_percent):
    '''
    This function creates an augment for dataset transform to use. Args
    determine which augments are used. The augments are predetermined and
    appliend in same order as args.
    Args:
        aug_pad (bool): Boolean value if pad filter will be used.
        aug_affine (bool): Boolean value if affine rotation filter will be used.
        aug_ch_suffle (bool): Boolean value if channel suffle filter will be used.
        aug_dropout (bool): Boolean value if dropout filter will be used.
        aug_AGN (bool): Boolean value if Additive Gaussian Noice filter will be used.
        aug_fliplr (bool): Boolean value if left-right flip filter will be used.
        aug_flipud (bool): Boolean value if up-down flip filter will be used.
        aug_percent (float): Float between 0 and 1. The determined augments are 
                             applied randomly based on aug_percent.
    Return:
        augment (imgaug augmenter): Imgaug augmenter that can be 
                                    used to perform transformations on images.
    '''

    #Create all augments
    resize = iaa.Resize(224)
    pad = iaa.Pad(px=(0, 4))
    affine = iaa.Affine(rotate=(-10, 10))
    ch_suffle = iaa.ChannelShuffle(0.35)
    dropout = iaa.Dropout(p=(0, 0.2))
    AGN = iaa.AdditiveGaussianNoise(loc=0, scale=(0, 15))
    flip_lr = iaa.Fliplr(0.5)
    flip_ud = iaa.Flipud(0.5)

    #Put augments to list and choose only if aug_ parameter is true
    aug_list = [pad, affine, ch_suffle, dropout, AGN, flip_lr, flip_ud]
    use_aug_list = [
        aug_pad, aug_affine, aug_ch_suffle, aug_dropout, aug_AGN, aug_fliplr,
        aug_flipud
    ]
    aug_list = [aug_list[i] for i in np.where(use_aug_list)[0]]
    #Create the augment and use aug_percent to determine how oftern augments are used
    augment = iaa.Sequential(
        [resize, iaa.Sometimes(aug_percent, iaa.Sequential(aug_list))])
    return augment
Ejemplo n.º 22
0
def augmentations1(images):
    seq1 = iaa.Sequential(
        [iaa.AverageBlur(k=(2, 7)),
         iaa.MedianBlur(k=(3, 11))])

    seq2 = iaa.ChannelShuffle(p=1.0)
    seq3 = iaa.Dropout((0.05, 0.1), per_channel=0.5)
    seq4 = iaa.Sequential([iaa.Add((-15, 15)), iaa.Multiply((0.3, 1.5))])
    print("image augmentation beginning")
    images_aug1 = seq1.augment_images(images)
    print("sequence 1 completed......")
    images_aug2 = seq2.augment_images(images)
    print("sequence 2 completed......")
    images_aug3 = seq3.augment_images(images)
    print("sequence 3 completed......")
    images_aug4 = seq4.augment_images(images)
    print("sequence 4 completed......")
    print("proceed to next augmentations")
    list = [images_aug1, images_aug2, images_aug3, images_aug4]
    return list
Ejemplo n.º 23
0
def augment_object(image, N=20):
    images = np.array([image for _ in range(N)], dtype=np.uint8)

    seq = iaa.Sequential(
        [
            # Strengthen or weaken the contrast in each image.
            #iaa.ContrastNormalization((0.7, 1.50)),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            #iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
            #iaa.PiecewiseAffine(scale=(0.01, 0.07)),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.50, 1.50), per_channel=.50),
            iaa.ChannelShuffle(0.5),
        ],
        random_order=True)

    images_aug = seq(images=images)
    return images_aug
        train_label[os.path.splitext(image)[0] +
                    "_flip_horizontal.jpeg"] = train_label[image]

flip_vert = iaa.Flipud(1.0)
for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        img = cv2.imread(os.path.join(train_img_dir, image))
        img_aug = flip_vert(image=img)
        cv2.imwrite(
            os.path.join(save_dir,
                         os.path.splitext(image)[0] + "_flip_vertical.jpeg"),
            img_aug)
        train_label[os.path.splitext(image)[0] +
                    "_flip_vertical.jpeg"] = train_label[image]

channel_flip = iaa.ChannelShuffle(1.0)
for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        img = cv2.imread(os.path.join(train_img_dir, image))
        img_aug = channel_flip(image=img)
        cv2.imwrite(
            os.path.join(save_dir,
                         os.path.splitext(image)[0] + "_channel_flip.jpeg"),
            img_aug)
        train_label[os.path.splitext(image)[0] +
                    "_channel_flip.jpeg"] = train_label[image]

for image in os.listdir(train_img_dir):
    if image.endswith(".jpeg"):
        os.rename(os.path.join(train_img_dir, image),
                  os.path.join(save_dir, image))
path= 'Image A*/train/*.xml'
import cv2



seq = iaa.Sequential([
    iaa.Fliplr(p=0),# basically this is original one
    iaa.Sometimes(0.05,(iaa.Crop(px=(22, 45),keep_size=True))), # crop images from each side by 0 to 16px (randomly chosen)
    iaa.Sometimes(0.5,(iaa.Fliplr(1))), # horizontally flip 50% of the images
    iaa.Sometimes(0.02,iaa.GaussianBlur(sigma=(5, 7.0))), # blur images with a sigma of 0 to 3.0
    iaa.Sometimes(0.02 ,iaa.ImpulseNoise(p=(0.6,1))),
    iaa.Sometimes(0.02 ,iaa.EdgeDetect(alpha=(0.09,1))),
    #iaa.AddToBrightness(add=(100,124)),
    iaa.Sometimes(0.02 ,iaa.Canny(alpha=(0.8,0.9))),
    iaa.Sometimes(0.5 ,iaa.Grayscale(alpha=1.00)),
    iaa.Sometimes(0.5 ,iaa.ChannelShuffle(p=1)),
    #iaa.Sometimes(0.02 ,(iaa.geometric.Affine( scale=2,rotate=22,order=1))),
    iaa.Sometimes(0.5 ,iaa.Cartoon(blur_ksize=(11,13))),
    iaa.Sometimes(0.02 ,iaa.CenterCropToAspectRatio(1)),
    iaa.Sometimes(0.02 ,iaa.CenterCropToFixedSize(100,100)),
    iaa.Sometimes(0.12 ,iaa.ChangeColorTemperature(kelvin=(2222,3333))),
    #iaa.segmentation(),
    iaa.Sometimes(0.12 ,iaa.CLAHE(clip_limit=(4,8))),
    iaa.Sometimes(0.8 ,iaa.Rotate(rotate=(-90,90),order=1))
])

plt.figure(figsize=(12,12))


ls=glob.glob(path)
res=[]
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
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
Ejemplo n.º 28
0
    def img_augment(self, image, bboxes=None, p=.7):
        """
        使用imgaug库进行的图像增强。
        :param image: np.array, images.
        :param bboxes: np.array, bboxes of object detection.
        :param n: max number of augmenters.
        :return: image and bboxes after augmenting.
        """
        if random.random() > p:
            return image, bboxes

        h, w, _ = image.shape
        if bboxes is not None:
            bboxes_list = bboxes.tolist()
            max_bbox = np.concatenate([
                np.min(bboxes[:, 0:2], axis=0),
                np.max(bboxes[:, 2:4], axis=0)
            ],
                                      axis=-1)
            top = max_bbox[1]
            left = max_bbox[0]
            bottom = h - max_bbox[3]
            right = w - max_bbox[2]
        else:
            top = int(h * 0.25)
            left = int(w * 0.25)
            bottom = int(h * 0.25)
            right = int(w * 0.25)

        while True:
            new_bndbox_list = []
            seq = iaa.Sequential(children=[
                # color
                iaa.Sometimes(
                    COLOR['probability'],
                    iaa.SomeOf(2, [
                        iaa.Multiply(COLOR['multiply'],
                                     per_channel=COLOR['per_channel']),
                        iaa.AddToHueAndSaturation(
                            COLOR['add_to_hue_value'],
                            per_channel=COLOR['per_channel']),
                        iaa.GammaContrast(COLOR['gamma'],
                                          per_channel=COLOR['per_channel']),
                        iaa.ChannelShuffle()
                    ])),

                # blur
                iaa.Sometimes(
                    BLUR['probability'],
                    iaa.OneOf([
                        iaa.GaussianBlur(sigma=BLUR['gaussian_sigma']),
                        iaa.AverageBlur(k=BLUR['average_k']),
                        iaa.MedianBlur(k=BLUR['median_k'])
                    ])),

                # noise
                iaa.Sometimes(
                    NOISE['probability'],
                    iaa.OneOf([
                        iaa.AdditiveGaussianNoise(
                            scale=NOISE['gaussian_scale'],
                            per_channel=NOISE['per_channel']),
                        iaa.SaltAndPepper(p=NOISE['salt_p'],
                                          per_channel=NOISE['per_channel']),
                        iaa.Dropout(p=NOISE['drop_out_p'],
                                    per_channel=NOISE['per_channel']),
                        iaa.CoarseDropout(p=NOISE['drop_out_p'],
                                          size_percent=(0.05, 0.1),
                                          per_channel=NOISE['per_channel'])
                    ])),

                # crop and pad
                iaa.Sometimes(
                    CROP['probability'],
                    iaa.Crop(px=(random.randint(0, top),
                                 random.randint(0, right),
                                 random.randint(0, bottom),
                                 random.randint(0, left)),
                             keep_size=False)),
                iaa.Sometimes(
                    PAD['probability'],
                    iaa.Pad(
                        percent=PAD['size'],
                        # pad_mode=ia.ALL,
                        pad_mode=[
                            "constant", "edge", "linear_ramp", "maximum",
                            "mean", "median", "minimum"
                        ] if bboxes is not None else ia.ALL,
                        pad_cval=(0, 255))),

                # flip
                iaa.Flipud(FLIPUD['probability']),
                iaa.Fliplr(FLIPLR['probability']),
                iaa.Sometimes(PIECEWISEAFFINE['probability'],
                              iaa.PiecewiseAffine(scale=(0.01, 0.04)))
            ])
            seq_det = seq.to_deterministic()  # 保持坐标和图像同步改变
            # 读取图片
            image_aug = seq_det.augment_images([image])[0]
            n_h, n_w, _ = image_aug.shape
            if bboxes is not None:
                for box in bboxes_list:
                    x1, y1, x2, y2, c = tuple(box)
                    bbs = ia.BoundingBoxesOnImage([
                        ia.BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2),
                    ],
                                                  shape=image.shape)

                    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
                    n_x1 = int(
                        max(
                            1,
                            min(image_aug.shape[1],
                                bbs_aug.bounding_boxes[0].x1)))
                    n_y1 = int(
                        max(
                            1,
                            min(image_aug.shape[0],
                                bbs_aug.bounding_boxes[0].y1)))
                    n_x2 = int(
                        max(
                            1,
                            min(image_aug.shape[1],
                                bbs_aug.bounding_boxes[0].x2)))
                    n_y2 = int(
                        max(
                            1,
                            min(image_aug.shape[0],
                                bbs_aug.bounding_boxes[0].y2)))
                    new_bndbox_list.append([n_x1, n_y1, n_x2, n_y2, c])
                bboxes_aug = np.array(new_bndbox_list)
            else:
                bboxes_aug = bboxes
            # 长宽比太大的图片不要,产生新的image和bboxes
            if 1 / 3 <= image_aug.shape[0] / image_aug.shape[1] <= 3:
                break
        return image_aug, bboxes_aug
Ejemplo n.º 29
0
    iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
    iaa.JpegCompression(compression=(50, 99)),
    iaa.MultiplyHueAndSaturation(mul_hue=(0.5, 1.5)),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50))),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50)),
                               to_colorspace=[iaa.CSPACE_Lab, iaa.CSPACE_HSV]),
    iaa.MaxPooling(2),
    iaa.MinPooling((1, 2)),
    # iaa.Superpixels(p_replace=(0.1, 0.2), n_segments=(16, 128)),
    iaa.Clouds(),
    iaa.Fog(),
    iaa.AdditiveGaussianNoise(scale=0.1 * 255, per_channel=True),
    iaa.Dropout(p=(0, 0.2)),

    # iaa.WithChannels(0, iaa.Affine(rotate=(0, 0))),
    iaa.ChannelShuffle(0.35),
    iaa.WithColorspace(to_colorspace="HSV",
                       from_colorspace="RGB",
                       children=iaa.WithChannels(0, iaa.Add((0, 50)))),
    #
    iaa.WithHueAndSaturation([
        iaa.WithChannels(0, iaa.Add((-30, 10))),
        iaa.WithChannels(
            1, [iaa.Multiply((0.5, 1.5)),
                iaa.LinearContrast((0.75, 1.25))])
    ]),
    #
    # # iaa.Canny()
    # iaa.FastSnowyLandscape(
    #     lightness_threshold=140,
    #     lightness_multiplier=2.5
Ejemplo n.º 30
0
    def augmentation_of_image(self, test_image, output_path):
        self.test_image = test_image
        self.output_path = output_path
        #define the Augmenters

        #properties: A range of values signifies that one of these numbers is randmoly chosen for every augmentation for every batch

        # Apply affine transformations to each image.
        rotate = iaa.Affine(rotate=(-90, 90))
        scale = iaa.Affine(scale={
            "x": (0.5, 0.9),
            "y": (0.5, 0.9)
        })
        translation = iaa.Affine(translate_percent={
            "x": (-0.15, 0.15),
            "y": (-0.15, 0.15)
        })
        shear = iaa.Affine(shear=(-2, 2))
        #plagio parallhlogrammo wihthin a range (-8,8)
        zoom = iaa.PerspectiveTransform(
            scale=(0.01, 0.15),
            keep_size=True)  # do not change the output size of the image
        h_flip = iaa.Fliplr(1.0)
        # flip horizontally all images (100%)
        v_flip = iaa.Flipud(1.0)
        #flip vertically all images
        padding = iaa.KeepSizeByResize(
            iaa.CropAndPad(percent=(0.05, 0.25))
        )  #positive values correspond to padding 5%-25% of the image,but keeping the origial output size of the new image

        #More augmentations
        blur = iaa.GaussianBlur(
            sigma=(0, 1.22)
        )  # blur images with a sigma 0-2,a number ofthis range is randomly chosen everytime.Low values suggested for this application
        contrast = iaa.contrast.LinearContrast((0.75, 1.5))
        #change the contrast by a factor of 0.75 and 1.5 sampled randomly per image
        contrast_channels = iaa.LinearContrast(
            (0.75, 1.5), per_channel=True
        )  #and for 50% of all images also independently per channel:
        sharpen = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))
        #sharpen with an alpha from 0(no sharpening) - 1(full sharpening) and change the lightness form 0.75 to 1.5
        gauss_noise = iaa.AdditiveGaussianNoise(
            scale=0.111 * 255, per_channel=True
        )  #some random gaussian noise might occur in cell images,especially when image quality is poor
        laplace_noise = iaa.AdditiveLaplaceNoise(
            scale=(0, 0.111 * 255)
        )  #we choose to be in a small range, as it is logical for training the cell images

        #Brightness
        brightness = iaa.Multiply(
            (0.35, 1.65
             ))  #change brightness between 35% or 165% of the original image
        brightness_channels = iaa.Multiply(
            (0.5, 1.5), per_channel=0.75
        )  # change birghtness for 25% of images.For the remaining 75%, change it, but also channel-wise.

        #CHANNELS (RGB)=(Red,Green,Blue)
        red = iaa.WithChannels(0, iaa.Add(
            (10,
             100)))  #increase each Red-pixels value within the range 10-100
        red_rot = iaa.WithChannels(0, iaa.Affine(
            rotate=(0, 45)))  #rotate each image's red channel by 0-45 degrees
        green = iaa.WithChannels(1, iaa.Add(
            (10,
             100)))  #increase each Green-pixels value within the range 10-100
        green_rot = iaa.WithChannels(1, iaa.Affine(
            rotate=(0,
                    45)))  #rotate each image's green channel by 0-45 degrees
        blue = iaa.WithChannels(2, iaa.Add(
            (10,
             100)))  #increase each Blue-pixels value within the range 10-100
        blue_rot = iaa.WithChannels(2, iaa.Affine(
            rotate=(0, 45)))  #rotate each image's blue channel by 0-45 degrees

        #colors
        channel_shuffle = iaa.ChannelShuffle(1.0)
        #shuffle all images of the batch
        grayscale = iaa.Grayscale(1.0)
        hue_n_saturation = iaa.MultiplyHueAndSaturation(
            (0.5, 1.5), per_channel=True
        )  #change hue and saturation with this range of values for different values
        add_hue_saturation = iaa.AddToHueAndSaturation(
            (-50, 50),
            per_channel=True)  #add more hue and saturation to its pixels
        #Quantize colors using k-Means clustering
        kmeans_color = iaa.KMeansColorQuantization(
            n_colors=(4, 16)
        )  #quantizes to k means 4 to 16 colors (randomly chosen). Quantizes colors up to 16 colors

        #Alpha Blending
        blend = iaa.AlphaElementwise((0, 1.0), iaa.Grayscale((0, 1.0)))
        #blend depending on which value is greater

        #Contrast augmentors
        clahe = iaa.CLAHE(tile_grid_size_px=((3, 21), [
            0, 2, 3, 4, 5, 6, 7
        ]))  #create a clahe contrast augmentor H=(3,21) and W=(0,7)
        histogram = iaa.HistogramEqualization(
        )  #performs histogram equalization

        #Augmentation list of metadata augmentors
        OneofRed = iaa.OneOf([red])
        OneofGreen = iaa.OneOf([green])
        OneofBlue = iaa.OneOf([blue])
        contrast_n_shit = iaa.OneOf(
            [contrast, brightness, brightness_channels])
        SomeAug = iaa.SomeOf(
            2, [rotate, scale, translation, shear, h_flip, v_flip],
            random_order=True)
        SomeClahe = iaa.SomeOf(
            2, [
                clahe,
                iaa.CLAHE(clip_limit=(1, 10)),
                iaa.CLAHE(tile_grid_size_px=(3, 21)),
                iaa.GammaContrast((0.5, 2.0)),
                iaa.AllChannelsCLAHE(),
                iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True)
            ],
            random_order=True)  #Random selection from clahe augmentors
        edgedetection = iaa.OneOf([
            iaa.EdgeDetect(alpha=(0, 0.7)),
            iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0))
        ])
        # Search in some images either for all edges or for directed edges.These edges are then marked in a black and white image and overlayed with the original image using an alpha of 0 to 0.7.
        canny_filter = iaa.OneOf([
            iaa.Canny(),
            iaa.Canny(alpha=(0.5, 1.0), sobel_kernel_size=[3, 7])
        ])
        #choose one of the 2 canny filter options
        OneofNoise = iaa.OneOf([blur, gauss_noise, laplace_noise])
        Color_1 = iaa.OneOf([
            channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation,
            kmeans_color
        ])
        Color_2 = iaa.OneOf([
            channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation,
            kmeans_color
        ])
        Flip = iaa.OneOf([histogram, v_flip, h_flip])

        #Define the augmentors used in the DA
        Augmentors = [
            SomeAug, SomeClahe, SomeClahe, edgedetection, sharpen,
            canny_filter, OneofRed, OneofGreen, OneofBlue, OneofNoise, Color_1,
            Color_2, Flip, contrast_n_shit
        ]

        for i in range(0, 14):
            img = cv2.imread(test_image)  #read you image
            images = np.array(
                [img for _ in range(14)], dtype=np.uint8
            )  # 12 is the size of the array that will hold 8 different images
            images_aug = Augmentors[i].augment_images(
                images
            )  #alternate between the different augmentors for a test image
            cv2.imwrite(
                os.path.join(output_path,
                             test_image + "new" + str(i) + '.jpg'),
                images_aug[i])  #write all changed images