Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Cutout augmentation #264

Merged
merged 6 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Preserve old API
As mentioned by @albu, it would be better to preserve API by deprecating
current `Cutout` class and introducing another version of it.
  • Loading branch information
kirillbobyrev committed May 25, 2019
commit fd3fffb3eb4c797c04f565e3b65d87920fa3e51d
74 changes: 65 additions & 9 deletions albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
from .bbox_utils import union_of_bboxes, denormalize_bbox, normalize_bbox
from ..core.transforms_interface import to_tuple, DualTransform, ImageOnlyTransform, NoOp

__all__ = ['Blur', 'VerticalFlip', 'HorizontalFlip', 'Flip', 'Normalize', 'Transpose', 'RandomCrop', 'RandomGamma',
'RandomRotate90', 'Rotate', 'ShiftScaleRotate', 'CenterCrop', 'OpticalDistortion', 'GridDistortion',
'ElasticTransform', 'HueSaturationValue', 'PadIfNeeded', 'RGBShift', 'RandomBrightness', 'RandomContrast',
'MotionBlur', 'MedianBlur', 'GaussianBlur', 'GaussNoise', 'CLAHE', 'ChannelShuffle', 'InvertImg', 'ToGray',
'JpegCompression', 'Cutout', 'ToFloat', 'FromFloat', 'Crop', 'RandomScale', 'LongestMaxSize',
'SmallestMaxSize', 'Resize', 'RandomSizedCrop', 'RandomBrightnessContrast', 'RandomCropNearBBox',
'RandomSizedBBoxSafeCrop', 'RandomSnow', 'RandomRain', 'RandomFog', 'RandomSunFlare',
'RandomShadow', 'Lambda']
__all__ = [
'Blur', 'VerticalFlip', 'HorizontalFlip', 'Flip', 'Normalize', 'Transpose',
'RandomCrop', 'RandomGamma', 'RandomRotate90', 'Rotate',
'ShiftScaleRotate', 'CenterCrop', 'OpticalDistortion', 'GridDistortion',
'ElasticTransform', 'HueSaturationValue', 'PadIfNeeded', 'RGBShift',
'RandomBrightness', 'RandomContrast', 'MotionBlur', 'MedianBlur',
'GaussianBlur', 'GaussNoise', 'CLAHE', 'ChannelShuffle', 'InvertImg',
'ToGray', 'JpegCompression', 'Cutout', 'CoarseDropout', 'ToFloat',
'FromFloat', 'Crop', 'RandomScale', 'LongestMaxSize', 'SmallestMaxSize',
'Resize', 'RandomSizedCrop', 'RandomBrightnessContrast',
'RandomCropNearBBox', 'RandomSizedBBoxSafeCrop', 'RandomSnow',
'RandomRain', 'RandomFog', 'RandomSunFlare', 'RandomShadow', 'Lambda',
]


class PadIfNeeded(DualTransform):
Expand Down Expand Up @@ -901,6 +906,57 @@ def get_transform_init_args_names(self):


class Cutout(ImageOnlyTransform):
"""CoarseDropout of the square regions in the image.
Args:
num_holes (int): number of regions to zero out
max_h_size (int): maximum height of the hole
max_w_size (int): maximum width of the hole
Targets:
image
Image types:
uint8, float32
Reference:
| https://arxiv.org/abs/1708.04552
| https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
| https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/arithmetic.py
"""

def __init__(self, num_holes=8, max_h_size=8, max_w_size=8, always_apply=False, p=0.5):
super(Cutout, self).__init__(always_apply, p)
self.num_holes = num_holes
self.max_h_size = max_h_size
self.max_w_size = max_w_size
warnings.warn("This class has been deprecated. Please use CoarseDropout", DeprecationWarning)

def apply(self, image, holes=[], **params):
return F.cutout(image, holes)

def get_params_dependent_on_targets(self, params):
img = params['image']
height, width = img.shape[:2]

holes = []
for n in range(self.num_holes):
y = random.randint(0, height)
x = random.randint(0, width)

