Load the dataset directory =…
- User
Load the dataset
directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sample_rate, files = load_dataset(directory)
Choose a couple of examples
audio_example_indices = [0, 5, 10, 20]
for idx in audio_example_indices: audio_data = X[idx] speech_segments = vad(audio_data, sample_rate) print(f"File: {files[idx]}, speech segments: {speech_segments}")
time = torch.linspace('/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2/0_0_0_1_0_1_1_0.wav') plt.plot(time, signal) plt.plot(time, upsampled_boundaries.squeeze())
- Assistant
You have a small mistake in using the
torch.linspacefunction. The function requires two arguments,startandend, which represent the time boundaries for the plot's x-axis. Additionally, you need to read the audio file using a suitable library like librosa or torchaudio.Here's how you can fix the code:
import librosa import torchaudio import matplotlib.pyplot as plt import torch # Load the dataset directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sample_rate, files = load_dataset(directory) # Choose a couple of examples audio_example_indices = [0, 5, 10, 20] for idx in audio_example_indices: audio_data = X[idx] speech_segments = vad(audio_data, sample_rate) print(f"File: {files[idx]}, speech segments: {speech_segments}") time = torch.linspace(0, len(audio_data)/sample_rate, len(audio_data)) plt.plot(time, audio_data) # Load the example file and find the speech boundaries audio_data, sample_rate = torchaudio.load(files[idx]) upsampled_boundaries = vad(audio_data, sample_rate) plt.plot(time, upsampled_boundaries.squeeze()) plt.show() - User
File: 0_0_1_0_0_0_1_0.wav, speech segments: [[22591, 28651], [34162, 40773], [46284, 51793], [52345, 52895], [57855, 63915], [68875, 74935], [80446, 86506], [92568, 96975], [103037, 107444]]
RuntimeError Traceback (most recent call last) in <cell line: 13>() 20 21 # Load the example file and find the speech boundaries ---> 22 audio_data, sample_rate = torchaudio.load(files[idx]) 23 upsampled_boundaries = vad(audio_data, sample_rate) 24 plt.plot(time, upsampled_boundaries.squeeze())
1 frames /usr/local/lib/python3.10/dist-packages/torchaudio/io/_compat.py in load_audio(src, frame_offset, num_frames, convert, channels_first, format) 105 format: Optional[str] = None, 106 ) -> Tuple[torch.Tensor, int]: --> 107 s = torch.classes.torchaudio.ffmpeg_StreamReader(src, format, None) 108 return _load_audio(s, frame_offset, num_frames, convert, channels_first) 109
RuntimeError: Failed to open the input "0_0_1_0_0_0_1_0.wav" (No such file or directory).
- Assistant
The error message indicates that the file "0_0_1_0_0_0_1_0.wav" was not found. The issue might be caused by the fact that the
fileslist contains only the filenames without the full path. You should add the directory path to the filename when loading the file with torchaudio.Here's the updated code snippet:
import librosa import torchaudio import matplotlib.pyplot as plt import torch # Load the dataset directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sample_rate, files = load_dataset(directory) # Choose a couple of examples audio_example_indices = [0, 5, 10, 20] for idx in audio_example_indices: audio_data = X[idx] speech_segments = vad(audio_data, sample_rate) print(f"File: {files[idx]}, speech segments: {speech_segments}") time = torch.linspace(0, len(audio_data)/sample_rate, len(audio_data)) plt.plot(time, audio_data) # Load the example file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, files[idx])) upsampled_boundaries = vad(audio_data, sample_rate) plt.plot(time, upsampled_boundaries.squeeze()) plt.show()