2017-04-12 89 views
1

我正在尝试训练音频数据的一些模型。我写了一些代码来加载一些mp3文件,将它们分成短片(每片大约0.1秒)并分批分析这些片段。所以,我写了这段代码。在张量流中批处理音频数据

 
import glob 
import tensorflow as tf 
from tensorflow.contrib import ffmpeg 

def load(fname): 
    binary = tf.read_file(fname) 
    return ffmpeg.decode_audio(binary, file_format='mp3', samples_per_second=44100, channel_count=2) 

def preprocess(audio, seconds_per_sample=0.1, rate=44100): 
    # pad to a with 1 second of silence front and back 
    front = tf.zeros([rate, 2], dtype=audio.dtype) 
    back = tf.zeros([rate - tf.mod(tf.shape(audio)[0], rate) + rate, 2], dtype=audio.dtype) 
    audio = tf.concat([front, audio, back], 0) 
    # normalize to 0 to 1 range 
    audio = tf.add(audio, tf.abs(tf.reduce_min(audio))) 
    audio = tf.multiply(audio, 1.0/tf.reduce_max(audio)) 
    # [data, channels] => [samples, data, channels] 
    audio = tf.reshape(audio, [-1, int(rate * seconds_per_sample), 2]) 
    return audio 

tf.reset_default_graph() 
with tf.Graph().as_default(): 
    # take files one by one and read data from them 
    files = glob.glob('music/*.mp3')  
    queue = tf.train.string_input_producer(files, num_epochs=1) 
    fname = queue.dequeue() 
    audio = load(fname) 
    audio = preprocess(audio) 
    samples = tf.train.slice_input_producer([audio], num_epochs=1) 
    batch = tf.train.batch(samples, 10) 

    model = tf.identity(batch) 

    init = [tf.global_variables_initializer(), tf.local_variables_initializer()] 

    coord = tf.train.Coordinator() 

    with tf.Session() as session: 
     session.run(init) 
     threads = tf.train.start_queue_runners(sess=session, coord=coord) 
     for _ in range(10): 
      try: 
       result = session.run(model) 
      except tf.errors.OutOfRangeError: 
       coord.request_stop() 
     coord.request_stop() 
     coord.join(threads) 

这似乎很简单,类似的方法为我以前的模型工作。我重塑了音频数据,所以第一个维度变成样本,使用切片输入将样本排队,然后使用batch()将样本10一次送入模型。为了简单起见,我将模型作为身份函数离开。这段代码使我的python segfault处于tensorflow深处。有什么我明显错误的吗?

这里是OSX崩溃报告的开始

 
Process:    Python [57865] 
Path:     /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python 
Identifier:   Python 
Version:    3.6.1 (3.6.1) 
Code Type:    X86-64 (Native) 
Parent Process:  Python [57654] 
Responsible:   Python [57865] 
User ID:    502 

Date/Time:    2017-04-12 16:07:13.318 -0400 
OS Version:   Mac OS X 10.12.3 (16D32) 
Report Version:  12 
Anonymous UUID:  B5DE676B-FEC7-9626-B1CC-F392948D410C 

Sleep/Wake UUID:  F3A5360E-B7A0-4675-9DC9-EAEE938E2E70 

Time Awake Since Boot: 440000 seconds 
Time Since Wake:  16000 seconds 

System Integrity Protection: disabled 

Crashed Thread:  16 

Exception Type:  EXC_CRASH (SIGABRT) 
Exception Codes:  0x0000000000000000, 0x0000000000000000 
Exception Note:  EXC_CORPSE_NOTIFY 

Application Specific Information: 
abort() called 

编辑:我在GitHub上打开问题是没有解释关闭,但“看问题跟踪策略”。我不知道我还能在这里做什么。如果有人有任何解决这个问题的方法,请做。

回答

0

在运行代码之前您已经在我的计算机上发布了一些信息,但我不得不将一些MP3文件添加到“音乐”文件夹中。我假设你有一些音频,但也请注意ffmpeg二进制。 Tensorflow要求ffmpeg位于/usr/local/sbin/文件夹中。

一个快速解决方法

一个通常的符号链接为我工作。

ln -s /usr/bin/ffmpeg /usr/local/sbin/ffmpeg

如果这个答案是没有帮助的话,请通过运行终端仿真器的代码,并张贴在这里回溯提供更多信息。

+1

我不认为tensorflow关心二进制,它似乎直接链接到ffmpeg库。但无论哪种方式,如果我在没有所有批处理代码的情况下执行相同的操作,只需使用load()所做的相同操作加载一个文件,然后查看其工作正常的输出。 –