-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: partial imgaug library support
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import tensor | ||
from .common import prob_aug | ||
from .imgaug import imgaug |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from __future__ import annotations | ||
|
||
import einops | ||
from imgaug import augmenters as iaa | ||
|
||
from zetta_utils import builder | ||
from zetta_utils.tensor_ops import convert | ||
|
||
|
||
@builder.register("imgaug") | ||
def imgaug(augmentables, augmenters): | ||
for k, v in augmentables.items(): | ||
if k in ("image", "images", "heatmaps", "segmentation_maps"): | ||
augmentables[k] = convert.to_np(einops.rearrange(v, "C X Y Z -> Z X Y C")) | ||
elif k in ("keypoints", "bounding_boxes", "polygons", "line_strings"): | ||
pass | ||
else: | ||
raise ValueError(f"Unsupported augmentable {k}") | ||
|
||
for augmenter in augmenters: | ||
augmentables = augmenter.augment(**augmentables, return_batch=True) | ||
augmentables = { | ||
k: getattr(augmentables, f"{k}_aug") for k in augmentables.get_column_names() | ||
} | ||
|
||
for k, v in augmentables.items(): | ||
if k in ("image", "images", "heatmaps", "segmentation_maps"): | ||
augmentables[k] = convert.to_torch(einops.rearrange(v, "Z X Y C -> C X Y Z")) | ||
|
||
return augmentables | ||
|
||
|
||
def unpack_kwargs(cls): | ||
class wrapper(cls): | ||
def __call__(kwargs_tuple): | ||
super.__call__(**kwargs_tuple) | ||
|
||
return wrapper | ||
|
||
|
||
for k in dir(iaa): | ||
if k[0].isupper() and hasattr(getattr(iaa, k), "augment"): | ||
builder.register(f"imgaug.augmenters.{k}")(unpack_kwargs(getattr(iaa, k))) | ||
|
||
for k in dir(iaa.imgcorruptlike): | ||
if k[0].isupper() and hasattr(getattr(iaa.imgcorruptlike, k), "augment"): | ||
builder.register(f"imgaug.augmenters.imgcorruptlike.{k}")( | ||
unpack_kwargs(getattr(iaa.imgcorruptlike, k)) | ||
) |