TorchLibrosa: PyTorch implementation of Librosa
This codebase provides PyTorch implementation of some librosa functions. If users previously used for training cpu-extracted features from librosa, but want to add GPU acceleration during training and evaluation, TorchLibrosa will provide almost identical features to standard torchlibrosa functions (numerical difference less than 1e-5).
Install
$ pip install torchlibrosa
Examples 1
Extract Log mel spectrogram with TorchLibrosa.
import torch
import torchlibrosa as tl
batch_size = 16
sample_rate = 22050
win_length = 2048
hop_length = 512
n_mels = 128
batch_audio = torch.empty(batch_size, sample_rate).uniform_(-1, 1) # (batch_size, sample_rate)
# TorchLibrosa feature extractor the same as librosa.feature.melspectrogram()
feature_extractor = torch.nn.Sequential(
tl.Spectrogram(
hop_length=hop_length,
win_length=win_length,
), tl.LogmelFilterBank(
sr=sample_rate,
n_mels=n_mels,
is_log=False, # Default is true
))
batch_feature = feature_extractor(batch_audio) # (batch_size, 1, time_steps, mel_bins)
Examples 2
Extracting spectrogram, then log mel spectrogram, STFT and ISTFT with TorchLibrosa.
import torch
import torchlibrosa as tl
batch_size = 16
sample_rate = 22050
win_length = 2048
hop_length = 512
n_mels = 128
batch_audio = torch.empty(batch_size, sample_rate).uniform_(-1, 1) # (batch_size, sample_rate)
# Spectrogram
spectrogram_extractor = tl.Spectrogram(n_fft=win_length, hop_length=hop_length)
sp = spectrogram_extractor.forward(batch_audio) # (batch_size, 1, time_steps, freq_bins)
# Log mel spectrogram
logmel_extractor = tl.LogmelFilterBank(sr=sample_rate, n_fft=win_length, n_mels=n_mels)
logmel = logmel_extractor.forward(sp) # (batch_size, 1, time_steps, mel_bins)
# STFT
stft_extractor = tl.STFT(n_fft=win_length, hop_length=hop_length)
(real, imag) = stft_extractor.forward(batch_audio)
# real: (batch_size, 1, time_steps, freq_bins), imag: (batch_size, 1, time_steps, freq_bins) #
# ISTFT
istft_extractor = tl.ISTFT(n_fft=win_length, hop_length=hop_length)
y = istft_extractor.forward(real, imag, length=batch_audio.shape[-1]) # (batch_size, samples_num)
Example 3
Check the compability of TorchLibrosa to Librosa. The numerical difference should be less than 1e-5.
python3 torchlibrosa/stft.py --device='cuda' # --device='cpu' | 'cuda'
Contact
Qiuqiang Kong, qiuqiangkong@gmail.com
Cite
[1] Qiuqiang Kong, Yin Cao, Turab Iqbal, Yuxuan Wang, Wenwu Wang, and Mark D. Plumbley. "PANNs: Large-scale pretrained audio neural networks for audio pattern recognition." IEEE/ACM Transactions on Audio, Speech, and Language Processing 28 (2020): 2880-2894.
External links
Other related repos include:
torchaudio: https://github.com/pytorch/audio
Asteroid-filterbanks: https://github.com/asteroid-team/asteroid-filterbanks
Metadata
Release files for torchlibrosa 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchlibrosa-0.1.0.tar.gz | 11.7 kB | Without provenance | Details for torchlibrosa-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchlibrosa-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for torchlibrosa-0.1.0-py3-none-any.whl |
Total release size: 23.5 kB
Release files / torchlibrosa-0.1.0.tar.gz
| Download URL | torchlibrosa-0.1.0.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
62a8beedf9c9b4141a06234df3f10229f7ba86e67678ccee02489ec4ef044028
|
|
BLAKE2b-256 checksum How to use checksums |
a467e4c79da3f15777b9bc2b655d47dac553fc31e40360500fef5e66d6877ce8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.5
|
Release files / torchlibrosa-0.1.0-py3-none-any.whl
| Download URL | torchlibrosa-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
89b65fd28b833ceb6bc74a3d0d87e2924ddc5a845d0a246b194952a4e12a38cb
|
|
BLAKE2b-256 checksum How to use checksums |
3eafccf007edf442c3c0cd3a98be2c82bc99edc957c04436a759b6e1e01077e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.5
|
Security for torchlibrosa 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.