コード例 #1
0
 def __init__(self):
     self.seq = iaa.Sequential(
         [
             # iaa.Fliplr(0.5), # horizontal flips
             # Small gaussian blur with random sigma between 0 and 0.5.
             # But we only blur about 50% of all images.
             iaa.GaussianBlur(sigma=(0, 0.5)),
             iaa.MotionBlur(k=[5, 12], angle=[-45, 45]),
             # Strengthen or weaken the contrast in each image.
             iaa.Alpha([0.25, 0.35, 0.55],
                       iaa.Sequential([
                           iaa.GaussianBlur(sigma=(60, 100)),
                           iaa.LinearContrast((1, 3)),
                           iaa.Add((0, 30))
                       ])),
             #iaa.Lambda(radial_blur),
             # 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.LinearContrast((0.5, 1.0)),
             iaa.MultiplyHueAndSaturation((0.5, 1.5))
             # iaa.Alpha([0.25, 0.35], iaa.Clouds()),
         ],
         random_order=False)
コード例 #2
0
def chapter_alpha_masks_introduction():
    # -----------------------------------------
    # example introduction
    # -----------------------------------------
    import imgaug as ia
    from imgaug import augmenters as iaa

    ia.seed(2)

    # Example batch of images.
    # The array has shape (8, 128, 128, 3) and dtype uint8.
    images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)],
                      dtype=np.uint8)

    seqs = [
        iaa.BlendAlpha((0.0, 1.0),
                       foreground=iaa.MedianBlur(11),
                       per_channel=True),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0),
                                   per_channel=False),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0),
                                   background=iaa.LinearContrast((0.5, 2.0)),
                                   per_channel=0.5),
        iaa.BlendAlphaFrequencyNoise(foreground=iaa.Affine(rotate=(-10, 10),
                                                           translate_px={
                                                               "x": (-4, 4),
                                                               "y": (-4, 4)
                                                           }),
                                     background=iaa.AddToHueAndSaturation(
                                         (-40, 40)),
                                     per_channel=0.5),
        iaa.BlendAlphaSimplexNoise(foreground=iaa.BlendAlphaSimplexNoise(
            foreground=iaa.EdgeDetect(1.0),
            background=iaa.LinearContrast((0.5, 2.0)),
            per_channel=True),
                                   background=iaa.BlendAlphaFrequencyNoise(
                                       exponent=(-2.5, -1.0),
                                       foreground=iaa.Affine(rotate=(-10, 10),
                                                             translate_px={
                                                                 "x": (-4, 4),
                                                                 "y": (-4, 4)
                                                             }),
                                       background=iaa.AddToHueAndSaturation(
                                           (-40, 40)),
                                       per_channel=True),
                                   per_channel=True,
                                   aggregation_method="max",
                                   sigmoid=False)
    ]

    cells = []
    for seq in seqs:
        images_aug = seq(images=images)
        cells.extend(images_aug)

    # ------------

    save("alpha", "introduction.jpg", grid(cells, cols=8, rows=5))
コード例 #3
0
def augmentation(images, annotations, distributed=False):
    height, width, _ = images[0].shape
    keypoints = [KeypointsOnImage(
        [
            Keypoint(x=0, y=annotation[0]*height),
            Keypoint(x=annotation[1]*width, y=annotation[2]*height),
            Keypoint(x=width, y=annotation[3]*height)
        ], shape=(height, width)) for annotation in annotations]

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),
            iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.125))),
            iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01*255), per_channel=0.5),
            iaa.Sometimes(0.5, drop_light_shadow_generator),
            iaa.SomeOf((0, 3), [
                iaa.Multiply((0.75, 1.5), per_channel=False),
                iaa.BlendAlphaFrequencyNoise(
                    exponent=(-1, 1),
                    foreground=iaa.Multiply((0.7, 1.2)),
                    background=iaa.LinearContrast((0.75, 1.5))
                ),
                iaa.MotionBlur(k=[3, 9]),
                iaa.Add((-20, 20), per_channel=0.5),
                iaa.LinearContrast((0.75, 1.5), per_channel=0.5)
            ], random_order=True)
        ], random_order=False).to_deterministic()

    if distributed:
        data = np.asarray(Parallel(n_jobs=multiprocessing.cpu_count())(delayed(seq)(image=img, keypoints=kps) for img, kps in zip(images, keypoints)), dtype=object)
        augmented_images, augmented_keypoints = data[:, 0], data[:, 1]
    else:
        augmented_images, augmented_keypoints = seq(images=images, keypoints=keypoints)

    augmented_annotations = []
    for i, k in enumerate(augmented_keypoints):
        if k[0].x > k[2].x:  k = k[::-1]

        peak = (-1, -1)
        if annotations[i][1] == -1 and annotations[i][2] == -1:
            x, y = [k[0].x, k[2].x], [k[0].y, k[2].y]
        elif k[1].x < 0 or (k[0].y < 0 and k[1].y < 0) or (k[0].y > height and k[1].y > height):
            x, y = [k[1].x, k[2].x], [k[1].y, k[2].y]
        elif k[1].x > width or (k[1].y < 0 and k[2].y < 0) or (k[1].y > height and k[2].y > height):
            x, y = [k[0].x, k[1].x], [k[0].y, k[1].y]
        else:
            x, y = [k[0].x, k[1].x, k[2].x], [k[0].y, k[1].y, k[2].y]
            peak = (x[1]/width, np.interp(x[1], x, y)/height)
        augmented_annotation = [np.interp(0, x, y)/height, peak[0], peak[1], np.interp(width, x, y)/height]

        if augmented_annotation[0] < 0 and augmented_annotation[3] < 0:
            augmented_annotation = [0, -1, -1, 0]
        elif augmented_annotation[0] > 1 and augmented_annotation[2] > 1 and augmented_annotation[3] > 1:
            augmented_annotation = [1, -1, -1, 1]

        augmented_annotations.append(augmented_annotation)
    return augmented_images, np.asarray(augmented_annotations)
