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

Added MixUp augmentation #1409

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
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
53 changes: 53 additions & 0 deletions albumentations/augmentations/geometric/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"OpticalDistortion",
"GridDistortion",
"PadIfNeeded",
"MixUp"
]


Expand Down Expand Up @@ -1491,3 +1492,55 @@ def get_params_dependent_on_targets(self, params):

def get_transform_init_args_names(self):
return "num_steps", "distort_limit", "interpolation", "border_mode", "value", "mask_value", "normalized"


class MixUp(DualTransform):
"""Generates a weighted combination of the pair of input images.
The bboxes are kept as in the input images

Args:
alpha (float): Alpha for distribution
beta (float): Beta for distribution
Targets:
image, image1, bboxes, bboxes1
Image types:
uint8, float32
Reference:
Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz:
"mixup: Beyond Empirical Risk Minimization"
"""

def __init__(
self,
alpha = 32.,
beta = 32.,
always_apply: bool = False,
p: float = 1,
):
super(MixUp, self).__init__(always_apply=always_apply, p=p)
self.alpha = alpha
self.beta = beta

def apply(self, image: np.ndarray, image1: np.ndarray, r, **params) -> np.ndarray:
h1, w1, _ = image.shape
h2, w2, _ = image1.shape
if h1 != h2 or w1 != w2:
raise TypeError("MixUp transformation expects both images to have identical shape.")
im = (image * r + image1 * (1 - r))
return im

def get_params(self):
return {"r": np.random.beta(self.alpha, self.beta)} # draw samples from a Beta distribution.

def apply_to_bboxes(self, bboxes: Sequence[BoxType], bboxes1: Sequence[BoxType], **params) -> List[BoxType]:
return bboxes + bboxes1

def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, Any]:
return {
"image1": params['image1'],
"bboxes1": params['bboxes1']
}

@property
def targets_as_params(self) -> List[str]:
return ["image", "image1", "bboxes", "bboxes1"]
Loading