예제 #1
0
파일: train7.py 프로젝트: garyCC227/thesis
def create_custom_gen(img_gen):
    seq = iaa.Sequential(
        [iaa.MultiplyHue((0.5, 1.5)),
         iaa.imgcorruptlike.Contrast(severity=1)])
    for X_batch, y_batch in img_gen:
        hue = seq(images=X_batch.astype(np.uint8))
        yield hue, y_batch
def main():
    image = ia.quokka_square((128, 128))
    images_aug = []

    for mul in np.linspace(0.0, 2.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    for mul_hue in np.linspace(0.0, 5.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul_hue=mul_hue)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    for mul_saturation in np.linspace(0.0, 5.0, 10):
        aug = iaa.MultiplyHueAndSaturation(mul_saturation=mul_saturation)
        image_aug = aug.augment_image(image)
        images_aug.append(image_aug)

    ia.imshow(ia.draw_grid(images_aug, rows=3))

    images_aug = []
    images_aug.extend(iaa.MultiplyHue().augment_images([image] * 10))
    images_aug.extend(iaa.MultiplySaturation().augment_images([image] * 10))
    ia.imshow(ia.draw_grid(images_aug, rows=2))
예제 #3
0
def chapter_augmenters_multiplyhue():
    fn_start = "color/multiplyhue"

    aug = iaa.MultiplyHue((0.5, 1.5))
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
예제 #4
0
    def __call__(self, *args, **kwargs) -> typing.Tuple[np.ndarray, typing.List[Polygon]]:

        if self.is_training:
            resize = iaa.Resize(size=dict(longer_side=self.long_sizes,
                                          width='keep-aspect-ratio'))
            rotate = iaa.Rotate(rotate=self.angles, fit_output=True)
            resize_height = iaa.Resize(size=dict(height=self.height_ratios,
                                                 width='keep'))
            crop = iaa.CropToFixedSize(width=self.cropped_size[0], height=self.cropped_size[1])
            fix_resize = iaa.Resize(size=self.output_size)


            # blur = iaa.GaussianBlur()
            # blur = iaa.Sometimes(p=self.blur_prob,
            #                      then_list=blur)

            brightness = iaa.MultiplyBrightness((0.5, 1.5))
            brightness = iaa.Sometimes(self.color_jitter_prob, then_list=brightness)

            saturation = iaa.MultiplySaturation((0.5, 1.5))
            saturation = iaa.Sometimes(self.color_jitter_prob, then_list=saturation)

            contrast = iaa.LinearContrast(0.5)
            contrast = iaa.Sometimes(self.color_jitter_prob, then_list=contrast)

            hue = iaa.MultiplyHue()
            hue = iaa.Sometimes(self.color_jitter_prob, then_list=hue)

            augs = [resize,
                    rotate,
                    resize_height,
                    crop,
                    fix_resize,
                    brightness,
                    saturation,
                    contrast,
                    hue]
            ia = iaa.Sequential(augs)
        else:
            fix_resize = iaa.Resize(size=self.output_size)
            ia = iaa.Sequential([fix_resize])

        image = args[0]
        polygons = args[1]

        polygon_list = []
        for i in range(polygons.shape[0]):
            polygon_list.append(Polygon(polygons[i].tolist()))

        polygons_on_image = PolygonsOnImage(polygon_list, shape=image.shape)

        image_aug, polygons_aug = ia(image=image, polygons=polygons_on_image)

        return image_aug, polygons_aug.polygons
예제 #5
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])
예제 #6
0
def new_gen_train_trans(image, mask):

    image = np.array(image)
    mask = np.array(mask)

    h, w = mask.shape
    th, tw = args.train_size

    crop_scales = [1.0, 0.875, 0.75, 0.625, 0.5]
    hue_factor = 0.6
    brightness_factor = 0.6  # was 0.5
    p_flip = 0.5
    jpeg_scale = 0, 80  # was 70
    p_erase_class = 0.5

    crop_scale = np.random.choice(crop_scales)
    ch, cw = [int(x * crop_scale) for x in (h, w)]
    i = np.random.randint(0, h - ch + 1)
    j = np.random.randint(0, w - cw + 1)
    image = image[i:i + ch, j:j + cw, :]
    mask = mask[i:i + ch, j:j + cw]

    brightness = iaa.MultiplyBrightness(
        (1 - brightness_factor, 1 + brightness_factor))
    hue = iaa.MultiplyHue((1 - hue_factor, 1 + hue_factor))
    jpeg = iaa.JpegCompression(compression=jpeg_scale)

    img_transforms = iaa.Sequential([brightness, hue, jpeg])
    image = img_transforms(image=image)

    if np.random.rand() < p_flip:
        image = np.flip(image, axis=1)
        mask = np.flip(mask, axis=1)

    image = Image.fromarray(image)
    mask = Image.fromarray(mask)

    # Resize, 1 for Image.LANCZOS
    image = TF.resize(image, (th, tw), interpolation=1)
    # Resize, 0 for Image.NEAREST
    mask = TF.resize(mask, (th, tw), interpolation=0)

    # From PIL to Tensor
    image = TF.to_tensor(image)
    # Normalize
    image = TF.normalize(image, args.dataset_mean, args.dataset_std)

    # Convert ids to train_ids
    mask = np.array(mask, np.uint8)
    mask = torch.from_numpy(mask)  # Numpy array to tensor

    return image, mask
