-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
AugMix #607
Open
akarsakov
wants to merge
8
commits into
albumentations-team:main
Choose a base branch
from
akarsakov:aug_mix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−6
Open
AugMix #607
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cb58c7
Add AugMix transform
akarsakov 778d8c1
Refactoring: move aug_mix code to functional, add random state
akarsakov b549361
Rename scale->scale_coef
akarsakov 6676222
Add RandomShear and Autocontrast transforms, refactored Augmix
akarsakov 0775282
Add tests for Autocontrast and RandomShear
akarsakov 3301381
Remove unnecessary call
akarsakov d4ba844
Merge branch 'master' into aug_mix
Dipet 5e1635f
Fix icorrect width, height as dsize parameter
akarsakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,9 @@ | |
"FancyPCA", | ||
"MaskDropout", | ||
"GridDropout", | ||
"AugMix", | ||
"RandomShear", | ||
"Autocontrast", | ||
] | ||
|
||
|
||
|
@@ -581,6 +584,68 @@ def get_transform_init_args(self): | |
return {"interpolation": self.interpolation, "scale_limit": to_tuple(self.scale_limit, bias=-1.0)} | ||
|
||
|
||
class RandomShear(DualTransform): | ||
"""Randomly resize the input. Output image size is different from the input image size. | ||
Args: | ||
shear_x (float, tuple of floats): Shear along x axis. If single float shear_x is picked | ||
from (-shear_x, shear_x) interval. Default: 0.1. | ||
shear_y (float, tuple of floats): Shear along y axis. If single float shear_y is picked | ||
from (-shear_y, shear_y) interval. Default: 0.1. | ||
interpolation (OpenCV flag): flag that is used to specify the interpolation algorithm. Should be one of: | ||
cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. | ||
Default: cv2.INTER_LINEAR. | ||
border_mode (OpenCV flag): flag that is used to specify the pixel extrapolation method. Should be one of: | ||
cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_REFLECT_101. | ||
Default: cv2.BORDER_REFLECT_101 | ||
value (int, float, list of int, list of float): padding value if border_mode is cv2.BORDER_CONSTANT. | ||
p (float): probability of applying the transform. Default: 0.5. | ||
Targets: | ||
image, mask, bboxes, keypoints | ||
Image types: | ||
uint8, float32 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
shear_x=0.1, | ||
shear_y=0.1, | ||
interpolation=cv2.INTER_LINEAR, | ||
border_mode=cv2.cv2.BORDER_REFLECT_101, | ||
value=None, | ||
always_apply=False, | ||
p=0.5, | ||
): | ||
super().__init__(always_apply, p) | ||
self.shear_x = to_tuple(shear_x) | ||
self.shear_y = to_tuple(shear_y) | ||
self.interpolation = interpolation | ||
self.border_mode = border_mode | ||
self.value = value | ||
|
||
def get_params(self): | ||
return { | ||
"shear_x": random.uniform(self.shear_x[0], self.shear_x[1]), | ||
"shear_y": random.uniform(self.shear_y[0], self.shear_y[1]), | ||
} | ||
|
||
def apply(self, img, shear_x=0, shear_y=0, **params): | ||
return F.shear( | ||
img, shear_x, shear_y, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_REFLECT_101, value=self.value | ||
) | ||
|
||
def apply_to_bbox(self, bbox, shear_x=0, shear_y=0, **params): | ||
return F.bbox_shear(bbox, shear_x, shear_y, **params) | ||
|
||
def apply_to_keypoint(self, keypoint, shear_x=0, shear_y=0, **params): | ||
return F.keypoint_shear(keypoint, shear_x, shear_y) | ||
|
||
def get_transform_init_args_names(self): | ||
return ("shear_x", "shear_y", "interpolation", "border_mode", "value") | ||
|
||
|
||
class ShiftScaleRotate(DualTransform): | ||
"""Randomly apply affine transforms: translate, scale and rotate the input. | ||
|
@@ -2287,6 +2352,30 @@ def get_transform_init_args_names(self): | |
return ("mode", "by_channels") | ||
|
||
|
||
class Autocontrast(ImageOnlyTransform): | ||
"""Perform automatic contrast enhancement. | ||
Args: | ||
p (float): probability of applying the transform. Default: 0.5. | ||
Targets: | ||
image | ||
Image types: | ||
uint8 | ||
""" | ||
|
||
def __init__(self, always_apply=False, p=0.5): | ||
super().__init__(always_apply, p) | ||
|
||
def apply(self, image, **params): | ||
return F.autocontrast(image) | ||
|
||
def get_transform_init_args_names(self): | ||
return () | ||
|
||
|
||
class RGBShift(ImageOnlyTransform): | ||
"""Randomly shift values for each channel of the input RGB image. | ||
|
@@ -3378,3 +3467,87 @@ def get_transform_init_args_names(self): | |
"mask_fill_value", | ||
"random_offset", | ||
) | ||
|
||
|
||
class AugMix(ImageOnlyTransform): | ||
"""Augmentation from "AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty" | ||
Please note that this augmentation performs normalization internally, so resulting image will be float type. | ||
Args: | ||
alpha (float): Probability coefficient for Beta and Dirichlet distributions. Default: (1.0). | ||
width (int): Width of augmentation chain. Default: 3. | ||
depth (int, tuple of ints): Depth of augmentation chain. If single int will be used provided number. | ||
If tuple of depth will be generated in range `[depth[0], depth[1])`. Default: (3). | ||
transforms (list of albumentation transforms): List of transforms from which augmentation will be sampled | ||
on each step of AugMix procedure. | ||
mean (float, list of floats, tuple of float): mean values for normalization. Default: (0.485, 0.456, 0.406). | ||
std (float, list of floats, tuple of floats): std values for normalization. Default: (0.229, 0.224, 0.225). | ||
Targets: | ||
image | ||
Image types: | ||
uint8, float32 3-channel images only | ||
Credit: | ||
https://arxiv.org/pdf/1912.02781.pdf | ||
https://github.com/google-research/augmix | ||
""" | ||
|
||
def __init__( | ||
self, | ||
alpha=1.0, | ||
width=3, | ||
depth=3, | ||
transforms=None, | ||
mean=(0.485, 0.456, 0.406), | ||
std=(0.229, 0.224, 0.225), | ||
always_apply=False, | ||
p=0.5, | ||
): | ||
super().__init__(always_apply=always_apply, p=p) | ||
self.alpha = alpha | ||
self.width = width | ||
self.depth = to_tuple(depth, low=1) | ||
self.transforms = transforms | ||
|
||
if self.transforms is None: | ||
self.transforms = [ | ||
Autocontrast(), | ||
Posterize(num_bits=(3, 4)), | ||
ShiftScaleRotate(shift_limit=0, scale_limit=0, rotate_limit=5, border_mode=cv2.BORDER_CONSTANT), | ||
Solarize(threshold=77), | ||
RandomShear(shear_x=0.09, shear_y=0, border_mode=cv2.BORDER_CONSTANT), | ||
RandomShear(shear_x=0, shear_y=0.09, border_mode=cv2.BORDER_CONSTANT), | ||
ShiftScaleRotate(shift_limit=0.09, scale_limit=0, rotate_limit=0, border_mode=cv2.BORDER_CONSTANT), | ||
] | ||
|
||
self.mean = mean | ||
self.std = std | ||
|
||
def apply(self, img, depth=3, random_state=None, **params): | ||
return F.aug_mix( | ||
img, | ||
self.alpha, | ||
self.width, | ||
depth, | ||
self.transforms, | ||
self.mean, | ||
self.std, | ||
random_state=np.random.RandomState(random_state), | ||
) | ||
|
||
def get_params(self): | ||
return {"depth": random.randint(self.depth[0], self.depth[1]), "random_state": random.randint(0, 10000)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assigning a single |
||
|
||
def _to_dict(self): | ||
state = { | ||
"__class_fullname__": self.get_class_fullname(), | ||
"alpha": self.alpha, | ||
"width": self.width, | ||
"depth": self.depth, | ||
"mean": self.mean, | ||
"std": self.std, | ||
"transforms": [t._to_dict() for t in self.transforms], # skipcq: PYL-W0212 | ||
} | ||
state.update(self.get_base_init_args()) | ||
return state |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All transforms need to have
p=1
, such that they are always executed when selected byAugmix
.Maybe add a comment and change default values ?