y1 = np.clip(y - self.max_h_size // 2, 0, height)
y2 = np.clip(y + self.max_h_size // 2, 0, height)
x1 = np.clip(x - self.max_w_size // 2, 0, width)
x2 = np.clip(x + self.max_w_size // 2, 0, width)
holes.append((x1, y1, x2, y2))

return {'holes': holes}

@property
def targets_as_params(self):
return ['image']

def get_transform_init_args_names(self):
return ('num_holes', 'max_h_size', 'max_w_size')


class CoarseDropout(ImageOnlyTransform):
"""CoarseDropout of the rectangular regions in the image.

Args:
Expand Down Expand Up @@ -930,7 +986,7 @@ class Cutout(ImageOnlyTransform):
def __init__(self, max_holes=8, max_height=8, max_width=8,
min_holes=None, min_height=None, min_width=None,
always_apply=False, p=0.5):
super(Cutout, self).__init__(always_apply, p)
super().__init__(always_apply, p)
self.max_holes = max_holes
self.max_height = max_height
self.max_width = max_width
Expand Down
12 changes: 7 additions & 5 deletions tests/test_augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Rotate, ShiftScaleRotate, CenterCrop, OpticalDistortion, GridDistortion, ElasticTransform, ToGray, RandomGamma, \
JpegCompression, HueSaturationValue, RGBShift, Blur, MotionBlur, MedianBlur, \
GaussianBlur, GaussNoise, CLAHE, ChannelShuffle, InvertImg, IAAEmboss, IAASuperpixels, IAASharpen, \
IAAAdditiveGaussianNoise, IAAPiecewiseAffine, IAAPerspective, Cutout, Normalize, ToFloat, FromFloat, \
IAAAdditiveGaussianNoise, IAAPiecewiseAffine, IAAPerspective, Cutout, CoarseDropout, Normalize, ToFloat, FromFloat, \
RandomBrightnessContrast, RandomSnow, RandomRain, RandomFog, RandomSunFlare, RandomCropNearBBox, RandomShadow, \
RandomSizedCrop

Expand All @@ -26,6 +26,7 @@
[RandomGamma, {}],
[ToGray, {}],
[Cutout, {}],
[CoarseDropout, {}],
[GaussNoise, {}],
[RandomSnow, {}],
[RandomRain, {}],
Expand Down Expand Up @@ -56,6 +57,7 @@ def test_image_only_augmentations(augmentation_cls, params, image, mask):
[JpegCompression, {}],
[ToGray, {}],
[Cutout, {}],
[CoarseDropout, {}],
[GaussNoise, {}],
[RandomSnow, {}],
[RandomRain, {}],
Expand Down Expand Up @@ -135,7 +137,6 @@ def test_imgaug_dual_augmentations(augmentation_cls, image, mask):


@pytest.mark.parametrize(['augmentation_cls', 'params'], [
[Cutout, {}],
[JpegCompression, {}],
[HueSaturationValue, {}],
[RGBShift, {}],
Expand All @@ -152,6 +153,7 @@ def test_imgaug_dual_augmentations(augmentation_cls, image, mask):
[RandomGamma, {}],
[ToGray, {}],
[Cutout, {}],
[CoarseDropout, {}],
[PadIfNeeded, {}],
[VerticalFlip, {}],
[HorizontalFlip, {}],
Expand Down Expand Up @@ -187,6 +189,7 @@ def test_augmentations_wont_change_input(augmentation_cls, params, image, mask):

@pytest.mark.parametrize(['augmentation_cls', 'params'], [
[Cutout, {}],
[CoarseDropout, {}],
[HueSaturationValue, {}],
[RGBShift, {}],
[RandomBrightnessContrast, {}],
Expand All @@ -200,7 +203,6 @@ def test_augmentations_wont_change_input(augmentation_cls, params, image, mask):
[InvertImg, {}],
[RandomGamma, {}],
[ToGray, {}],
[Cutout, {}],
[PadIfNeeded, {}],
[VerticalFlip, {}],
[HorizontalFlip, {}],
Expand Down Expand Up @@ -234,6 +236,7 @@ def test_augmentations_wont_change_float_input(augmentation_cls, params, float_i

@pytest.mark.parametrize(['augmentation_cls', 'params'], [
[Cutout, {}],
[CoarseDropout, {}],
[JpegCompression, {}],
[RandomBrightnessContrast, {}],
[Blur, {}],
Expand All @@ -243,7 +246,6 @@ def test_augmentations_wont_change_float_input(augmentation_cls, params, float_i
[GaussNoise, {}],
[InvertImg, {}],
[RandomGamma, {}],
[Cutout, {}],
[VerticalFlip, {}],
[HorizontalFlip, {}],
[Flip, {}],
Expand Down Expand Up @@ -286,6 +288,7 @@ def test_augmentations_wont_change_shape_grayscale(augmentation_cls, params, ima

@pytest.mark.parametrize(['augmentation_cls', 'params'], [
[Cutout, {}],
[CoarseDropout, {}],
[JpegCompression, {}],
[HueSaturationValue, {}],
[RGBShift, {}],
Expand All @@ -300,7 +303,6 @@ def test_augmentations_wont_change_shape_grayscale(augmentation_cls, params, ima
[InvertImg, {}],
[RandomGamma, {}],
[ToGray, {}],
[Cutout, {}],
[VerticalFlip, {}],
[HorizontalFlip, {}],
[Flip, {}],
Expand Down
4 changes: 3 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
[A.RandomGamma, {}],
[A.ToGray, {}],
[A.Cutout, {}],
[A.CoarseDropout, {}],
[A.RandomSnow, {}],
[A.RandomRain, {}],
[A.RandomFog, {}],
Expand Down Expand Up @@ -78,7 +79,8 @@ def test_augmentations_serialization(augmentation_cls, params, p, seed, image, m
[A.GaussNoise, {'var_limit': (20, 90)}],
[A.CLAHE, {'clip_limit': 2, 'tile_grid_size': (12, 12)}],
[A.RandomGamma, {'gamma_limit': (10, 90)}],
[A.Cutout, {'max_holes': 4, 'max_height': 4, 'max_width': 4}],
[A.Cutout, {'num_holes': 4, 'max_h_size': 4, 'max_w_size': 4}],
[A.CoarseDropout, {'max_holes': 4, 'max_height': 4, 'max_width': 4}],
[A.RandomSnow, {'snow_point_lower': 0.2, 'snow_point_upper': 0.4, 'brightness_coeff': 4}],
[A.RandomRain, {
'slant_lower': -5,
Expand Down
1 change: 1 addition & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def test_force_apply():
[A.ChannelShuffle, {}],
[A.GaussNoise, {}],
[A.Cutout, {}],
[A.CoarseDropout, {}],
[A.JpegCompression, {}],
[A.HueSaturationValue, {}],
[A.RGBShift, {}],
Expand Down