def test_from_float_serialization(float_image):
    aug = A.FromFloat(p=1, dtype="uint8")
    serialized_aug = A.to_dict(aug)
    deserialized_aug = A.from_dict(serialized_aug)
    aug_data = aug(image=float_image)
    deserialized_aug_data = deserialized_aug(image=float_image)
    assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
예제 #2
0
def sample_custom_augmentations_constructor(
        num_features: int, window_radius: int) -> albumentations.Compose:
    """
    This function returns a custom augmentations object for use with sequences via the load_sequences function in
    data_core.py. Please note that these augmentations have only been tested with RGB data between 0 and 1 and that
    order of operations is critical. e.g., blurs don't like missing data so shouldn't be applied before dropout, noise
    probably shouldn't be applied before color changes or blurs... of course, this is all dependent on your specific
    problem.

    Args:
        num_features:  number of features used in the model
        window_size:  window_size from the data configs

    Returns:
        custom augmentations function for use with sequences
    """
    max_kernel = int(round(0.1 * window_radius))
    max_hole_size = int(round(0.1 * window_radius))
    additional_targets = [
        ADDITIONAL_TARGETS_KEY.format(idx) for idx in range(1, num_features)
    ]

    return albumentations.Compose(
        [
            # The augmentations assume an image is RGB between 0 and 1
            albumentations.ToFloat(max_value=255, always_apply=True, p=1.0),
            # These augmentations should be order independent, toss 'em up front
            albumentations.Flip(p=0.5),
            albumentations.Transpose(p=0.5),
            albumentations.Rotate(limit=90, p=0.5),
            # Fogging as it's quite similar to top-down cloud effects, seems reasonable to apply up front
            albumentations.RandomFog(
                fog_coef_lower=0.2, fog_coef_upper=0.8, alpha_coef=0.08,
                p=0.5),
            # Color modifications
            albumentations.OneOf(
                [
                    albumentations.RandomBrightnessContrast(
                        brightness_limit=0.2,
                        contrast_limit=0.6,
                        brightness_by_max=True,
                        p=1.0),
                    albumentations.RGBShift(r_shift_limit=0.2,
                                            g_shift_limit=0.2,
                                            b_shift_limit=0.2,
                                            p=1.0),
                ],
                p=0.25,
            ),
            # Distortions
            albumentations.OneOf(
                [
                    albumentations.ElasticTransform(
                        alpha=1, sigma=50, alpha_affine=50, p=1.0),
                    albumentations.GridDistortion(
                        num_steps=5, distort_limit=0.4, p=1.0),
                    albumentations.OpticalDistortion(
                        distort_limit=0.1, shift_limit=0.1, p=1.0),
                ],
                p=0.25,
            ),
            albumentations.GaussianBlur(blur_limit=max_kernel, p=0.25),
            # Noise
            albumentations.OneOf(
                [
                    albumentations.CoarseDropout(max_holes=8,
                                                 max_height=max_hole_size,
                                                 max_width=max_hole_size,
                                                 fill_value=np.nan,
                                                 p=1.0),
                    albumentations.GaussNoise(var_limit=0.05, mean=0, p=1.0),
                ],
                p=0.25,
            ),
            # Scaling, adding last so that other augmentations are applied at a consistent resolution
            albumentations.RandomScale(scale_limit=0.05, p=0.25),
            # Augmentations may not return images of the same size, images can be both smaller and larger than expected, so
            # these two augmentations are added to keep things consistent
            albumentations.PadIfNeeded(
                2 * window_radius, 2 * window_radius, always_apply=True,
                p=1.0),
            albumentations.CenterCrop(
                2 * window_radius, 2 * window_radius, always_apply=True,
                p=1.0),
            # Return the data to its original scale
            albumentations.FromFloat(max_value=255, always_apply=True, p=1.0),
        ],
        p=1.0,
        additional_targets={target: "image"
                            for target in additional_targets},
    )