Ko *_*shi 17 python tensorflow tensorflow-datasets
我想使用 tf.data.Dataset.list_files 函数来提供我的数据集。
但是因为文件不是图像,我需要手动加载它。
问题是 tf.data.Dataset.list_files 将变量作为 tf.tensor 传递,而我的 python 代码无法处理张量。
如何从 tf.tensor 获取字符串值。dtype 是字符串。
train_dataset = tf.data.Dataset.list_files(PATH+'clean_4s_val/*.wav')
train_dataset = train_dataset.map(lambda x: load_audio_file(x))
def load_audio_file(file_path):
print("file_path: ", file_path)
# i want do something like string_path = convert_tensor_to_string(file_path)
Run Code Online (Sandbox Code Playgroud)
file_path 是 Tensor("arg0:0", shape=(), dtype=string)
我使用 tensorflow 1.13.1 和 Eager 模式。
提前致谢
gis*_*ang 25
您可以使用tf.py_func来包装load_audio_file().
import tensorflow as tf
tf.enable_eager_execution()
def load_audio_file(file_path):
# you should decode bytes type to string type
print("file_path: ",bytes.decode(file_path),type(bytes.decode(file_path)))
return file_path
train_dataset = tf.data.Dataset.list_files('clean_4s_val/*.wav')
train_dataset = train_dataset.map(lambda x: tf.py_func(load_audio_file, [x], [tf.string]))
for one_element in train_dataset:
print(one_element)
file_path: clean_4s_val/1.wav <class 'str'>
(<tf.Tensor: id=32, shape=(), dtype=string, numpy=b'clean_4s_val/1.wav'>,)
file_path: clean_4s_val/3.wav <class 'str'>
(<tf.Tensor: id=34, shape=(), dtype=string, numpy=b'clean_4s_val/3.wav'>,)
file_path: clean_4s_val/2.wav <class 'str'>
(<tf.Tensor: id=36, shape=(), dtype=string, numpy=b'clean_4s_val/2.wav'>,)
Run Code Online (Sandbox Code Playgroud)
TF 2更新
上面的解决方案不适用于 TF 2(用 2.2.0 测试),即使替换tf.py_func为tf.py_function,给
InvalidArgumentError: TypeError: descriptor 'decode' requires a 'bytes' object but received a 'tensorflow.python.framework.ops.EagerTensor'
Run Code Online (Sandbox Code Playgroud)
要使其在 TF 2 中工作,请进行以下更改:
tf.enable_eager_execution()(默认情况下在 TF 2 中启用了eager ,您可以通过tf.executing_eagerly()返回来验证True)tf.py_func为tf.py_functionfile_path用file_path.numpy()| 归档时间: |
|
| 查看次数: |
17101 次 |
| 最近记录: |