Commit 7cefe4a7 authored by Keunwoo Choi's avatar Keunwoo Choi
Browse files

update with multi-processing and comments

parent 782b6c16
......@@ -6,6 +6,8 @@ from models_transfer import build_convnet_model
import numpy as np
import keras
import kapre
import multiprocessing
SR = 12000 # [Hz]
LEN_SRC = 29. # [second]
......@@ -16,6 +18,7 @@ if keras.__version__[0] != '1':
def load_model(mid_idx):
"""Load one model and return it"""
assert 0 <= mid_idx <= 4
args = Namespace(test=False, data_percent=100, model_name='', tf_type='melgram',
normalize='no', decibel=True, fmin=0.0, fmax=6000,
......@@ -28,6 +31,7 @@ def load_model(mid_idx):
def load_audio(audio_path):
"""Load audio file, shape it and return"""
src, sr = librosa.load(audio_path, sr=SR, duration=LEN_SRC)
len_src = len(src)
if len_src < ref_n_src:
......@@ -38,17 +42,39 @@ def load_audio(audio_path):
return src[np.newaxis, np.newaxis, :ref_n_src]
def main(txt_path, out_path):
def _paths_models_generator(lines, models):
for line in lines:
yield (line.rstrip('\n'), models)
def _predict_one(args):
"""target function in pool.map()"""
line, models = args
audio_path = line.rstrip('\n')
print('Loading/extracting {}...'.format(audio_path))
src = load_audio(audio_path)
features = [models[i].predict(src)[0] for i in range(5)]
return np.concatenate(features, axis=0)
def predict_cpu(f_path, models, n_jobs):
"""Predict features with multiprocessing
path_line: string, path + '\n'
models: five models for each layer
"""
pool = multiprocessing.Pool(processes=n_jobs)
paths = f_path.readlines()
arg_gen = _paths_models_generator(paths, models)
features = pool.map(_predict_one, arg_gen)
return np.array(features, dtype=np.float32)
def main(txt_path, out_path, n_jobs=1):
models = [load_model(mid_idx) for mid_idx in range(5)] # for five models...
all_features = []
with open(txt_path) as f_path:
for line in f_path:
path = line.rstrip('\n')
print('Loading/extracting {}...'.format(path))
src = load_audio(path)
features = [models[i].predict(src)[0] for i in range(5)]
all_features.append(np.concatenate(features, axis=0))
all_features = np.array(all_features, dtype=np.float32)
with open(txt_path) as f_path:
all_features = predict_cpu(f_path, models, n_jobs)
print('Saving all features at {}..'.format(out_path))
np.save(out_path, all_features)
print('Done. Saved a numpy array size of (%d, %d)' % all_features.shape)
......@@ -58,11 +84,11 @@ def warning():
print('-' * 65)
print(' * Python 2.7-ish')
print(' * Keras 1.2.2,')
print(' * Kapre old one (git checkout a3bde3e)')
print(' * Kapre old one (git checkout a3bde3e, see README)')
print(" * Read README.md. Come on, it's short..")
print('')
print(' Usage: ')
print('$ python easy_feature_extraction.py audio_paths.txt features.npy')
print(' Usage: set path file, numpy file, and n_jobs >= 1')
print('$ python easy_feature_extraction.py audio_paths.txt features.npy 8')
print ''
print(' , where audio_path.txt is for paths audio line-by-line')
print(' and features.npy is the path to store result feature array.')
......@@ -74,4 +100,6 @@ if __name__ == '__main__':
txt_path = sys.argv[1]
out_path = sys.argv[2]
main(txt_path, out_path)
n_jobs = int(sys.argv[3])
main(txt_path, out_path, n_jobs)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment