How to use the torchaudio.transforms.Transform function in torchaudio

To help you get started, we’ve selected a few torchaudio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github willfrey / audio / tests / test_transforms.py View on Github external
def set_up(self):
        self.transform = transforms.Transform()
github willfrey / audio / test / test_transforms.py View on Github external
def setUp(self):
        self.transforms = [torchaudio.transforms.Transform(),
                           torchaudio.transforms.Transform()]
        self.args = [torchaudio.transforms.Transform(),
                     torchaudio.transforms.Transform()]
github willfrey / audio / test / test_transforms.py View on Github external
def setUp(self):
        self.transforms = [torchaudio.transforms.Transform(),
                           torchaudio.transforms.Transform()]
        self.args = [torchaudio.transforms.Transform(),
                     torchaudio.transforms.Transform()]
github willfrey / audio / torchaudio / transforms.py View on Github external
shift = np.random.randint(*self.min_max_displacement)
            if np.random.binomial(1, 0.5):  # shift to the right
                y[shift:] = y[:-shift]
                y[:shift] = 0
            else:
                y[:-shift] = y[shift:]
                y[-shift:] = 0

        return y


############################
# Text-Oriented Transforms #
############################

class CharToInt(Transform):
    """Maps a string or other iterable, character-wise, to integer labels.

    Attributes:
        char_to_index: A dictionary containing character keys and
        integer values.

    """

    def __init__(self, char_to_index):
        self.char_to_index = char_to_index

    def __call__(self, data):
        return [int_ for int_ in map(self.char_to_index, data)
                if int_ is not None]
github willfrey / audio / torchaudio / transforms.py View on Github external
"""Converts a numpy.ndarray to a torch.*Tensor."""

    def __call__(self, nparray):
        # pylint: disable=E1101
        return torch.from_numpy(nparray)
        # pylint: enable=E1101


class ToArray(Transform):
    """Converts a torch.*Tensor to a numpy.ndarray."""

    def __call__(self, tensor):
        return tensor.numpy()


class Lambda(Transform):
    """Applies a lamba as a transform.

    Attributes:
        func: A lambda function to be applied to data.
    """

    def __init__(self, func):
        """Inits Lambda with func."""
        assert isinstance(func, types.LambdaType)
        self.func = func

    def __call__(self, data):
        return self.func(data)


#############################
github willfrey / audio / torchaudio / transforms.py View on Github external
"""
        self._transforms = list(transforms)

    def __len__(self):
        return len(self._transforms)

    def __getitem__(self, index):
        return self._transforms[index]

    def __call__(self, data):
        for transform in self._transforms:
            data = transform(data)
        return data


class ToTensor(Transform):
    """Converts a numpy.ndarray to a torch.*Tensor."""

    def __call__(self, nparray):
        # pylint: disable=E1101
        return torch.from_numpy(nparray)
        # pylint: enable=E1101


class ToArray(Transform):
    """Converts a torch.*Tensor to a numpy.ndarray."""

    def __call__(self, tensor):
        return tensor.numpy()


class Lambda(Transform):
github willfrey / audio / torchaudio / transforms.py View on Github external
def __call__(self, y):
        return librosa.feature.melspectrogram(y=y, **self.__dict__)


class LogMelSpectrogram(_Structure, Compose):
    """Computes the log-power Mel spectrogram of an input signal."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._transforms = [
            MelSpectrogram(*args, **kwargs),
            LogAmplitude(*args, **kwargs)
        ]


class MFCC(_Structure, Transform):
    """Computes the mel-frequency cepstral coefficients of an input signal."""

    def __call__(self, y):
        return librosa.feature.mfcc(y, **self.__dict__)


class StackMemory(_Structure, Transform):
    """Short-term history embedding.

    Vertically concatenate a data vector or matrix with delayed
    copies of itself.
    """

    def __call__(self, data):
        return librosa.feature.stack_memory(data, **self.__dict__)
github willfrey / audio / torchaudio / transforms.py View on Github external
def __call__(self, y):
        # Vectorize this?
        for i in range(1, len(y) - 1):
            # pylint: disable=E1101
            success = np.random.binomial(1, self.probability)
            if success:
                swap_right = np.random.binomial(1, 0.5)
                if swap_right:
                    y[i], y[i + 1] = y[i + 1], y[i]
                else:
                    y[i], y[i - 1] = y[i - 1], y[i]
        return y


class TimeShift(Transform):
    """Stochastically shifts audio samples."""

    def __init__(self, probability, min_max_displacement=(80, 81)):
        self.probability = probability
        self.min_max_displacement = min_max_displacement

    def __call__(self, y):
        # pylint: disable=E1101
        success = np.random.binomial(1, self.probability)
        if success:
            shift = np.random.randint(*self.min_max_displacement)
            if np.random.binomial(1, 0.5):  # shift to the right
                y[shift:] = y[:-shift]
                y[:shift] = 0
            else:
                y[:-shift] = y[shift:]
github willfrey / audio / torchaudio / transforms.py View on Github external
def __call__(self, data):
        for transform in self._transforms:
            data = transform(data)
        return data


class ToTensor(Transform):
    """Converts a numpy.ndarray to a torch.*Tensor."""

    def __call__(self, nparray):
        # pylint: disable=E1101
        return torch.from_numpy(nparray)
        # pylint: enable=E1101


class ToArray(Transform):
    """Converts a torch.*Tensor to a numpy.ndarray."""

    def __call__(self, tensor):
        return tensor.numpy()


class Lambda(Transform):
    """Applies a lamba as a transform.

    Attributes:
        func: A lambda function to be applied to data.
    """

    def __init__(self, func):
        """Inits Lambda with func."""
        assert isinstance(func, types.LambdaType)