RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 3, 3], but got 5-dimensional input of size [1, 1, 128, 128, 128] instead

username: Palash_Shah
cooked: <p>Using this UNet implementation: <a href="https://github.com/kilgore92/PyTorch-UNet" rel="nofollow noopener">https://github.com/kilgore92/PyTorch-UNet</a></p>
<p>With this instantiation of the UNet object:</p>
<pre><code class="lang-auto">model = UNet(n_channels=1,
             mode='3D',
             num_classes=1,
             use_pooling=True,
             )
</code></pre>
<p>Whenever I try running my training script with the dataLoader I get this error: <code>RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 3, 3], but got 5-dimensional input of size [1, 1, 128, 128, 128] instead</code></p>
<p>Here’s more information:</p>
<p>This is my training script, and it is erroring out at the <code>output = model.forward(images)</code>.</p>
<pre><code class="lang-auto">def train():
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    num_steps_train = len(train_loader)
    
    print(num_steps_train)
    
    for epoch in range(epochs):
        print(' - training - ')
        for i, (images, masks) in enumerate(train_loader):
            images = images.to(device)
            masks = masks.to(device)
            outputs = model.forward(images)
</code></pre>
<p>The size of images and masks is (1, 1, 128, 128) when I print it out. This is because in my <em>getitem</em>_ in the Dataset object, i’m using <code>image = torch.reshape(image, shape=(1, 128, 128, 128))</code> to reshape my image to the designated size. I tried changing that to image = torch.reshape(image, shape=(128, 128, 128)) and this doesn’t work either.</p>
<p>This is my train_loader:</p>
<pre><code class="lang-auto">train_loader = DataLoader(dataset=Dataset(partition['orig'], partition['segment']), 
                          batch_size = batch_size, shuffle = True)
</code></pre>
<p>partition is just a dictionary that has original and segmentation mask images in it.</p>

username: Nikronic
cooked: <aside class="quote no-group" data-username="Palash_Shah" data-post="1" data-topic="91146">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/palash_shah/48/27323_2.png" class="avatar"> Palash_Shah:</div>
<blockquote>
<p>The size of images and masks is (1, 1, 128, 128)</p>
</blockquote>
</aside>
<p>Hi,</p>
<p>If the size of images and masks are <code>[1, 1, 128, 128]</code> then is not your images 2D?<br>
Also, how can you do</p>
<aside class="quote no-group" data-username="Palash_Shah" data-post="1" data-topic="91146">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/palash_shah/48/27323_2.png" class="avatar"> Palash_Shah:</div>
<blockquote>
<p>image = torch.reshape(image, shape=(1, 128, 128, 128))</p>
</blockquote>
</aside>
<p>when you have only 128*128 elements?</p>
<p>Although error says inputs are <code>[1, 1, 128, 128, 128]</code> which still I cannot understand how your inputs of size <code>[1, 1, 128, 128]</code> became 3d.</p>
<p>Bests</p>

username: Palash_Shah
cooked: <p>fixed this. it was just an issue of my reshape.</p>