RuntimeError: stack expects each tensor to be equal size, but got [1, 6502400] at entry 0 and [2, 2173694] at entry 1

username: Isaiah_addie
cooked: <p>Im trying to train a model to know whats background noise and whats not and I keep getting this error. Ive truncated it so it should be the same size but its not, any help would be appreciated! Im new to this so im not sure why the post is popping up weird</p>
<p>import torch<br>
import torchaudio<br>
from torch import nn<br>
import torch.nn.functional as F<br>
from torch.utils.data import Dataset, DataLoader</p>
<h1><a name="load-background-noise-and-speech-audio-files-1" class="anchor" href="#load-background-noise-and-speech-audio-files-1"></a>Load background noise and speech audio files</h1>
<p>background_files = [“C:\Users\iaddi\Downloads\crowd_talking-6762.mp3”]<br>
speech_files = [“C:\Users\iaddi\Downloads\Cam_1.mp3”]</p>
<h1><a name="audio-preprocessing-2" class="anchor" href="#audio-preprocessing-2"></a>Audio preprocessing</h1>
<p>def pad_truncate(audio, max_length=16000):<br>
print(f"Before: {len(audio)}")<br>
if len(audio) &gt; max_length:<br>
audio = audio[:max_length]<br>
elif len(audio) &lt; max_length:<br>
audio = F.pad(audio, (0, max_length - len(audio)))</p>
<pre><code>return audio
</code></pre>
<h1><a name="create-dataset-3" class="anchor" href="#create-dataset-3"></a>Create dataset</h1>
<p>class AudioDataset(Dataset):<br>
def <strong>init</strong>(self, background_files, speech_files):<br>
self.background_files = background_files<br>
self.speech_files = speech_files</p>
<pre><code>def __getitem__(self, index):
    if index &lt; len(self.background_files):
        audio, sample_rate = torchaudio.load(self.background_files[index])
        audio = audio.squeeze(0)
        audio = pad_truncate(audio)
        print(len(audio))
        label = 0
    else:
        audio, sample_rate = torchaudio.load(self.speech_files[index-len(self.background_files)]) 
        label = 1
    return audio, label

def __len__(self):
    return len(self.background_files) + len(self.speech_files)
</code></pre>
<h1><a name="create-model-4" class="anchor" href="#create-model-4"></a>Create model</h1>
<p>class AudioClassifier(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(1, 16, 3, stride=2, padding=1)<br>
self.conv2 = nn.Conv2d(16, 32, 3, stride=2, padding=1)<br>
self.conv3 = nn.Conv2d(32, 64, 3, stride=2, padding=1)</p>
<pre><code>    self.classifier = nn.Linear(64, 1)
    
def forward(self, x):
    x = self.conv1(x)
    x = F.relu(x)
    
    x = self.conv2(x)
    x = F.relu(x)
    
    x = self.conv3(x)
    x = F.relu(x)
    
    x = torch.mean(x, dim=2)       
    x = self.classifier(x)
    
    return x
</code></pre>
<h1><a name="train-model-5" class="anchor" href="#train-model-5"></a>Train model</h1>
<p>dataset = AudioDataset(background_files, speech_files)<br>
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)</p>
<p>model = AudioClassifier()<br>
criterion = nn.BCELoss()<br>
optimizer = torch.optim.Adam(model.parameters())</p>
<p>num_epochs = 10<br>
for epoch in range(num_epochs):<br>
for i, (inputs, labels) in enumerate(dataloader):</p>
<pre><code>    # Forward pass and loss
    outputs = model(inputs)
    loss = criterion(outputs, labels.unsqueeze(1).float())
    
    # Backward pass and update
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')   
</code></pre>
<p>print(‘Finished training’)</p>

username: akt42
cooked: <p>Some comments:</p>
<ol>
<li><code>audio.squeeze(0)</code> in <code>__getitem__()</code>: If you have a stereo audio file, <code>audio</code> will have a dimension of <code>[2, length]</code>. When you call <code>audio.squeeze(0)</code>, it will still return a  <code>[2, length]</code> tensor. Perhaps you need to use some approach that does stereo to mono conversion here.</li>
<li>Usage of <code>len(audio)</code> in <code>pad_truncate()</code>: In your case this will either return 1 or 2 because it will return the first dimension of the tensor. Maybe what you wanted was <code>audio.shape[-1]</code>?</li>
</ol>
<p>Note: point (2) can be ignored if you set the <code>channels_first=False</code> in <a href="https://pytorch.org/audio/stable/generated/torchaudio.load.html#torchaudio-load" rel="noopener nofollow ugc">torchaudio.load()</a></p>

username: Isaiah_addie
cooked: <p>I made this change to my get item:<br>
<strong>getitem</strong>(self, index):<br>
if index &lt; len(self.background_files):<br>
audio, sample_rate = torchaudio.load(self.background_files[index])<br>
audio = torch.mean(audio, dim=0, keepdim=True)<br>
audio = audio.squeeze(0)<br>
audio = pad_truncate(audio)<br>
print(len(audio))<br>
label = 0<br>
else:<br>
audio, sample_rate = torchaudio.load(self.speech_files[index-len(self.background_files)])<br>
audio = torch.mean(audio, dim=0, keepdim=False)<br>
audio = pad_truncate(audio)<br>
label = 1<br>
return audio, label</p>
<p>but unfortunately its now saying that its expecting a 3d or 4d input but got 2d or “[2, 16000]”</p>

username: Isaiah_addie
cooked: <p>for your 2nd comment Im a little confused as for the direction you think I should go?</p>

username: akt42
cooked: <p><code>[2, 16000]</code> means you have a two-channel (stereo) audio array. Maybe you need to do the mono conversion to the <code>background_files</code> as well.</p>
<p>Please post the code and the compete error trace when referring errors. You can surround each of them by three backticks to format code and errors so it is more readable with proper indentation.</p>
<p>Eg:</p>
<p><code>``` &lt;CODE&gt; ```</code></p>