예제 #7
0
 def test_returns_correct_class(self):
     # this test is practically identical to
     # TestMultiplyToHueAndSaturation.test_returns_correct_objects__mul_hue
     aug = iaa.MultiplyHue((0.9, 1.1))
     assert isinstance(aug, iaa.WithHueAndSaturation)
     assert isinstance(aug.children, iaa.Sequential)
     assert len(aug.children) == 1
     assert isinstance(aug.children[0], iaa.WithChannels)
     assert aug.children[0].channels == [0]
     assert len(aug.children[0].children) == 1
     assert isinstance(aug.children[0].children[0], iaa.Multiply)
     assert isinstance(aug.children[0].children[0].mul, iap.Uniform)
     assert np.isclose(aug.children[0].children[0].mul.a.value, 0.9)
     assert np.isclose(aug.children[0].children[0].mul.b.value, 1.1)
예제 #8
0
    def augmentation(self, img):
        # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
        # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second
        # image.

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

        seq = iaa.Sequential([
            sometimes(iaa.JpegCompression(compression=(1, 30))),
            sometimes(iaa.GaussianBlur(sigma=(0.2, 1.0))),
            sometimes(iaa.MultiplyHue((0.9, 1.1))),
            sometimes(
                iaa.AdditiveGaussianNoise(scale=0.01 * 255, per_channel=0.5))
        ],
                             random_order=True)
        img_aug = seq(image=img)

        #cv2.imwrite("asd.jpg", img_aug)
        return img_aug
예제 #9
0
    def __init__(self, dataset: Dataset, cfg):
        self._dataset = dataset
        self.input_shape = cfg.AUGMENT.INPUT_SHAPE
        self.zoom_in = cfg.AUGMENT.ZOOM_IN
        self.min_scale = cfg.AUGMENT.MIN_SCALE
        self.max_scale = cfg.AUGMENT.MAX_SCALE
        self.max_try_times = cfg.AUGMENT.MAX_TRY_TIMES
        self.flip = cfg.AUGMENT.FLIP
        self.aspect_ratio = cfg.AUGMENT.ASPECT_RATIO
        self.translate_percent = cfg.AUGMENT.TRANSLATE_PRESENT
        self.rotate = cfg.AUGMENT.ROTATE
        self.shear = cfg.AUGMENT.SHEAR
        self.perspective_transform = cfg.AUGMENT.PERSPECTIVE_TRANSFORM
        self.brightness = cfg.AUGMENT.BRIGHTNESS
        self.hue = cfg.AUGMENT.HUE
        self.saturation = cfg.AUGMENT.SATURATION
        self.augment_background = cfg.AUGMENT.BACKGROUND
        if self.augment_background:
            self.backgrounds = [
                os.path.join('../../data/background', item)
                for item in os.listdir('../../data/background')
            ]

        self.seq = iaa.Sequential([
            iaa.Fliplr(self.flip),
            iaa.Affine(scale={
                "x": self.aspect_ratio,
                "y": self.aspect_ratio
            },
                       translate_percent={
                           "x": self.translate_percent,
                           "y": self.translate_percent
                       },
                       rotate=self.rotate,
                       shear=self.shear,
                       order=[0, 1],
                       cval=(0, 255)),
            iaa.PerspectiveTransform(scale=self.perspective_transform),
            iaa.MultiplyBrightness(self.brightness),
            iaa.MultiplySaturation(self.saturation),
            iaa.MultiplyHue(self.hue)
        ])
예제 #10
0
def _load_augmentation_aug_non_geometric():
    return iaa.Sequential([
        iaa.Sometimes(0.3, iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))),
        iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0, 3.0))),
        iaa.Sometimes(0.2, iaa.MotionBlur(k=15, angle=[-45, 45])),
        iaa.Sometimes(0.2, iaa.MultiplyHue((0.5, 1.5))),
        iaa.Sometimes(0.2, iaa.MultiplySaturation((0.5, 1.5))),
        iaa.Sometimes(
            0.34, iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)),
        iaa.Sometimes(0.34, iaa.Grayscale(alpha=(0.0, 1.0))),
        iaa.Sometimes(0.2, iaa.ChangeColorTemperature((1100, 10000))),
        iaa.Sometimes(0.1, iaa.GammaContrast((0.5, 2.0))),
        iaa.Sometimes(0.2, iaa.SigmoidContrast(gain=(3, 10),
                                               cutoff=(0.4, 0.6))),
        iaa.Sometimes(0.1, iaa.CLAHE()),
        iaa.Sometimes(0.1, iaa.HistogramEqualization()),
        iaa.Sometimes(0.2, iaa.LinearContrast((0.5, 2.0), per_channel=0.5)),
        iaa.Sometimes(0.1, iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)))
    ])
예제 #11
0
 def aug(self, img):
     seq = iaa.Sequential([
         # iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect keypoints
         # iaa.Fliplr(0.5),
         iaa.Affine(
             rotate=(0, 10),  # 0~360随机旋转
             # scale=(0.7, 1.0),#通过增加黑边缩小图片
         ),  # rotate by exactly 0~360deg and scale to 70-100%, affects keypoints
         iaa.GaussianBlur(
             sigma=(0, 3.)
         ),
         # iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
         # iaa.WithChannels(channels=0, children=iaa.Add((50, 100))),
         # iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB"),
         iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.3, 0.9)),
         iaa.MultiplyHue(mul=(0.5, 1.5))
         # iaa.Resize(0.5, 3)
     ])
     seq_def = seq.to_deterministic()
     image_aug = seq_def.augment_image(img)
     return image_aug
예제 #12
0
 def __init__(self):
     self.transform = iaa.Sequential(
         [
             iaa.Sometimes(
                 0.5,
                 iaa.SomeOf((1, 2), [
                     iaa.Fliplr(1.0),
                     iaa.Flipud(1.0),
                 ])),
             iaa.OneOf([
                 iaa.Sometimes(
                     0.3,
                     [
                         iaa.OneOf([
                             iaa.Multiply((0.7, 1.2)),
                             iaa.MultiplyElementwise((0.7, 1.2)),
                         ]),
                         iaa.OneOf([
                             iaa.MultiplySaturation((5.0, 10.0)),  # good
                             iaa.MultiplyHue((1.5, 3.0)),
                             iaa.LinearContrast((0.8, 2.0)),
                             iaa.AllChannelsHistogramEqualization(),
                         ]),
                     ]),
                 iaa.Sometimes(0.3, [
                     iaa.SomeOf((1, 2), [
                         iaa.pillike.EnhanceColor((1.1, 1.6)),
                         iaa.pillike.EnhanceSharpness((0.7, 1.6)),
                         iaa.pillike.Autocontrast(cutoff=(4, 8)),
                         iaa.MultiplySaturation((1.2, 5.1)),
                     ])
                 ])
             ]),
             iaa.Sometimes(0.3, [
                 iaa.Dropout(p=(0.01, 0.09)),
                 iaa.GaussianBlur((0.4, 1.5)),
             ]),
         ],
         random_order=True  # apply the augmentations in random order
     )
