How to filter tensor shape during creating dataset in pytorch?

207 Views Asked by At

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?

2

There are 2 best solutions below

0
On
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
0
On

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.