コード例 #4
0
 def __init__(self,num_of_augms=0):
     self.num_of_augms=num_of_augms
     self.aug=iaa.OneOf([
         iaa.Sequential([
             iaa.LinearContrast(alpha=(0.75, 1.5)),
             iaa.Fliplr(0.5)
         ]),
         iaa.Sequential([
             iaa.Grayscale(alpha=(0.1, 0.9)),
             iaa.Affine(
                 translate_percent={"y": (-0.15, 0.15)}
             )
         ]),
         iaa.Sequential([
             iaa.LinearContrast((0.6, 1.4)),
             iaa.ShearX((-10, 10))
         ]),
         iaa.Sequential([
             iaa.GaussianBlur(sigma=(0, 1)),
             iaa.ShearY((-10, 10))
         ]),
         iaa.Sequential([
             iaa.Cutout(nb_iterations=(1, 2), size=0.1, squared=False),
             iaa.Multiply((0.8, 1.2), per_channel=0.25),
             iaa.Fliplr(0.5),
         ]),
         iaa.Sequential([
             iaa.LinearContrast((0.6, 1.4)),
             iaa.Affine(
                 translate_percent={"x": (-0.25, 0.25)}
             )
         ]),
         iaa.Sequential([
             iaa.Cutout(nb_iterations=(1, 5), size=0.1, squared=False),
             iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 15), per_channel=0.5),
             iaa.Affine(
                 scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
             )
         ]),
         iaa.Sequential([
             iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
             iaa.GaussianBlur(sigma=(0, 2)),
             iaa.Affine(
                 scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}
             )])
         # iaa.Sequential([
         #     iaa.Cutout(nb_iterations=(1, 5), size=0.05, squared=False),
         #     iaa.Grayscale(alpha=(0.0, 0.50)),
         #     iaa.ScaleX((0.75, 1.25))
         # ]),
         # iaa.Sequential([
         #     iaa.LinearContrast((0.8, 1.2), per_channel=True),
         #     iaa.PerspectiveTransform(scale=(0.01, 0.15))
         # ])
     ])
コード例 #5
0
def linear_contrast(img_array, offset=0.3):
    """对比度:线性变换"""
    value = offset * uniform(-1, 1) + 1
    try:
        seq = iaa.Sequential([
            iaa.LinearContrast(value)
        ])
    except AttributeError:
        np.random.bit_generator = np.random._bit_generator
        seq = iaa.Sequential([
            iaa.LinearContrast(value)
        ])
    return seq(images=[img_array])[0]
コード例 #6
0
def augment_video(video):
    # video has shape (3, L, h, w)

    if isinstance(video, torch.Tensor):
        video = video.permute(1, 2, 3, 0).numpy()
    imgaug.seed(torch.randint(0, 99999999, size=(1, )).item())
    aug = iaa.Sequential([
        iaa.Crop(percent=(0, 0.1)),
        iaa.LinearContrast((0.75, 1.2)),
        iaa.Multiply((0.75, 1.333), per_channel=0.5),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_percent={
                       "x": (-0.1, 0.1),
                       "y": (-0.1, 0.1)
                   },
                   rotate=(-10, 10),
                   shear=(-10, 10))
    ])

    aug_det = aug.to_deterministic()
    results = np.zeros(video.shape, video.dtype)
    for i in range(video.shape[0]):
        results[i] = aug_det.augment_image(video[i])

    return torch.from_numpy(results).permute(3, 0, 1, 2)
コード例 #7
0
ファイル: parser.py プロジェクト: gkorepanov/bonnetal
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     assert kwargs['h'] == 144
     assert kwargs['w'] == 256
     self.augmentations = iaa.Sequential([
         iaa.Fliplr(0.5),
         iaa.Affine(scale={
             'x': (0.95, 1.05),
             'y': 1
         },
                    rotate=iap.Normal(0, 10)),
         iaa.Sometimes(
             0.2,
             iaa.OneOf([
                 iaa.Sequential([
                     iaa.pillike.EnhanceBrightness((0.2, 0.9)),
                     iaa.LinearContrast((0.75, 1.5)),
                     iaa.AdditiveGaussianNoise(
                         loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5),
                     iaa.GaussianBlur(sigma=(0, 1)),
                 ]),
                 iaa.pillike.EnhanceBrightness((1.1, 1.6))
             ]))
     ],
                                         random_order=False)