예제 #13
0
 def load_augmentation_aug_non_geometric():
     return iaa.Sequential([
         iaa.Sometimes(
             0.5,
             iaa.AdditiveGaussianNoise(loc=0,
                                       scale=(0.0, 0.05 * 255),
                                       per_channel=0.5)),
         iaa.Sometimes(
             0.5,
             iaa.OneOf([
                 iaa.GaussianBlur(sigma=(0.0, 3.0)),
                 iaa.GaussianBlur(sigma=(0.0, 5.0))
             ])),
         iaa.Sometimes(0.5, iaa.MultiplyAndAddToBrightness(mul=(0.4, 1.7))),
         iaa.Sometimes(0.5, iaa.GammaContrast((0.4, 1.7))),
         iaa.Sometimes(0.5, iaa.Multiply((0.4, 1.7), per_channel=0.5)),
         iaa.Sometimes(0.5, iaa.MultiplyHue((0.4, 1.7))),
         iaa.Sometimes(
             0.5, iaa.MultiplyHueAndSaturation((0.4, 1.7),
                                               per_channel=True)),
         iaa.Sometimes(0.5, iaa.LinearContrast((0.4, 1.7), per_channel=0.5))
     ])
예제 #14
0
    def __getitem__(self, index):
        co_ords = self.coords[index * BATCH_SIZE:(index + 1) * BATCH_SIZE]

        batch_images = np.zeros((len(co_ords), INPUT_HEIGHT, INPUT_WIDTH, 3),
                                dtype=np.float32)
        batch_heatmaps = np.zeros(
            (len(co_ords), OUTPUT_HEIGHT, OUTPUT_WIDTH, 2), dtype=np.float32)

        for i, row in enumerate(co_ords):
            images_path, x, y = row

            proc_image = image.load_img(self.image_path + images_path,
                                        target_size=(INPUT_HEIGHT,
                                                     INPUT_WIDTH))
            proc_image = image.img_to_array(proc_image, dtype='uint8')

            heatmap = heatmap_splat(y, x)  # y is height and x is width!!
            heatmap = np.expand_dims(heatmap, axis=0)

            aug_list = iaa.OneOf([
                iaa.Dropout([0.05, 0.1]),
                iaa.Sharpen((0.0, 1.0)),
                iaa.MultiplyHue((0.7, 1.4)),
                iaa.MultiplyBrightness((0.7, 1.4)),
            ])

            aug = iaa.Sequential([aug_list, iaa.Fliplr(0.5)],
                                 random_order=True)

            proc_image, heatmap = aug.augment(image=proc_image,
                                              heatmaps=heatmap)

            proc_image = np.expand_dims(proc_image, axis=0)
            proc_image = proc_image / 255.  #just for now try without normalising
            batch_images[i] = proc_image
            batch_heatmaps[i] = heatmap

        return batch_images, batch_heatmaps
