FrequencyMixStyle2D(probability: float = 0.5, alpha: float = 1.0, epsilon: float = 1e-06, **kwargs)
Apply frequency mix style augmentation to the 2D input.
Parameters:
-
probability
(float, default:
0.5
)
–
Probability of applying the augmentation.
-
alpha
(float, default:
1.0
)
–
-
epsilon
(float, default:
1e-06
)
–
Epsilon value for numerical stability.
Example:
x = np.random.rand(4, 4, 3)
lyr = FrequencyMixStyle2D(probability=1.0, alpha=1.0)
y = lyr(x, training=True)
Source code in helia_edge/layers/preprocessing/frequency_mix_style.py
| def __init__(
self,
probability: float = 0.5,
alpha: float = 1.0,
epsilon: float = 1e-6,
**kwargs,
):
"""Apply frequency mix style augmentation to the 2D input.
Args:
probability (float): Probability of applying the augmentation.
alpha (float): Mixup alpha value.
epsilon (float): Epsilon value for numerical stability.
Example:
```python
x = np.random.rand(4, 4, 3)
lyr = FrequencyMixStyle2D(probability=1.0, alpha=1.0)
y = lyr(x, training=True)
```
"""
super().__init__(**kwargs)
self.probability, _ = parse_factor([None, probability], min_value=0.0, max_value=1.0, param_name="probability")
self.alpha, _ = parse_factor([None, alpha], min_value=0.0, max_value=None, param_name="alpha")
self.epsilon, _ = parse_factor([None, epsilon], min_value=0.0, max_value=None, param_name="epsilon")
|
Functions
get_random_transformations(input_shape: tuple[int, int, int]) -> dict
Generate noise distortion tensor
Parameters:
Returns:
-
dict ( dict
) –
Dictionary containing the noise tensor.
Source code in helia_edge/layers/preprocessing/frequency_mix_style.py
| def get_random_transformations(self, input_shape: tuple[int, int, int]) -> dict:
"""Generate noise distortion tensor
Args:
input_shape (tuple[int, ...]): Input shape.
Returns:
dict: Dictionary containing the noise tensor.
"""
batch_size = input_shape[0]
skip_augment = keras.random.uniform(
shape=(), minval=0.0, maxval=1.0, dtype="float32", seed=self.random_generator
)
lmda = keras.random.beta(
shape=(batch_size, 1, 1, 1), alpha=self.alpha, beta=self.alpha, seed=self.random_generator
)
perm = keras.random.shuffle(keras.ops.arange(batch_size), seed=self.random_generator)
return {"lmda": lmda, "perm": perm, "skip_augment": skip_augment}
|
apply_mixstyle
apply_mixstyle(x, lmda, perm)
Apply mixstyle augmentation
Parameters:
-
x
(Tensor)
–
-
lmda
(Tensor)
–
-
perm
(Tensor)
–
Returns:
-
–
tf.Tensor: Augmented tensor
Source code in helia_edge/layers/preprocessing/frequency_mix_style.py
| def apply_mixstyle(self, x, lmda, perm):
"""Apply mixstyle augmentation
Args:
x (tf.Tensor): Input tensor
lmda (tf.Tensor): Lambda tensor
perm (tf.Tensor): Permutation tensor
Returns:
tf.Tensor: Augmented tensor
"""
f_mu = keras.ops.mean(x, axis=[2, 3], keepdims=True)
f_var = keras.ops.var(x, axis=[2, 3], keepdims=True)
f_sig = keras.ops.sqrt(f_var + self.epsilon)
x_normed = (x - f_mu) / f_sig
f_mu_perm = keras.ops.take(f_mu, perm, axis=0)
f_sig_perm = keras.ops.take(f_sig, perm, axis=0)
x_perm = keras.ops.take(x_normed, perm, axis=0)
x_mix = lmda * x_normed + (1 - lmda) * x_perm
x_mix = x_mix * f_sig_perm + f_mu_perm
return x_mix
|
augment_samples
augment_samples(inputs) -> keras.KerasTensor
Augment samples
Parameters:
Returns:
-
KerasTensor
–
tf.Tensor: Augmented tensor
Source code in helia_edge/layers/preprocessing/frequency_mix_style.py
| def augment_samples(self, inputs) -> keras.KerasTensor:
"""Augment samples
Args:
inputs (tf.Tensor): Input tensor
Returns:
tf.Tensor: Augmented tensor
"""
samples = inputs[self.SAMPLES]
transforms = inputs[self.TRANSFORMS]
skip_augment = transforms["skip_augment"]
if self.training:
lmda = transforms["lmda"]
perm = transforms["perm"]
return keras.ops.cond(
skip_augment > self.probability, lambda: samples, lambda: self.apply_mixstyle(samples, lmda, perm)
)
return samples
|