'How to filter tensor shape during creating dataset in pytorch?
I have loaded the 1 second audio files in a tensor format and most of them have the [1,22050] tensor size. But several audio files have smaller sizes such as [1,3042] and I want to get rid of them. How to make filter during loading the audio files in a custom dataset?
here is my code:
data_waveform, rate_of_sample = torchaudio.load(audio_file)
if data_waveform.shape ==[1,22050]:
sound_data = data_waveform
else:
pass
sample = {'audio_file': sound_data, 'labels': label}
if self.transform:
sample = self.transform(sample)
return sample
But I am getting the error message such as "UnboundLocalError: local variable 'sound_data' referenced before assignment". How to create tensor size checker for loading only correct sized tensors?
Solution 1:[1]
sound_data_list = []
shape_audio = 22050
data_waveform, rate_of_sample = torchaudio.load(audio_file)
if data_waveform.shape[1] ==shape_audio :
sound_data.append(data_waveform)
else:
pass
if len(sound_data_list) > 0:
sample = {'audio_file': sound_data_list[0], 'labels': label}
if self.transform:
sample = self.transform(sample)
return sample
Solution 2:[2]
I decided to delete files with the different length (tensor size) before creating custom dataset. or I can use padding method to make all the tensors the same shape. Creating the dataset and filtering the tensors by their size in pytorch was not a good idea.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | YScharf |
| Solution 2 | Saltanat Khalyk |