예제 #15
0
    elif augmentation == 'glass_blur':
        transform = GlassBlur(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'defocus_blur':
        transform = iaa.imgcorruptlike.DefocusBlur(severity=2)
        transformed_image = transform(image=image)

    elif augmentation == 'zoom_blur':
        transform = iaa.imgcorruptlike.ZoomBlur(severity=2)
        transformed_image = transform(image=image)

    ## Color

    elif augmentation == 'multiply_hue':
        transform = iaa.MultiplyHue((0.5, 1.5))
        transformed_image = transform(image=image)

    elif augmentation == 'addto_hue':
        transform = iaa.AddToHue((-100, 100))
        transformed_image = transform(image=image)
    
    elif augmentation == 'multiply_saturation':
        transform = iaa.MultiplySaturation((0.5, 1.5))
        transformed_image = transform(image=image)
    
    elif augmentation == 'addto_saturation':
        transform = iaa.AddToSaturation((-100, 100))
        transformed_image = transform(image=image)
    
    elif augmentation == 'saturate':
def main():

    try:
        config_dirs_file = sys.argv[1] # directories file
        config_file = sys.argv[2]      # main params file
    except:
        print("Config file names not specified, setting them to default namess")
        config_dirs_file = "config_dirs.json"
        config_file = "config760.json"
    print(f'USING CONFIG FILES: config dirs:{config_dirs_file}  main config:{config_file}')    
    
    #print(type(feature_directory))
    C = cs760.loadas_json('config760.json')
    print("Running with parameters:", C)
    
    Cdirs = cs760.loadas_json(config_dirs_file)
    print("Directories:", Cdirs)
    
    C['dirs'] = Cdirs
    video_directory = C['dirs']['indir']
    feature_directory = C['dirs']['outdir']
    
    print(f'Creating feature file Dir: {feature_directory}')
    os.makedirs(feature_directory, exist_ok=True)        #if dir already exists will continue and WILL NOT delete existing files in that directory


    sometimes = lambda aug: iaa.Sometimes(C["augmentation_chance"][0], aug)
    sequential_list = [iaa.Sequential([sometimes(iaa.Fliplr(1.0))]), # horizontal flip
    iaa.Sequential([sometimes(iaa.Rotate(-5, 5))]), # rotate 5 degrees +/-
    iaa.Sequential([sometimes(iaa.CenterCropToAspectRatio(1.15))]),
    iaa.Sequential([sometimes(iaa.MultiplyBrightness((2.0, 2.0)))]), # increase brightness
    iaa.Sequential([sometimes(iaa.MultiplyHue((0.5, 1.5)))]), # change hue random
    iaa.Sequential([sometimes(iaa.RemoveSaturation(1.0))]), # effectively greyscale
    iaa.Sequential([sometimes(iaa.pillike.FilterContour())]), # edge detection
    iaa.Sequential([sometimes(iaa.AdditiveLaplaceNoise(scale=0.05*255, per_channel=True))]), # add colourful noise
    iaa.Sequential([sometimes(iaa.Invert(1))]) # invert colours
    ]


    print("Reading videos from " + video_directory)
    print("Outputting features to " + feature_directory)

    print("Loading pretrained CNN...")
    model = hub.KerasLayer(C["module_url"])  # can be used like any other kera layer including in other layers...
    print("Pretrained CNN Loaded OK")

    vids = cs760.list_files_pattern(video_directory, C["vid_type"])
    print(f'Processing {len(vids)} videos...')

    for i, vid in enumerate(vids):
        print(f'{i} Processing: {vid}')    
        vid_np = cs760.get_vid_frames(vid, 
                        video_directory, 
                        writejpgs=False,
                        writenpy=False,
                        returnnp=True)
        (framecount, frameheight, framewidth, channels) = vid_np.shape
        res_key = str(frameheight) + "-" + str(framewidth)
        #print(vid, vid_np.shape)
        outfile = os.path.splitext(vid)[0]
        
        print(f"Vid frames, h, w, c = {(framecount, frameheight, framewidth, channels)}")
        
        if C["crop_by_res"].get(res_key) is not None:
            vid_np_top = cs760.crop_image(vid_np, C["crop_by_res"][res_key])
            print(f"Cropped by resolution to {C['crop_by_res'][res_key]}")
        else:    
            vid_np_top = cs760.crop_image(vid_np, C["crop_top"])
            print(f"Cropped by default to {C['crop_top']}")

        outfile_top = outfile + "__TOP.pkl"

        for n in range((len(sequential_list) + 1)):
            if n != 0:
                vid_aug = sequential_list[n - 1](images=vid_np_top) # augments frames
                if type(vid_aug) is list:
                    vid_aug = np.asarray(vid_aug)
                batch = cs760.resize_batch(vid_aug, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                            inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                            simplenormalize=True,
                            imagenetmeansubtract=False)
                temp_outfile = outfile_top[:-4] + C["augmentation_type"][n - 1] + ".pkl"
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, temp_outfile))
            else:
                batch = cs760.resize_batch(vid_np_top, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                                inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                                simplenormalize=True,
                                imagenetmeansubtract=False)
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_top))
                print(f'Features output shape: {features.shape}')
                
        if C["crop_type"] == 'B':  # only for boston vids
            vid_np_bot = cs760.crop_image(vid_np, C["crop_bottom"])
            outfile_bot = outfile + "__BOT.pkl"  
            batch = cs760.resize_batch(vid_np_bot, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                        inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                        simplenormalize=True,
                        imagenetmeansubtract=False)
            features = extract(C, model, batch)
            cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_bot))

    print('Finished outputting features!!')
