Common Transforms¶
sign_language_tools.common.transforms provides small, data-agnostic transform building blocks used
to combine and glue together the domain-specific transforms from pose, video
and annotations.
Example¶
from sign_language_tools.common.transforms import Compose, Randomize, Identity
from sign_language_tools.pose.transforms import HorizontalFlip, GaussianNoise
augment = Compose([
Randomize(HorizontalFlip(), probability=0.5),
Randomize(GaussianNoise(), probability=0.3),
])
pose_sequence = augment(pose_sequence)
Compose also supports chaining transforms that take/return multiple positional arguments (e.g. a
pose sequence and its segments together) via multi_input=True.
Reference¶
sign_language_tools.common.transforms.compose.Compose ¶
Compose(
transforms: list[Callable], multi_input: bool = False
)
Bases: Transform
Chains a sequence of transforms into a single callable transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list[Callable]
|
List of transforms to apply in order. |
required |
multi_input
|
bool
|
If |
False
|
Example
from sign_language_tools.common.transforms import Compose transform = Compose([lambda x: x + 1, lambda x: x * 2]) transform(3) 8
__call__ ¶
__call__(*args)
Applies the chained transforms in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Input(s) to transform. If |
()
|
Returns:
| Type | Description |
|---|---|
|
The output of the last transform in the chain. |
sign_language_tools.common.transforms.concatenate.Concatenate ¶
Concatenate(dim: int = 0)
Bases: Transform
Concatenates a sequence of arrays along a given axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Axis along which the arrays are concatenated. |
0
|
Example
import numpy as np from sign_language_tools.common.transforms import Concatenate transform = Concatenate(dim=0) transform((np.zeros((2, 3)), np.zeros((4, 3)))).shape (6, 3)
__call__ ¶
__call__(x: tuple[ndarray, ...]) -> np.ndarray
Concatenates the given arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
tuple[ndarray, ...]
|
Sequence of arrays to concatenate. All arrays must have the
same shape, except along |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The arrays concatenated along |
sign_language_tools.common.transforms.identity.Identity ¶
Identity()
Bases: Transform
Returns its input(s) unchanged.
Useful as a no-op placeholder wherever a transform is expected, e.g. as
the "do nothing" branch of
Randomize.
Example
from sign_language_tools.common.transforms import Identity transform = Identity() transform(42) 42
__call__ ¶
__call__(*args)
Returns the input(s) unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any number of inputs. |
()
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
|
sign_language_tools.common.transforms.map.MapTransform ¶
MapTransform(
transforms: Union[list[Callable], dict[Any, Callable]],
)
Bases: Transform
Applies a different transform to each element of a tuple, list, or dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
Union[list[Callable], dict[Any, Callable]]
|
If |
required |
Warning
For list/tuple input, transforms and x are paired positionally
with zip, so if transforms is shorter than x, the extra
trailing elements of x are silently dropped from the output
rather than passed through unchanged.
Example
from sign_language_tools.common.transforms import MapTransform transform = MapTransform([lambda x: x + 1, None]) transform((1, 2)) (2, 2)
__call__ ¶
__call__(*args)
Applies the mapped transforms to the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
A single tuple, list, or dict, or several positional arguments that are treated as a tuple. |
()
|
Returns:
| Type | Description |
|---|---|
|
A tuple with each element mapped through the corresponding |
|
|
transform (if |
|
|
mapped through the corresponding transform (if |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is not a tuple, list, or dict. |
sign_language_tools.common.transforms.map.ApplyToAll ¶
ApplyToAll(transform: Callable)
Bases: Transform
Applies the same transform to every element of a list or dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable
|
The transform applied to each element of |
required |
Example
from sign_language_tools.common.transforms import ApplyToAll transform = ApplyToAll(lambda x: x + 1) transform([1, 2, 3]) [2, 3, 4]
__call__ ¶
__call__(x: Union[list, dict]) -> Union[list, dict]
Applies the transform to every element of x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[list, dict]
|
A list or dict whose elements (or values) are transformed. |
required |
Returns:
| Type | Description |
|---|---|
Union[list, dict]
|
A list, or dict, with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
sign_language_tools.common.transforms.randomize.Randomize ¶
Randomize(transform: Callable, probability: float = 0.5)
Bases: Transform
Applies the given transform with a given probability.
On each call, transform is applied with probability probability;
otherwise the input(s) are passed through unchanged (see
Identity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable
|
The transform to apply. |
required |
probability
|
float
|
Probability of applying |
0.5
|
Example
from sign_language_tools.common.transforms import Randomize transform = Randomize(lambda x: x * 2, probability=1.0) transform(3) 6
__call__ ¶
__call__(*args)
Randomly applies the transform or the identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Input(s) to pass to |
()
|
Returns:
| Type | Description |
|---|---|
|
The output of |
|
|
otherwise the input(s) unchanged. |
sign_language_tools.common.transforms.tuple.TransformTuple ¶
TransformTuple(transform: Callable, n: int = 2)
Bases: Transform
Applies a transform independently n times and returns the results as a tuple.
Note
transform is called n times with the exact same input(s), so it
only makes sense to use a stochastic transform (e.g.
Randomize
or a transform involving random noise) here. With a deterministic
transform, all n outputs would be identical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable
|
The transform to apply. |
required |
n
|
int
|
Number of times to apply |
2
|
Example
import numpy as np from sign_language_tools.common.transforms import TransformTuple add_noise = lambda x: x + np.random.normal(size=x.shape) transform = TransformTuple(add_noise, n=2) views = transform(np.zeros(3)) len(views) 2
__call__ ¶
__call__(*args, **kwargs)
Applies the transform n times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments forwarded to |
()
|
|
**kwargs
|
Keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
|
A tuple of |
sign_language_tools.common.transforms.replace_nan.ReplaceNaN ¶
ReplaceNaN(fill_value: float = 0.0)
Bases: Transform
Replaces NaN values in an array with a fixed value.
Warning
This transform mutates x in place (in addition to returning it),
since it assigns directly into the input array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_value
|
float
|
Value used to replace |
0.0
|
Example
import numpy as np from sign_language_tools.common.transforms import ReplaceNaN transform = ReplaceNaN(fill_value=0.0) transform(np.array([1.0, np.nan, 3.0])) array([1., 0., 3.])
__call__ ¶
__call__(x: ndarray) -> np.ndarray
Replaces the NaN values of x with fill_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Array of floats, potentially containing |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|