Librosa Memory Error

432 views
Skip to first unread message

W L

unread,
Nov 24, 2017, 4:04:00 PM11/24/17
to librosa
I'm using librosa's beat_track function to detect the times of beats in a wav file for a game in Pygame. However, when I try to store the beat times in a list, an error occurs. When I run the beat detection in a separate file without Pygame, it works but is somewhat slow. How do I fix this memory error?

Error message: 
File "Game2.py", line 98, in analyze
    tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
  File "C:\Python34\lib\site-packages\librosa\beat.py", line 188, in beat_track
    start_bpm=start_bpm)[0]
  File "C:\Python34\lib\site-packages\librosa\beat.py", line 442, in tempo
    win_length=win_length)
  File "C:\Python34\lib\site-packages\librosa\feature\rhythm.py", line 160, in tempogram
    axis=0),
  File "C:\Python34\lib\site-packages\librosa\core\audio.py", line 436, in autocorrelate
    autocorr = fft.ifft(powspec, axis=axis, overwrite_x=True)
  File "C:\Python34\lib\site-packages\scipy\fftpack\basic.py", line 360, in ifft
    tmp = work_function(tmp,n,-1,1,overwrite_x)
MemoryError

Brian McFee

unread,
Nov 27, 2017, 9:06:56 AM11/27/17
to librosa
That's pretty strange.  How long is your signal?

It might be more appropriate to handle this in the github issues, especially if you could provide more information about your installation and software versions.

W L

unread,
Nov 27, 2017, 11:29:46 AM11/27/17
to librosa
I'm loading a wav file that 4 minutes long. What I've done is split the wav file into 2 minute segments and process each one separately, then combine the times. 

Brian McFee

unread,
Nov 27, 2017, 11:32:57 AM11/27/17
to librosa
A 4-minute wav file should definitely not be crashing out of memory, unless you're on a severely limited device.

W L

unread,
Nov 28, 2017, 10:17:33 PM11/28/17
to librosa
When I check task manager to see how much memory is being used up, it's around 850 MB, which isn't alot. Is it because I am using Librosa with other modules, such as pygame and openCV? When I removed the other modules, librosa was able to run the beat detection but it took around 10 seconds for a 4 minute wav file.

Brian McFee

unread,
Nov 29, 2017, 8:50:51 AM11/29/17
to librosa
I haven't worked in pygame, or opencv's python bindings, but if either of those have memory leaks, that could be an issue.

Otherwise, that sounds plausible for timing, depending on how you're counting and what your hardware is.

Here's what I get on my laptop for a 4:45 track:

 → time ./beat_tracker.py ~/data/MedleyDB_sample/Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav beats.csv
Loading  /home/bmcfee/data/MedleyDB_sample/Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav
Tracking beats
Estimated tempo: 129.20 beats per minute
Saving output to  beats.csv
done!
13.22user 0.90system 0:13.31elapsed 106%CPU (0avgtext+0avgdata 399924maxresident)k
24inputs+32outputs (1major+81308minor)pagefaults 0swaps

 → soxi ~/data/MedleyDB_sample/Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav 

Input File     : '/home/bmcfee/data/MedleyDB_sample/Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:04:44.91 = 12564480 samples = 21368.2 CDDA sectors
File Size      : 50.3M
Bit Rate       : 1.41M
Sample Encoding: 16-bit Signed Integer PCM


Running it through a profiler, most of the time is spent in load and resampling to 22KHz.  Factoring out those parts and timing just the beat tracker gives me about 1.5s on average:

In [2]: y, sr = librosa.load('/home/bmcfee/data/MedleyDB_sample/Audio/LizNelson_Rainfall/LizNelson_Rainfall_MIX.wav', sr=22050)

In [3]: %timeit librosa.beat.beat_track(y=y, sr=sr)
1.49 s ± 10.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


Depending on what else you're doing, this could be made more efficient by precomputing spectrograms or onset strength envelopes.


FWIW, my system info is as follows:

In [2]: np.show_config()
lapack_mkl_info:
  NOT AVAILABLE
blas_mkl_info:
  NOT AVAILABLE
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
    library_dirs = ['/home/bmcfee/miniconda/envs/py35/lib']
openblas_info:
    libraries = ['openblas', 'openblas']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
    library_dirs = ['/home/bmcfee/miniconda/envs/py35/lib']
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
    library_dirs = ['/home/bmcfee/miniconda/envs/py35/lib']
blis_info:
  NOT AVAILABLE
blas_opt_info:
    libraries = ['openblas', 'openblas']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
    library_dirs = ['/home/bmcfee/miniconda/envs/py35/lib']


In [2]: librosa.show_versions()
INSTALLED VERSIONS
------------------
python: 3.5.4 | packaged by conda-forge | (default, Nov  4 2017, 10:11:29) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]

librosa: 0.5.1

audioread: installed, no version number available
numpy: 1.13.3
scipy: 1.0.0
sklearn: 0.19.1
joblib: 0.11
decorator: 4.1.2
six: 1.11.0
resampy: 0.2.0

numpydoc: 0.7.0
sphinx: 1.6.5
sphinx_rtd_theme: 0.2.4
sphinxcontrib.versioning: 2.2.1
matplotlib: 2.1.0
numba: 0.35.0+10.g143f70e90

CPU: Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
 → free -h
              total        used        free      shared  buff/cache   available
Mem:           7.5G        6.0G        447M        186M        1.1G        1.0G
Reply all
Reply to author
Forward
0 new messages