예제 #17
0
    def train_trans(self, image, mask, index):

        image_scale_x = self.epoch_image_scales_x[index]
        image_scale_y = self.epoch_image_scales_y[index]

        hue_factor = 0.6
        brightness_factor = 0.6  # was 0.5
        p_flip = 0.5
        p_imgaug = 0.7

        p_jpeg = 0.4
        jpeg_scale = 0, 70  # was 70

        p_pixel_attack = 0.0
        pixel_attack_density = 0.05

        rotation_angle = (np.random.rand() - 0.5) * 20

        image = TF.resize(image, (self.image_size[0] * image_scale_x,
                                  self.image_size[1] * image_scale_y),
                          interpolation=1)
        mask = TF.resize(mask, (self.image_size[0] * image_scale_x,
                                self.image_size[1] * image_scale_y),
                         interpolation=0)

        image = np.array(image)
        mask = np.array(mask)

        image = image[int(self.epoch_image_main_direcs[
            index, 0]):int(self.epoch_image_main_direcs[index, 0] +
                           self.train_size[0]),
                      int(self.epoch_image_main_direcs[
                          index,
                          1]):int(self.epoch_image_main_direcs[index, 1] +
                                  self.train_size[1]), :]
        mask = mask[int(self.epoch_image_main_direcs[
            index, 0]):int(self.epoch_image_main_direcs[index, 0] +
                           self.train_size[0]),
                    int(self.epoch_image_main_direcs[
                        index, 1]):int(self.epoch_image_main_direcs[index, 1] +
                                       self.train_size[1])]

        hue = iaa.MultiplyHue((1 - hue_factor, 1 + hue_factor))
        jpeg = iaa.JpegCompression(compression=jpeg_scale)
        rotator = iaa.Affine(rotate=rotation_angle)

        if np.random.rand() < p_imgaug:

            img_transforms = iaa.Sequential([jpeg, hue, rotator])
            image = img_transforms(image=image)

            rotator = iaa.Affine(rotate=rotation_angle, order=0, cval=19)

            mask_transforms = iaa.Sequential([rotator])
            mask = mask_transforms(image=mask)

        if np.random.rand() < p_flip:
            image = np.flip(image, axis=1)
            mask = np.flip(mask, axis=1)

        if np.random.rand() < p_pixel_attack:

            sel_pixels = np.random.choice(
                np.arange(self.train_size[0] * self.train_size[1]),
                int((self.train_size[0] * self.train_size[1] *
                     pixel_attack_density) // 1))

            rand_pixels = np.random.randint(0, 255, (sel_pixels.shape[0], 3))

            image = image.reshape((self.train_size[0] * self.train_size[1], 3))

            image[sel_pixels] = rand_pixels

            image = image.reshape((self.train_size[0], self.train_size[1], 3))

            mask = mask.reshape((self.train_size[0] * self.train_size[1], 1))

            mask[sel_pixels] = 19

            mask = mask.reshape((self.train_size[0], self.train_size[1]))

        image = Image.fromarray(image)
        mask = Image.fromarray(mask)

        # From PIL to Tensor
        image = TF.to_tensor(image)
        # Normalize
        image = TF.normalize(image, self.dataset_mean, self.dataset_std)

        # Convert ids to train_ids
        mask = np.array(mask, np.uint8)
        mask = torch.from_numpy(mask)  # Numpy array to tensor

        return image, mask
예제 #18
0
transforms = iaa.Sequential(
    [
        iaa.Sometimes(0.5,
                      iaa.SomeOf((1, 2), [
                          iaa.Fliplr(1.0),
                          iaa.Flipud(1.0),
                      ])),
        iaa.OneOf([
            iaa.Sometimes(0.4, [
                iaa.OneOf([
                    iaa.Multiply((0.7, 1.1)),
                    iaa.MultiplyElementwise((0.7, 1.1)),
                ]),
                iaa.OneOf([
                    iaa.MultiplySaturation((0.6, 1.5)),
                    iaa.MultiplyHue((0.6, 1.1)),
                    iaa.LinearContrast((0.8, 1.6)),
                    iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                ]),
            ]),
            iaa.Sometimes(0.5, [
                iaa.SomeOf((1, 2), [
                    iaa.pillike.EnhanceColor((0.8, 1.2)),
                    iaa.pillike.EnhanceSharpness((0.7, 1.6)),
                    iaa.pillike.Autocontrast(cutoff=(2, 5)),
                ])
            ])
        ]),
        iaa.Sometimes(0.5, [
            iaa.Dropout(p=(0.01, 0.05)),
            iaa.GaussianBlur((0.4, 1.2)),
    def __init__(self):
        self.seq = iaa.Sequential(
            [
                iaa.Fliplr(0.5),
                iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.1))),

                iaa.Sometimes(0.5, iaa.Affine(
                    rotate=(-20, 20),  # 旋转±20度
                    # shear=(-16, 16),   # 剪切变换±16度,矩形变平行四边形
                    # order=[0, 1],  # 使用最近邻插值 或 双线性插值
                    cval=0,  # 填充值
                    mode=ia.ALL  # 定义填充图像外区域的方法
                )),

                # 使用0~3个方法进行图像增强
                iaa.SomeOf((0, 3),
                           [
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.GaussianBlur((0, 2.0)),  # 高斯模糊
                                   iaa.AverageBlur(k=(1, 5)),  # 平均模糊,磨砂
                               ])),

                               # 要么运动,要么美颜
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.MotionBlur(k=(3, 11)),  # 运动模糊
                                   iaa.BilateralBlur(d=(1, 5),
                                                     sigma_color=(10, 250),
                                                     sigma_space=(10, 250)),  # 双边滤波,美颜
                               ])),

                               # 模仿雪花
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.SaltAndPepper(p=(0., 0.03)),
                                   iaa.AdditiveGaussianNoise(loc=0, scale=(0., 0.05 * 255), per_channel=False)
                               ])),

                               # 对比度
                               iaa.Sometimes(0.8, iaa.LinearContrast((0.6, 1.4), per_channel=0.5)),

                               # 锐化
                               iaa.Sometimes(0.8, iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),

                               # 整体亮度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   # 加性调整
                                   iaa.AddToBrightness((-30, 30)),
                                   # 线性调整
                                   iaa.MultiplyBrightness((0.5, 1.5)),
                                   # 加性 & 线性
                                   iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                                ])),

                               # 饱和度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToSaturation((-75, 75)),
                                   iaa.MultiplySaturation((0., 3.)),
                               ])),

                               # 色相
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToHue((-255, 255)),
                                   iaa.MultiplyHue((-3.0, 3.0)),
                               ])),

                               # 云雾
                               # iaa.Sometimes(0.3, iaa.Clouds()),

                               # 卡通化
                               # iaa.Sometimes(0.01, iaa.Cartoon()),
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
예제 #20
0
'''
changes the color temperature of images to a random value between 1100 and 10000 Kelvin
'''
aug_colorTemperature = iaa.ChangeColorTemperature((1100, 10000))
'''
Convert each image to a colorspace with a brightness-related channel, extract
that channel, multiply it by a factor between 0.5 and 1.5, add a value between
-30 and 30 and convert back to the original colorspace
'''
aug_brightness = iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30))
'''
Multiply the hue and saturation of images by random values;
Sample random values from the discrete uniform range [-50..50],and add them
'''
aug_hueSaturation = [
    iaa.MultiplyHue((0.5, 1.5)),
    iaa.MultiplySaturation((0.5, 1.5)),
    iaa.AddToHue((-50, 50)),
    iaa.AddToSaturation((-50, 50))
]
'''
Increase each pixel’s R-value (redness) by 10 to 100
'''
aug_redChannels = iaa.WithChannels(0, iaa.Add((10, 100)))