コード例 #8
0
def generate_sequential_augmenter(width: int = 512,
                                  height: int = 512) -> iaa.Sequential:
    seq = iaa.Sequential(
        [
            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
            # Apply affine transformations to each image.
            # Scale/zoom them, otate them and shear them.
            iaa.Sometimes(
                0.5,
                iaa.Affine(scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
                           rotate=(-25, 25),
                           shear=(-8, 8))),
            # Crops to a given size, uniformly and skipping
            # 10% of the image in all edges
            iaa.size.CropToFixedSize(width,
                                     height,
                                     position=(iap.Uniform(
                                         0.1, 0.9), iap.Uniform(0.1, 0.9))),
            iaa.Fliplr(0.5),  # horizontal flips
            iaa.Flipud(0.5),  # vertical flips
            # Strengthen or weaken the contrast in each image.
            iaa.LinearContrast((0.85, 1.15)),
            # Make some images brighter and some darker.
            # In 10% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.9, 1.1), per_channel=0.2)
        ],
        random_order=False)  # apply augmenters in the explicit order
    return (seq)
コード例 #9
0
ファイル: augment.py プロジェクト: peternara/crnn.pytorch-ocr
 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)
コード例 #10
0
def chapter_augmenters_linearcontrast():
    fn_start = "contrast/linearcontrast"

    aug = iaa.LinearContrast((0.6, 1.4))
    run_and_save_augseq(fn_start + ".jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)

    aug = iaa.LinearContrast((0.6, 1.4), per_channel=True)
    run_and_save_augseq(fn_start + "_per_channel.jpg",
                        aug,
                        [ia.quokka(size=(128, 128)) for _ in range(4 * 2)],
                        cols=4,
                        rows=2)
コード例 #11
0
ファイル: dataset.py プロジェクト: Lifulifu/array-align
    def __init__(self,
                 window_size=None,
                 window_expand=2,
                 down_sample=4,
                 equalize=False,
                 morphology=False,
                 pixel_size=10,
                 sigma=1):
        super().__init__(window_size=window_size,
                         window_expand=window_expand,
                         down_sample=down_sample,
                         equalize=equalize,
                         morphology=morphology,
                         pixel_size=pixel_size)

        self.sigma = sigma
        self.aug_seq = aug.Sequential([
            aug.HorizontalFlip(0.5),
            aug.VerticalFlip(0.5),
            aug.Sometimes(0.5, aug.GaussianBlur(sigma=(0, 0.2))),
            aug.LinearContrast((0.8, 1.2)),
            aug.AdditiveGaussianNoise(scale=(0.0, 0.05 * 255)),
            aug.Multiply((0.5, 1.0)),
            aug.Affine(scale=(0.7, 1),
                       translate_percent={
                           "x": (-0.2, 0.2),
                           "y": (-0.2, 0.2)
                       },
                       rotate=(-5, 5))
        ],
                                      random_order=True)
コード例 #12
0
ファイル: dataset.py プロジェクト: Lifulifu/array-align
 def __init__(self,
              window_resize=None,
              window_expand=2,
              equalize=False,
              morphology=False,
              channel_mode='max',
              pixel_size=10):
     super().__init__(window_resize=window_resize,
                      window_expand=window_expand,
                      equalize=equalize,
                      morphology=morphology,
                      channel_mode=channel_mode,
                      pixel_size=pixel_size)
     self.aug_seq = aug.Sequential(
         [
             # aug.HorizontalFlip(0.5),
             # aug.VerticalFlip(0.5),
             aug.Sometimes(0.2, aug.GaussianBlur(sigma=(0, 0.2))),
             aug.LinearContrast((0.8, 1.2)),
             # aug.AdditiveGaussianNoise(scale=(0.0, 0.05*255)),
             aug.Multiply((0.8, 1.0)),
             aug.Affine(translate_percent={
                 "x": (-0.2, 0.2),
                 "y": (-0.2, 0.2)
             },
                        rotate=(-5, 5),
                        mode='wrap')
         ],
         random_order=True)
     self.cache = dict()
コード例 #13
0
def get_seq(flag_normal, flag_affine, flag_noise, flag_snow, flag_cloud,
            flag_fog, flag_snowflakes, flag_rain, flag_dropout):
    if flag_normal:
        seq_list = [
            iaa.SomeOf((1, 2), [
                iaa.LinearContrast((0.5, 2.0), per_channel=0.5),
                iaa.Grayscale(alpha=(0.0, 1.0)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
            ])
        ]
    else:
        seq_list = []

    if flag_affine:
        seq_list.append(
            iaa.Sometimes(
                0.7,
                iaa.Affine(scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
                           translate_percent={
                               "x": (-0.2, 0.2),
                               "y": (-0.2, 0.2)
                           },
                           rotate=(-25, 25),
                           shear=(-8, 8))))

    if flag_noise:
        seq_list.append(
            iaa.OneOf([
                iaa.GaussianBlur((0, 3.0)),
                iaa.AverageBlur(k=(2, 7)),
                iaa.MedianBlur(k=(3, 11)),
            ]))

    if flag_snow:
        seq_list.append(
            iaa.FastSnowyLandscape(lightness_threshold=(100, 255),
                                   lightness_multiplier=(1.0, 4.0)))
    elif flag_cloud:
        seq_list.append(iaa.Clouds())
    elif flag_fog:
        seq_list.append(iaa.Fog())
    elif flag_snowflakes:
        seq_list.append(
            iaa.Snowflakes(flake_size=(0.2, 0.7), speed=(0.007, 0.03)))
    elif flag_rain:
        seq_list.append(iaa.Rain())

    if flag_dropout:
        seq_list.append(
            iaa.OneOf([
                iaa.Dropout((0.01, 0.1), per_channel=0.5),
                iaa.CoarseDropout((0.03, 0.15),
                                  size_percent=(0.02, 0.05),
                                  per_channel=0.2),
            ]))

    return iaa.Sequential(seq_list, random_order=True)
コード例 #14
0
ファイル: transforms.py プロジェクト: zebrajack/morefusion
    def transform(self, in_data):
        augmenter = iaa.Sequential([
            iaa.LinearContrast(alpha=(0.8, 1.2)),
            iaa.WithColorspace(
                to_colorspace="HSV",
                from_colorspace="RGB",
                children=iaa.Sequential([
                    # SV
                    iaa.WithChannels(
                        (1, 2),
                        iaa.Multiply(mul=(0.8, 1.2), per_channel=True),
                    ),
                    # H
                    iaa.WithChannels(
                        (0, ),
                        iaa.Multiply(mul=(0.95, 1.05), per_channel=True),
                    ),
                ]),
            ),
            iaa.GaussianBlur(sigma=(0, 1.0)),
            iaa.KeepSizeByResize(children=iaa.Resize((0.25, 1.0))),
        ])

        augmenter = augmenter.to_deterministic()
        for index in self._indices:
            in_data[index] = augmenter.augment_image(in_data[index])
コード例 #15
0
    def __call__(self, sample):
        #image, keypoints = sample['image'], sample['points']
        image, keypoints, valids = sample
        im = np.array(image)
        kpt = KeypointsOnImage([Keypoint(x, y) for x, y in keypoints],
                               shape=im.shape)

        seq = iaa.Sequential([
            iaa.Multiply((1.2, 1.5)),
            iaa.Affine(scale=(0.8, 1.2),
                       translate_px={
                           "x": (-20, 20),
                           "y": (-20, 20)
                       },
                       rotate=(5, 10),
                       shear=(1, 3)),
            iaa.LinearContrast((0.8, 1.2))
        ])

        img_aug, kpt_aug = seq(image=im, keypoints=kpt)
        keypoints_aug = None
        for i, point in enumerate(kpt_aug):
            if i == 0:
                keypoints_aug = [[point.x, point.y]]
            else:
                keypoints_aug = np.append(keypoints_aug, [[point.x, point.y]],
                                          axis=0)
        img_aug = Image.fromarray(img_aug)
        keypoints_aug = np.array(keypoints_aug)
        return img_aug, keypoints_aug, valids
コード例 #16
0
    def __getitem__(self, idx):

        pair = self.pair_list[idx]

        input_img = cv2.imread(pair[0])
        input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
        img_label = pair[1]  # normal is 0

        # shape must be deterministic so it can be reused
        if self.shape_augs is not None:
            shape_augs = self.shape_augs.to_deterministic()
            input_img = shape_augs.augment_image(input_img)

        if self.input_augs is not None:
            input_img = iaa.Sequential(
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(
                            (0, 3.0)),  # gaussian blur with random sigma
                        iaa.MedianBlur(
                            k=(3, 5)),  # median with random kernel sizes
                        iaa.AdditiveGaussianNoise(
                            loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                    ]),
                    iaa.Add((-26, 26)),
                    iaa.AddToHueAndSaturation((-10, 10)),
                    iaa.LinearContrast((0.8, 1.2), per_channel=1.0),
                ],
                random_order=True).augment_image(input_img)

        return input_img, img_label
