Skip to content

spec_augment

SpecAugment Layer API

This module provides classes to build SpecAugment layers.

Classes:

Classes

SpecAugment2D

SpecAugment2D(freq_mask_param: int, time_mask_param: int, n_freq_mask: int = 1, n_time_mask: int = 1, mask_value: float = 0.0, **kwargs)

SpecAugment layer w/o time warping

Parameters:

  • freq_mask_param (int) –

    Frequency Mask Parameter (F in the paper)

  • time_mask_param (int) –

    Time Mask Parameter (T in the paper)

  • n_freq_mask (int, default: 1 ) –

    Number of frequency masks to apply (mF in the paper). Defaults to 1.

  • n_time_mask (int, default: 1 ) –

    Number of time masks to apply (mT in the paper). Defaults to 1.

  • mask_value (float, default: 0.0 ) –

    Imputation value. Defaults to zero.

Source code in neuralspot_edge/layers/preprocessing/spec_augment.py
def __init__(
    self,
    freq_mask_param: int,
    time_mask_param: int,
    n_freq_mask: int = 1,
    n_time_mask: int = 1,
    mask_value: float = 0.0,
    **kwargs,
):
    """SpecAugment layer w/o time warping

    Args:
        freq_mask_param (int): Frequency Mask Parameter (F in the paper)
        time_mask_param (int): Time Mask Parameter (T in the paper)
        n_freq_mask (int, optional): Number of frequency masks to apply (mF in the paper). Defaults to 1.
        n_time_mask (int, optional): Number of time masks to apply (mT in the paper). Defaults to 1.
        mask_value (float, optional): Imputation value. Defaults to zero.

    """
    super().__init__(**kwargs)
    self.freq_mask_param = freq_mask_param
    self.time_mask_param = time_mask_param
    self.n_freq_mask = n_freq_mask
    self.n_time_mask = n_time_mask
    self.mask_value = keras.ops.cast(mask_value, "float32")

Functions

call
call(inputs: keras.KerasTensor, training=None, **kwargs) -> keras.KerasTensor

Applies the SpecAugment operation to the input Mel Spectrogram

Parameters:

  • inputs (KerasTensor) –

    The input mel spectrogram

  • training (bool, default: None ) –

    Whether the model is training. Defaults to None.

Returns:

  • KerasTensor

    keras.KerasTensor: The mel spectrogram with the SpecAugment applied

Source code in neuralspot_edge/layers/preprocessing/spec_augment.py
def call(self, inputs: keras.KerasTensor, training=None, **kwargs) -> keras.KerasTensor:
    """Applies the SpecAugment operation to the input Mel Spectrogram

    Args:
        inputs (keras.KerasTensor): The input mel spectrogram
        training (bool, optional): Whether the model is training. Defaults to None.

    Returns:
        keras.KerasTensor: The mel spectrogram with the SpecAugment applied
    """

    if training:
        inputs_masked = keras.ops.map(f=self._apply_spec_augment, xs=inputs)
        return inputs_masked
    return inputs
get_config
get_config()

Configuration to initialize the layer

Source code in neuralspot_edge/layers/preprocessing/spec_augment.py
def get_config(self):
    """Configuration to initialize the layer"""
    config = {
        "freq_mask_param": self.freq_mask_param,
        "time_mask_param": self.time_mask_param,
        "n_freq_mask": self.n_freq_mask,
        "n_time_mask": self.n_time_mask,
        "mask_value": self.mask_value.numpy(),
    }
    return config