예제 #1
0
def test_transform_pipeline_serialization_with_keypoints(seed, image, keypoints, keypoint_format, labels):
    aug = A.Compose(
        [
            A.OneOrOther(
                A.Compose([A.RandomRotate90(), A.OneOf([A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5)])]),
                A.Compose([A.Rotate(p=0.5), A.OneOf([A.HueSaturationValue(p=0.5), A.RGBShift(p=0.7)], p=1)]),
            ),
            A.SomeOf(
                n=2,
                transforms=[
                    A.HorizontalFlip(p=1),
                    A.Transpose(p=1),
                    A.HueSaturationValue(p=0.5),
                    A.RandomBrightnessContrast(p=0.5),
                ],
                replace=False,
            ),
        ],
        keypoint_params={"format": keypoint_format, "label_fields": ["labels"]},
    )
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, keypoints=keypoints, labels=labels)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, keypoints=keypoints, labels=labels)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["keypoints"], deserialized_aug_data["keypoints"])
예제 #2
0
def test_transform_pipeline_serialization(seed, image, mask):
    aug = A.Compose([
        A.OneOrOther(
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1024),
                                  height=512,
                                  width=512,
                                  p=1),
                A.OneOf([
                    A.RandomSizedCrop(min_max_height=(256, 512),
                                      height=384,
                                      width=384,
                                      p=0.5),
                    A.RandomSizedCrop(min_max_height=(256, 512),
                                      height=512,
                                      width=512,
                                      p=0.5),
                ]),
            ]),
            A.Compose([
                A.Resize(1024, 1024),
                A.RandomSizedCrop(min_max_height=(256, 1025),
                                  height=256,
                                  width=256,
                                  p=1),
                A.OneOf([A.HueSaturationValue(p=0.5),
                         A.RGBShift(p=0.7)], p=1),
            ]),
        ),
        A.SomeOf(
            [
                A.HorizontalFlip(p=1),
                A.Transpose(p=1),
                A.HueSaturationValue(p=0.5),
                A.RandomBrightnessContrast(p=0.5),
            ],
            2,
            replace=False,
        ),
    ])
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    set_seed(seed)
    aug_data = aug(image=image, mask=mask)
    set_seed(seed)
    deserialized_aug_data = deserialized_aug(image=image, mask=mask)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
    assert np.array_equal(aug_data["mask"], deserialized_aug_data["mask"])
예제 #3
0
def randAugment(N=2, M=4, p=1.0, mode="all", cut_out=False):
    """
    Examples:
        >>> # M from 0 to 20
        >>> transforms = randAugment(N=3, M=8, p=0.8, mode='all', cut_out=False)
    """
    # Magnitude(M) search space
    scale = np.linspace(0, 0.4, 20)
    translate = np.linspace(0, 0.4, 20)
    rot = np.linspace(0, 30, 20)
    shear_x = np.linspace(0, 20, 20)
    shear_y = np.linspace(0, 20, 20)
    contrast = np.linspace(0.0, 0.4, 20)
    bright = np.linspace(0.0, 0.4, 20)
    sat = np.linspace(0.0, 0.2, 20)
    hue = np.linspace(0.0, 0.2, 20)
    shar = np.linspace(0.0, 0.9, 20)
    blur = np.linspace(0, 0.2, 20)
    noise = np.linspace(0, 1, 20)
    cut = np.linspace(0, 0.6, 20)
    # Transformation search space
    Aug = [  # geometrical
        albumentations.Affine(scale=(1.0 - scale[M], 1.0 + scale[M]), p=p),
        albumentations.Affine(translate_percent=(-translate[M], translate[M]),
                              p=p),
        albumentations.Affine(rotate=(-rot[M], rot[M]), p=p),
        albumentations.Affine(shear={'x': (-shear_x[M], shear_x[M])}, p=p),
        albumentations.Affine(shear={'y': (-shear_y[M], shear_y[M])}, p=p),
        # Color Based
        albumentations.RandomContrast(limit=contrast[M], p=p),
        albumentations.RandomBrightness(limit=bright[M], p=p),
        albumentations.ColorJitter(brightness=0.0,
                                   contrast=0.0,
                                   saturation=sat[M],
                                   hue=0.0,
                                   p=p),
        albumentations.ColorJitter(brightness=0.0,
                                   contrast=0.0,
                                   saturation=0.0,
                                   hue=hue[M],
                                   p=p),
        albumentations.Sharpen(alpha=(0.1, shar[M]), lightness=(0.5, 1.0),
                               p=p),
        albumentations.core.composition.PerChannel(albumentations.OneOf([
            albumentations.MotionBlur(p=0.5),
            albumentations.MedianBlur(blur_limit=3, p=1),
            albumentations.Blur(blur_limit=3, p=1),
        ]),
                                                   p=blur[M] * p),
        albumentations.GaussNoise(var_limit=(8.0 * noise[M], 64.0 * noise[M]),
                                  per_channel=True,
                                  p=p)
    ]
    # Sampling from the Transformation search space
    if mode == "geo":
        transforms = albumentations.SomeOf(Aug[0:5], N)
    elif mode == "color":
        transforms = albumentations.SomeOf(Aug[5:], N)
    else:
        transforms = albumentations.SomeOf(Aug, N)

    if cut_out:
        cut_trans = albumentations.OneOf([
            albumentations.CoarseDropout(
                max_holes=8, max_height=16, max_width=16, fill_value=0, p=1),
            albumentations.GridDropout(ratio=cut[M], p=1),
            albumentations.Cutout(
                num_holes=8, max_h_size=16, max_w_size=16, p=1),
        ],
                                         p=cut[M])
        transforms = albumentations.Compose([transforms, cut_trans])

    return transforms