コード例 #17
0
def _lane_argue(*, image, lane_src):
    lines_tuple = [[(float(pt['x']), float(pt['y'])) for pt in line_spec] for line_spec in lane_src['Lines']]
    lss = [ia_LineString(line_tuple_spec) for line_tuple_spec in lines_tuple]

    lsoi = LineStringsOnImage(lss, shape=image.shape)
    color_shift = iaa.OneOf([
        iaa.GaussianBlur(sigma=(0.5, 1.5)),
        iaa.LinearContrast((1.5, 1.5), per_channel=False),
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(0, iaa.Multiply((0.7, 1.3)))),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(1, iaa.Multiply((0.1, 2)))),
        iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB,
                           children=iaa.WithChannels(2, iaa.Multiply((0.5, 1.5)))),
    ])
    posion_shift = iaa.SomeOf(4, [
        iaa.Fliplr(),
        iaa.Crop(percent=([0, 0.2], [0, 0.15], [0, 0], [0, 0.15]), keep_size=True),
        iaa.TranslateX(px=(-16, 16)),
        iaa.ShearX(shear=(-15, 15)),
        iaa.Rotate(rotate=(-15, 15))
    ])
    aug = iaa.Sequential([
        iaa.Sometimes(p=0.6, then_list=color_shift),
        iaa.Sometimes(p=0.6, then_list=posion_shift)
    ], random_order=True)
    batch = ia.Batch(images=[image], line_strings=[lsoi])
    batch_aug = list(aug.augment_batches([batch]))[0]  # augment_batches returns a generator
    image_aug = batch_aug.images_aug[0]
    lsoi_aug = batch_aug.line_strings_aug[0]
    lane_aug = [[dict(x=kpt.x, y=kpt.y) for kpt in shapely_line.to_keypoints()] for shapely_line in lsoi_aug]
    return image_aug, dict(Lines=lane_aug)
コード例 #18
0
def train(model, dataset_dir, annotations_file, epochs):
    from imgaug import augmenters as iaa
    """Train the mask rcnn model.

    Inputs:
        model: Model to train.
        dataset_dir: Root directory of dataset.
        epochs: Epochs to train for. If given two values, the network
                heads are first trained for epochs[0] before training
                the full model to epochs[1].
    """
    # Training dataset
    dataset_train = FoodDataset()
    dataset_train.load_food(dataset_dir, 'train', annotations_file)
    dataset_train.prepare()
    print('[*] Training dataset:')
    print(' ', 'Image Count: {}'.format(len(dataset_train.image_ids)))
    print(' ', 'Class Count: {}'.format(dataset_train.num_classes))
    print(' ', 'Classes:', dataset_train.class_names)

    #Validation dataset
    dataset_val = FoodDataset()
    dataset_val.load_food(dataset_dir, 'val', annotations_file)
    dataset_val.prepare()
    print('[*] Validation dataset:')
    print(' ', 'Image Count: {}'.format(len(dataset_val.image_ids)))
    print(' ', 'Class Count: {}'.format(dataset_val.num_classes))
    print(' ', 'Classes:', dataset_val.class_names)

    # Input augmentations
    augmentation = iaa.SomeOf(
        (0, None),
        [
            iaa.Fliplr(0.5),  # Left-right flip with probability 0.5
            iaa.Flipud(0.5),  # Up-down flip with probability 0.5
            iaa.Add((-40, 40)),  # Add delta value to brightness
            iaa.LinearContrast((0.8, 1.2)),  # Transform contrast
            iaa.AddToSaturation((-40, 40)),  # Add delta value to saturation
            iaa.AddToHue((-20, 20))  # Add delta value to hue
        ])

    # Train network heads first if two epoch values are given
    if len(epochs) > 1:
        print('[*] Training network heads.')
        model.train(dataset_train,
                    dataset_val,
                    learning_rate=config.LEARNING_RATE,
                    augmentation=augmentation,
                    epochs=epochs[0],
                    layers='heads')
    else:
        epochs.append(epochs[0])

    print('[*] Training network.')
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                augmentation=augmentation,
                epochs=epochs[1],
                layers='all')
コード例 #19
0
 def train_augmentors(self):
     shape_augs = [
         iaa.Resize((512, 512), interpolation='nearest'),
         # iaa.CropToFixedSize(width=800, height=800),
     ]
     #
     sometimes = lambda aug: iaa.Sometimes(0.2, aug)
     input_augs = [
         iaa.OneOf([
             iaa.GaussianBlur((0, 3.0)),  # gaussian blur with random sigma
             iaa.MedianBlur(k=(3, 5)),  # median with random kernel sizes
             iaa.AdditiveGaussianNoise(loc=0,
                                       scale=(0.0, 0.05 * 255),
                                       per_channel=0.5),
         ]),
         sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)),
         # move pixels locally around (with random strengths)
         sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))
                   ),  # sometimes move parts of the image around
         sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))),
         iaa.Sequential([
             iaa.Add((-26, 26)),
             iaa.AddToHueAndSaturation((-20, 20)),
             iaa.LinearContrast((0.75, 1.25), per_channel=1.0),
         ],
                        random_order=True),
         sometimes([
             iaa.CropAndPad(percent=(-0.05, 0.1),
                            pad_mode="reflect",
                            pad_cval=(0, 255)),
         ]),
     ]
     return shape_augs, input_augs
