-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransforms.py
44 lines (35 loc) · 1.29 KB
/
transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from typing import Any
import albumentations
import hydra
import numpy as np
from omegaconf import DictConfig
from PIL import Image
class TransformsWrapper:
def __init__(self, transforms_cfg: DictConfig) -> None:
"""TransformsWrapper module.
Args:
transforms_cfg (DictConfig): Transforms config.
"""
augmentations = []
if not transforms_cfg.get("order"):
raise RuntimeError(
"TransformsWrapper requires param <order>, i.e."
"order of augmentations as List[augmentation name]"
)
for augmentation_name in transforms_cfg.get("order"):
augmentation = hydra.utils.instantiate(
transforms_cfg.get(augmentation_name), _convert_="object"
)
augmentations.append(augmentation)
self.augmentations = albumentations.Compose(augmentations)
def __call__(self, image: Any, **kwargs: Any) -> Any:
"""Apply TransformsWrapper module.
Args:
image (Any): Input image.
kwargs (Any): Additional arguments.
Returns:
Any: Transformation results.
"""
if isinstance(image, Image.Image):
image = np.asarray(image)
return self.augmentations(image=image, **kwargs)