0

I have audio files and I'd like to make a tf.DataSet from their audio content (i.e. each audio file in the dataset should be represented as a vector of float values)

Here's my code

def convert_audio_file_to_numpy_array(filepath): sample_rate = sox.file_info.sample_rate(filepath) audio, sr = librosa.load(filepath, sr=sample_rate) array = np.asarray(audio) return array filenames_ds = tf.data.Dataset.from_tensor_slices(input_filepaths) waveforms_ds = filenames_ds.map(convert_audio_file_to_numpy_array, num_parallel_calls=tf.data.AUTOTUNE) 

This produces this error: TypeError: stat: path should be string, bytes, os.PathLike or integer, not Tensor

I'm using DataSet's map function following the pattern in this official tutorial (see the call to files_ds.map). In it, the function that map uses takes a filepath.

What am I doing differently to the official tutorial?

2
  • 1
    Please provide the full error traceback. Commented Feb 11, 2021 at 20:52
  • The problem is that the function sox.file_info.sample_rate expects a string or a Path, but the values in your dataset are Tensors (of type string). If you want to load things from a string Tensor, you need to use tf.io functions or similar. Commented Feb 12, 2021 at 8:01

1 Answer 1

2

The problem is that the function def sample_rate(input_filepath: Union[str, Path]) -> float: expects either a string of a pathlib.Path, and you are providing a Tensor. (The elements of your filename_ds are Tensors of type string).

In the tensorflow tutorial, they load the data with a tensorflow functions that expects a Tensor of type string. You should check if you can load your files with tf.audio native functions.

Otherwise, a common workaround is to use a generator with tf.data.Dataset.from_generator, something akin to the following solution:

def generator_func(list_of_path): def convert_audio_file_to_numpy_array(filepath): sample_rate = sox.file_info.sample_rate(filepath) audio, sr = librosa.load(filepath, sr=sample_rate) array = np.asarray(audio) return array for path in list_of_path: yield convert_audio_file_to_numpy_array(path) ds = tf.data.Dataset.from_generator(generator_func, output_types=tf.float32) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.