### add the augmenters ###
seq = iaa.Sequential([
    ## 0.5 is the probability, horizontally flip 50% of the images
    iaa.Fliplr(0.5),
    #iaa.Flipud(0.5),
    ## crop images from each side by 0 to 16px(randomly chosen)
예제 #21
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
예제 #22
0
파일: dataset.py 프로젝트: redman0226/TLPD
import cv2
import numpy as np

from config import config
import misc_utils

from pycocotools.coco import COCO
import torch

import imgaug as ia
import imgaug.augmenters as iaa

aug_seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=(0, 0.025 * 255)),
    iaa.MultiplyHue((0.75, 1.25)),
    iaa.Add((-80, 80))
])
heavy_aug_seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=(0, 0.025 * 255)),
    iaa.MultiplyHue((0.75, 1.25)),
    iaa.Add((-80, 80)),
    iaa.Dropout(p=(0, 0.3)),
    iaa.imgcorruptlike.MotionBlur(severity=(1, 3)),
    iaa.GammaContrast((0.0, 2.0), per_channel=True)
])
category_map = ['background', 'person']


def load_coco_json_lines(fpath, image_folder=None):
    cocoGt = COCO(fpath)
예제 #23
0
proc_image = image.img_to_array(proc_image, dtype='uint8')
#imageio.imwrite("example_segmaps.jpg", proc_image)
#proc_image = np.expand_dims(proc_image, axis=0)

heatmap = np.zeros((1, OUTPUT_HEIGHT, OUTPUT_WIDTH, 1), dtype=np.float32)
heatmap[0, 20, 140, 0] = 1.
#heatmap =heatmap[:,:,:,0]
#plt.imshow(heatmap, cmap='gray')
#plt.show()


aug_list = iaa.OneOf([
<<<<<<< Updated upstream
  #iaa.Dropout([0.02, 0.1]),
  #iaa.Sharpen((0.0, 1.0)),
  iaa.MultiplyHue((0.7, 1.4)),
  #iaa.MultiplyBrightness((0.7, 1.4))
=======
  iaa.Dropout([0.02, 0.1]),
  iaa.Sharpen((0.0, 1.0)),
  iaa.MultiplyHue((0.7, 1.4)),
  iaa.MultiplyBrightness((0.7, 1.4))
>>>>>>> Stashed changes
])

aug = iaa.Sequential([aug_list, iaa.Fliplr(0.5)], random_order=True)

proc, hm= aug.augment(image=proc_image, heatmaps=heatmap)

hm = hm[0,:,:,0]
plt.imshow(hm, cmap='gray')