我对tf不熟悉,我在处理一些文件时遇到了一个问题。这是代码的摘录。
xlabel_to_files_list_map['dog_bark'] # subset of data with two files
# result
['gs://some_bucket/some_dir/data/dog_bark/100652.mp3', 'gs://some_bucket/some_dir/dog_bark/100795.mp3']
在这里,我只想通过一个简单的图表来处理这些字符串:
file_to_process = tf.placeholder(tf.string)
audio_binary_remote = tf.gfile.Open(file_to_process, 'rb').read()
waveform = tf.contrib.ffmpeg.decode_audio(audio_binary_remote, file_format='mp3', samples_per_second=44100, channel_count=2)
with tf.Session() as sess:
result = sess.run(waveform, feed_dict={
file_to_process: xlabel_to_files_list_map['dog_bark']
})
#print (result)
这导致
TypeError: Expected binary or unicode string, got <tf.Tensor 'Placeholder_9:0' shape=<unknown> dtype=string>
FWIW,这项工作
a_string = tf.placeholder(tf.string)
z = a_string
with tf.Session() as sess:
result = sess.run(z, feed_dict={a_string: ['one', 'two', 'three']})
print(result)
这导致
['one' 'two' 'three']
有效的简单示例是字符串列表。使用哈希映射值部分的更复杂的示例,该部分是字符串列表。我不知道为什么它不能像第二个例子那样工作。
另一种方法
我试着用另一种方法。这一次,我尝试构建一个结果列表,然后处理该列表。这也失败了。它没有产生错误。它只是给出了空白的结果。
waveform_tensor_list = []
for a_file in dir_to_selected_files_list_map['gs://some_bucket/some_dir/dog_bark/']:
#print (a_file)
waveform = tf.contrib.ffmpeg.decode_audio(a_file, file_format='mp3', samples_per_second=44100, channel_count=2)
waveform_tensor_list.append(waveform)
此单元格的输出立即看起来错误,但格式正确:
waveform_tensor_list
导致:
[<tf.Tensor 'DecodeAudioV2_7:0' shape=(?, 2) dtype=float32>,
<tf.Tensor 'DecodeAudioV2_8:0' shape=(?, 2) dtype=float32>,
stuff deleted,
<tf.Tensor 'DecodeAudioV2_22:0' shape=(?, 2) dtype=float32>,
<tf.Tensor 'DecodeAudioV2_23:0' shape=(?, 2) dtype=float32>]
然后,要评估图表,请执行以下操作:
with tf.Session() as sess:
result = sess.run(waveform_tensor_list)
print (result)
如果此单元格的输出是:
[array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32), array([], shape=(0, 0), dtype=float32)]