Unable to use Multithread for librosa melspectrogram

自古美人都是妖i 提交于 2019-12-04 20:21:23

I recommend using joblib to parallel process with librosa. I believe librosa is using it internally, so this might avoid some conflicts. Below is a working example, based on code that I regularly use to process some 10k files.

import os.path
import joblib
import librosa
import numpy

def compute(inpath, outpath):
    y, sr = librosa.load(inpath)
    S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
    numpy.save(outpath, S)
    return outpath

out_dir = 'temp/'
n_jobs=8
verbose=1

# as an reproducable example just processes the same input file
# but making sure to give them unique output names
inputs = [ librosa.util.example_audio_file() ] * 10
outputs = [ os.path.join(out_dir, '{}.npy'.format(n)) for n in range(len(inputs)) ]

jobs = [ joblib.delayed(compute)(i, o) for i,o in zip(inputs, outputs) ]
out = joblib.Parallel(n_jobs=n_jobs, verbose=verbose)(jobs)

print(out)

Output

[Parallel(n_jobs=8)]: Using backend LokyBackend with 8 concurrent workers.
[Parallel(n_jobs=8)]: Done   6 out of  10 | elapsed:   10.4s remaining:    6.9s
[Parallel(n_jobs=8)]: Done  10 out of  10 | elapsed:   13.2s finished
['temp/0.npy', 'temp/1.npy', 'temp/2.npy', 'temp/3.npy', 'temp/4.npy', 'temp/5.npy', 'temp/6.npy', 'temp/7.npy', 'temp/8.npy', 'temp/9.npy']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!