コード例 #20
0
    def randomDataAugument(self, num_trans):
        # 以下で定義する変換処理の内ランダムに幾つかの処理を選択
        seq = iaa.SomeOf(num_trans, [
            iaa.Affine(rotate=(-90, 90), order=1, mode="edge"),
            iaa.Fliplr(1.0),
            iaa.OneOf([
                # 同じ系統の変換はどれか1つが起きるように 1つにまとめる
                iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge"),
                iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge")
            ]),
            iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge"),
            iaa.OneOf([
                iaa.AdditiveGaussianNoise(scale=[0.05 * 255, 0.2 * 255]),
                iaa.AdditiveLaplaceNoise(scale=[0.05 * 255, 0.2 * 255]),
                iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True)
            ]),
            iaa.OneOf([
                iaa.LogContrast((0.5, 1.5)),
                iaa.LinearContrast((0.5, 2.0))
            ]),
            iaa.OneOf([
                iaa.GaussianBlur(sigma=(0.5, 1.0)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))
            ]),
            iaa.Invert(1.0)
        ], random_order=True)

        return seq
コード例 #21
0
 def __init__(self):
     sometimes = lambda aug: iaa.Sometimes(0.5, aug)
     self.seq = iaa.Sequential(
         [
             # horisontal_flip
             iaa.Fliplr(0.3),
             # Blur each image with varying strength using
             # gaussian blur (sigma between 0 and 3.0),
             # average/uniform blur (kernel size between 2x2 and 7x7)
             # median blur (kernel size between 3x3 and 11x11).
             iaa.OneOf([
                 iaa.GaussianBlur((0, 3.0)),
                 iaa.AverageBlur(k=(2, 7)),
                 iaa.MedianBlur(k=(3, 11)),
             ]),
             # Sharpen each image, overlay the result with the original
             # image using an alpha between 0 (no sharpening) and 1
             # (full sharpening effect).
             sometimes(iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.5))),
             # Add gaussian noise to some images.
             sometimes(
                 iaa.AdditiveGaussianNoise(
                     loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)),
             # Add a value of -5 to 5 to each pixel.
             sometimes(iaa.Add((-5, 5), per_channel=0.5)),
             # Change brightness of images (80-120% of original value).
             sometimes(iaa.Multiply((0.8, 1.2), per_channel=0.5)),
             # Improve or worsen the contrast of images.
             sometimes(iaa.LinearContrast((0.5, 2.0), per_channel=0.5)),
         ],
         # do all of the above augmentations in random order
         random_order=True)
コード例 #22
0
 def augmentator(images):
     """Apply data augmentation"""
     augmenter = iaa.Sequential([
         # Invert pixel values on 25% images
         iaa.Invert(0.25, per_channel=0.5),
         # Blur 30% of images
         iaa.Sometimes(
             .3,
             iaa.OneOf([
                 iaa.GaussianBlur(sigma=(0.0, 3.0)),
                 iaa.AverageBlur(k=(2, 2)),
                 iaa.MedianBlur(k=(1, 3)),
             ]),
         ),
         # Do embossing or sharpening
         iaa.OneOf([
             iaa.Sometimes(.2, iaa.Emboss(alpha=(0.0, .3),
                                          strength=(.2, .8))),
             iaa.Sometimes(
                 .2, iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))),
         ]),
         # Add one noise (or none)
         iaa.OneOf([
             iaa.Dropout((0, 0.01)),
             iaa.AdditiveGaussianNoise(scale=0.01 * 255),
             iaa.SaltAndPepper(0.01),
             iaa.Noop(),
         ]),
         # Convert to grayscale
         iaa.Sometimes(.2, iaa.Grayscale(alpha=(0.0, 1.0))),
         iaa.Sometimes(.4, iaa.LinearContrast((0.5, 1.5), per_channel=0.5)),
         # iaa.PiecewiseAffine(scale=(0.005, 0.05)),
     ])
     images = augmenter(images=images)
     return images
コード例 #23
0
 def __init__(self):
     st = lambda aug: iaa.Sometimes(0.5, aug)
     self.seq = iaa.Sequential([
         st(iaa.Pad(percent=((0, 0.2), (0, 0.2), (0, 0.2), (0, 0.2)), keep_size=False)),
         #
         #st(iaa.Crop(percent=([0.0, 0.1], [0.00, 0.1], [0.0, 0.1], [0.0, 0.1]), keep_size=False)),
         st(iaa.Affine(scale=(0.9, 1.0), rotate=(-30, 30), shear=(-5, 5),
                       translate_px={"x": (-30, 30), "y": (-10, 10)},
                       fit_output=True)),
         # st(iaa.PerspectiveTransform((0,0.1),fit_output=True)),
         # st(iaa.MultiplyAndAddToBrightness(mul=(0.6, 1.5), add=(0, 30))),
         st(iaa.ChangeColorTemperature(kelvin=(3000, 9100))),
         st(iaa.LinearContrast((0.75, 1.5))),
         st(iaa.GaussianBlur((0, 0.2))),
         # st(iaa.PerspectiveTransform(scale=0.05,)),
         st(iaa.AddToHueAndSaturation((-20, 20))),
         #
         st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 16),
                                      per_channel=True)),  # add gaussian noise to images
         # # # #st(iaa.Dropout((0.0, 0.1), per_channel=0.5)),  # randomly remove up to 10% of the pixels
         # # # change brightness of images (by -10 to 10 of original value)
         st(iaa.Add((-40, 40), per_channel=True)),
         # # change brightness of images (50-150% of original value)
         st(iaa.Multiply((0.5, 1.5), per_channel=True)),
     ])
コード例 #24
0
def augment_sequential():
    return iaa.Sequential([
        iaa.SomeOf(
            (0, 3),
            [  # 每次使用0~3个Augmenter来处理图片
                iaa.DirectedEdgeDetect(alpha=(0.0, 0.3),
                                       direction=(0.0, 1.0)),  # 边缘检测,只检测某些方向的
                iaa.OneOf([  # 每次以下Augmenters中选择一个来变换
                    iaa.GaussianBlur((0, 1.0)),
                    iaa.AverageBlur(k=(2, 3)),
                ]),
                iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.0)),  # 锐化
                iaa.SimplexNoiseAlpha(
                    iaa.OneOf([
                        iaa.EdgeDetect(alpha=(0.0, 0.5)),
                        iaa.DirectedEdgeDetect(alpha=(0.0, 0.5),
                                               direction=(0.5, 1.0)),
                    ])),
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                iaa.OneOf([
                    iaa.Dropout((0.01, 0.3), per_channel=0.5),  # 随机丢弃像素
                    iaa.CoarseDropout((0.03, 0.15),
                                      size_percent=(0.02, 0.1),
                                      per_channel=0.2),  # 随机丢弃某位置某通道像素
                ]),
                iaa.Add((-50, 50), per_channel=0.5),  # 像素值成比例增加/减小(特指亮度)
                iaa.AddToHueAndSaturation((-50, 50)),  # 增加色相、饱和度
                iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
                iaa.Grayscale(alpha=(0.0, 1.0)),
            ])
    ])
コード例 #25
0
ファイル: transforms.py プロジェクト: jinglingzhua/blinkblink
def get_training_augmentation(**kwargs):
    seq = iaa.Sequential([
        iaa.Resize({
            'height': kwargs['crop_sz'],
            'width': kwargs['crop_sz']
        }),
        iaa.flip.Fliplr(p=0.5),
        iaa.OneOf(
            [iaa.GaussianBlur(sigma=(0.0, 1.0)),
             iaa.MotionBlur(k=(3, 5))]),
        iaa.OneOf([
            iaa.GammaContrast((0.8, 1.0)),
            iaa.LinearContrast((0.75, 1.5)),
            iaa.LogContrast((0.8, 1.0))
        ]),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        iaa.Crop(px=(0, 2 * (kwargs['crop_sz'] - kwargs['inp_sz']))),
        iaa.Resize({
            'height': kwargs['inp_sz'],
            'width': kwargs['inp_sz']
        })
    ])
    return seq
コード例 #26
0
 def __init__(self):
     self.seq = iaa.Sequential(
         [
             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.Add((-10, 10), per_channel=0.5)),
             iaa.Sometimes(0.5, iaa.AddToHueAndSaturation((-20, 20))),
             iaa.Sometimes(
                 0.5,
                 iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                         first=iaa.Multiply(
                                             (0.5, 1.5), per_channel=True),
                                         second=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)
コード例 #27
0
ファイル: dataAugment.py プロジェクト: wucng/torchTools
def imgaug_img2(img):
    seq = iaa.Sequential([
        iaa.Fliplr(0.5),  # horizontal flips
        iaa.Crop(percent=(0, 0.1)),  # random crops
        # Small gaussian blur with random sigma between 0 and 0.5.
        # But we only blur about 50% of all images.
        iaa.Sometimes(
            0.5,
            iaa.GaussianBlur(sigma=(0, 0.5))
        ),
        # Strengthen or weaken the contrast in each image.
        iaa.LinearContrast((0.75, 1.5)),
        # Add gaussian noise.
        # For 50% of all images, we sample the noise once per pixel.
        # For the other 50% of all images, we sample the noise per pixel AND
        # channel. This can change the color (not only brightness) of the
        # pixels.
        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
        # Make some images brighter and some darker.
        # In 20% of all cases, we sample the multiplier once per channel,
        # which can end up changing the color of the images.
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.
        iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
            translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
            rotate=(-25, 25),
            shear=(-8, 8)
        )
    ], random_order=True)  # apply augmenters in random order

    images_aug = seq.augment_images([img])
    return images_aug[0]
