0

I'm trying to prepare some audio data for a Dataloader but I'm struggling with a few points. At the moment my data is organised into two lists inputs and target, which are both length 32, but have different dimensions for their elements; inputs[0].shape = (8, 3690288) (8 mono audio tracks) and target[0].shape = (2, 3690288) (a single stereo mix).

I've converted each array to a tensor by:

tensor_inputs = torch.Tensor(inputs)
tensor_target = torch.Tensor(target)

which seems to work: tensor_inputs.shape = torch.Size([32, 8, 3690288]). I've then tried to convert each of these to a melspectrogram:

melspectrogram = torchaudio.transforms.melspectrogram(
        sr=44100,
        n_fft=1024,
        hop_length=512,
        n_mels=64)

tensor_input_specs = []

for i in range(len(tensor_inputs)):
    spec = mel_spectrogram(tensor_inputs[i])
    tensor_input_specs.append(spec)
    
tensor_target_specs = []

for i in range(len(tensor_target)):
    spec = mel_spectrogram(tensor_target[i])
    tensor_target_specs.append(spec)

and then move these into a Dataloader by doing:

dataset = TensorDataset(tensor_input_specs,tensor_target_specs)
iter = DataLoader(dataset)

However I get the following error: AttributeError: 'list' object has no attribute 'size', which I imagine is due to the fact that I'm appending the spectrograms to a list, but I'm not sure how else to achieve this.

EDIT:

AttributeError                            Traceback (most recent call last)
C:\Users\BRUDAL~1\AppData\Local\Temp/ipykernel_24968/2240294361.py in <module>
     14     tensor_target_specs.append(spec)
     15 
---> 16 dataset = TensorDataset(tensor_input_specs,tensor_target_specs)
     17 iter = DataLoader(dataset) # create your dataloader

~\anaconda3\lib\site-packages\torch\utils\data\dataset.py in __init__(self, *tensors)
    165 
    166     def __init__(self, *tensors: Tensor) -> None:
--> 167         assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors), "Size mismatch between tensors"
    168         self.tensors = tensors
    169 

~\anaconda3\lib\site-packages\torch\utils\data\dataset.py in <genexpr>(.0)
    165 
    166     def __init__(self, *tensors: Tensor) -> None:
--> 167         assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors), "Size mismatch between tensors"
    168         self.tensors = tensors
    169 

AttributeError: 'list' object has no attribute 'size'
5
  • please paste the full error. I can't see where you used the size attribute. Commented Aug 1, 2022 at 11:32
  • 1
    @PrakharSharma I've just pasted the full error in an edit to the question. Commented Aug 1, 2022 at 11:38
  • here you go. In the error see line 167 with a -->. assert all(tensors[0].size(0) == tensor.size(0). You need to pass tensors not a list. Obviously list does not have size attribute, it uses len. I would suggest you to print thetype of tensor_input_specs and tensor_target_specs to make sure they are tensor. Commented Aug 1, 2022 at 11:45
  • 1
    Ok I got it. This is a list tensor_target_specs = []. Use torch.empty and then append using torch.cat. Commented Aug 1, 2022 at 11:49
  • 1
    @PrakharSharma Thank you for the pointers, they led me to finding what seems like the easiest way, through using torch.stack(tensor_target_specs). I'll update with an answer soon. Commented Aug 1, 2022 at 12:13

1 Answer 1

1

The most straightforward method I've found is by stacking the list after the for loops, by using torch.stack:

tensor_input_specs = []

for i in range(len(tensor_inputs)):
    spec = mel_spectrogram(tensor_inputs[i])
    tensor_input_specs.append(spec)
    
tensor_input_specs = torch.stack(train_tensor_input_specs)

tensor_input_specs.shape

>>> torch.size([32, 8, 64, 7208])
Sign up to request clarification or add additional context in comments.

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.