0

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 Answers 2

0
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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.