コード例 #28
0
def imgaugRGB(img):

    print(img.shape)
    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((0, 2), [
                iaa.GaussianBlur((0.0, 2.0)),
                iaa.AverageBlur(k=(3, 7)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.BilateralBlur(d=(1, 7)),
                iaa.MotionBlur(k=(3, 7))
            ]),
            #color
            iaa.SomeOf(
                (0, 2),
                [
                    #iaa.WithColorspace(),
                    iaa.AddToHueAndSaturation((-20, 20)),
                    #iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                    iaa.Grayscale(alpha=(0.0, 0.2))
                ]),
            #brightness
            iaa.OneOf([
                iaa.Sequential([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.5, 1.5), per_channel=0.5)
                ]),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.Multiply((0.5, 1.5), per_channel=0.5),
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Multiply(
                                            (0.5, 1.5), per_channel=0.5),
                                        second=iaa.ContrastNormalization(
                                            (0.5, 2.0), per_channel=0.5))
            ]),
            #contrast
            iaa.SomeOf((0, 2), [
                iaa.GammaContrast((0.5, 1.5), per_channel=0.5),
                iaa.SigmoidContrast(
                    gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
            ]),
            #arithmetic
            iaa.SomeOf((0, 3), [
                iaa.AdditiveGaussianNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditiveLaplaceNoise(scale=(0, 0.05), per_channel=0.5),
                iaa.AdditivePoissonNoise(lam=(0, 8), per_channel=0.5),
                iaa.Dropout(p=(0, 0.05), per_channel=0.5),
                iaa.ImpulseNoise(p=(0, 0.05)),
                iaa.SaltAndPepper(p=(0, 0.05)),
                iaa.Salt(p=(0, 0.05)),
                iaa.Pepper(p=(0, 0.05))
            ]),
            #iaa.Sometimes(p=0.5, iaa.JpegCompression((0, 30)), None),
        ],
        random_order=True)
    return seq.augment_image(img)
コード例 #29
0
ファイル: base_dataset.py プロジェクト: jolibrain/joliGAN
 def __init__(self,with_mask=True):
     self.with_mask = with_mask
     self.seq = iaa.Sequential(
         [
         iaa.SomeOf((0, 5),
             [
                 sometimes(iaa.Superpixels(p_replace=(0, 0.5), n_segments=(100, 200))), # convert images into their superpixel representation
                 iaa.OneOf([
                     iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                     iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                     iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                 ]),
                 iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                 iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                 # search either for all edges or for directed edges,
                 # blend the result with the original image using a blobby mask
                 iaa.SimplexNoiseAlpha(iaa.OneOf([
                     iaa.EdgeDetect(alpha=(0.5, 1.0)),
                     iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                 ])),
                 iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                 #iaa.OneOf([
                 #    iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                 #    iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                 #]),
                 iaa.Invert(0.05, per_channel=True), # invert color channels
                 iaa.Add((-5, 5), per_channel=0.5), # change brightness of images
                 iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                 # either change the brightness of the whole image (sometimes
                 # per channel) or change the brightness of subareas
                 iaa.OneOf([
                     iaa.Multiply((0.5, 1.5), per_channel=0.5),
                     iaa.FrequencyNoiseAlpha(
                         exponent=(-4, 0),
                         first=iaa.Multiply((0.5, 1.5), per_channel=True),
                         second=iaa.LinearContrast((0.5, 2.0))
                     )
                 ]),
                 iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                 iaa.Grayscale(alpha=(0.0, 1.0))
             ],
             random_order=True
         )
     ],
     random_order=True
 )
コード例 #30
0
def augmenter_1(p=0.99):
    """Create augmenter.

    Contains no coordinate transforms.

    Parameters
    ----------
    p : float
        Number in [0, 1] representing the probability of a random augmentation happening.

    Returns
    -------
    seq : iaa.Augmenter
        Augmenter where each augmentation was manually inspected and makes
        sense.

    """
    subsubseq_1 = iaa.Multiply(mul=(0.8, 1.2))
    subsubseq_2 = iaa.Sequential([iaa.Sharpen(alpha=(0, 1))])

    subsubseq_3 = iaa.Sequential([iaa.EdgeDetect(alpha=(0, 0.9))])

    subsubseq_4 = iaa.OneOf(
        [iaa.GaussianBlur((0, 3.0)),
         iaa.AverageBlur(k=(2, 7))])

    subsubseq_5 = iaa.AdditiveGaussianNoise(loc=(0, 0.5), scale=(0, 0.2))

    subsubseq_6 = iaa.Add((-0.3, 0.3))

    subsubseq_7 = iaa.Invert(p=1)

    subsubseq_8 = iaa.CoarseDropout(p=0.25, size_percent=(0.005, 0.06))

    subsubseq_9 = iaa.SigmoidContrast(gain=(0.8, 1.2))

    subsubseq_10 = iaa.LinearContrast(alpha=(0.8, 1.2))

    subsubseq_11 = iaa.Sequential([iaa.Emboss(alpha=(0, 1))])

    seq = iaa.Sometimes(
        p,
        iaa.OneOf([
            subsubseq_1,
            subsubseq_2,
            subsubseq_3,
            subsubseq_4,
            subsubseq_5,
            subsubseq_6,
            subsubseq_7,
            subsubseq_8,
            subsubseq_9,
            subsubseq_10,
            subsubseq_11,
        ]),
    )

    return seq