How to achieve a single call to apply
when having multiple image targets
#1410
-
I recentrly created a PR with MixUp augmentation that look lite this: class MixUp(DualTransform):
"""Generates a weighted combination of the pair of input images.
Targets:
image, image1, bboxes, bboxes1
Image types:
uint8, float32
"""
def __init__(
self,
always_apply: bool = False,
p: float = 1,
):
super(MixUp, self).__init__(always_apply, p)
def apply(self, image, image1, **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.")
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
im = (image * r + image1 * (1 - r)).astype(np.uint8)
return im
def apply_to_bboxes(self, bboxes: Sequence[BoxType], bboxes1, **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"] However, This would minimize the processing time and would be useful for implementing CutMix and Mosaic as well where the same is needed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I got an answer here: #1409 (comment). Just copy pasting @i-aki-y's answer:
|
Beta Was this translation helpful? Give feedback.
I got an answer here: #1409 (comment).
Just copy pasting @i-aki-y's answer: