# RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: \[1, 1, 374, 402, 3\]

**URL:** <https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535>\
**Category:** Uncategorized\
**Created:** [June 19, 2022, 4:38pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535 "2022-06-19T16:38:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrTanvirHasan27](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jrtanvirhasan27/32/50678_2.png) [@jrTanvirHasan27](https://discuss.pytorch.org/u/jrTanvirHasan27)\
**Post date:** [June 19, 2022, 4:38pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/1 "2022-06-19T16:38:15Z")

</div>

![image](https://discuss.pytorch.org/uploads/default/original/3X/5/e/5e44b4e53dec9756376d3b9555113f75c7a3dc19.png)  
I am getting error in this line. I can’t understand why I am getting this.

---

<div class="post-metadata">

**Author:** ![jrTanvirHasan27](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jrtanvirhasan27/32/50678_2.png) [@jrTanvirHasan27](https://discuss.pytorch.org/u/jrTanvirHasan27)\
**Post date:** [June 19, 2022, 4:39pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/2 "2022-06-19T16:39:25Z")

</div>

![image](https://discuss.pytorch.org/uploads/default/original/3X/1/6/16ad695b0ac31956075532a09ec0128f7183dac9.png)  
Had to add in the comment section as new users can’t upload more than one media.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 20, 2022, 3:30am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/3 "2022-06-20T03:30:32Z")

</div>

`nn.Conv2d` expects an input in the shape `[batch_size, channels, height, width]` while you are passing a 5-dimensional input as given in the error message.  
Remove the unnecessary dimension and `permute` the input to have the aforementioned channels-first memory layout.

PS: you can post code snippets by wrapping them into three backticks ```.

---

<div class="post-metadata">

**Author:** ![Ajay\_krishan\_gairola](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajay_krishan_gairola/32/51613_2.png) [@Ajay\_krishan\_gairola](https://discuss.pytorch.org/u/Ajay_krishan_gairola)\
**Post date:** [May 3, 2023, 4:46am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/5 "2023-05-03T04:46:27Z")

</div>

@jrTanvirHasan27  
is your problem solved or not?  
i am facing the same error?

---

<div class="post-metadata">

**Author:** ![DackJempsey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dackjempsey/32/67594_2.png) [@DackJempsey](https://discuss.pytorch.org/u/DackJempsey)\
**Post date:** [February 11, 2024, 3:51am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/6 "2024-02-11T03:51:21Z")

</div>

You can either reduce the dimension as @ptrblck says, or if available for your use case use the conv3d instead of the 2d counterpart.

---

<div class="post-metadata">

**Author:** ![Giang\_Phong](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/giang_phong/32/66007_2.png) [@Giang\_Phong](https://discuss.pytorch.org/u/Giang_Phong)\
**Post date:** [March 13, 2024, 9:48pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/7 "2024-03-13T21:48:49Z")

</div>

You can use the .reshape() method to flatten the data. Specifically, if you have a tensor with dimensions [batch\_size, num\_frames, channels, height, width], you can use .reshape() to turn it into a tensor with dimensions [batch\_size \* num\_frames, channels, height, width]. This means all batch and frame sizes will be stacked together to form a single large batch. Result should be : ([1, 374, 402, 3])

---

<div class="post-metadata">

**Author:** ![fatema\_hammad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fatema_hammad/32/72477_2.png) [@fatema\_hammad](https://discuss.pytorch.org/u/fatema_hammad)\
**Post date:** [September 2, 2024, 12:14pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/8 "2024-09-02T12:14:37Z")

</div>

I have the same problem but I’m using 3D MRI images which are in nifti format  
the original dimension is [170, 195, 160], in which 170 is the number of slices.

I wrote a custom dataset class and a custom transform.

also I tracked the dimension they changes from ([1, 1, 170, 195, 160]) to ([1, 1, 1, 170, 195, 160]) before entering the model

how to solve this problem  
(I’m using 3D model with conv3d)

---

<div class="post-metadata">

**Author:** ![deepsthecoder](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/deepsthecoder/32/66316_2.png) [@deepsthecoder](https://discuss.pytorch.org/u/deepsthecoder)\
**Post date:** [September 2, 2024, 1:47pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/9 "2024-09-02T13:47:41Z")

</div>

Hey there,  
nn.Conv3d expects the input in the format (N,Cin,D,H,W) → 5 dim. Yours is a 6 dim input.

- you could reduce a dimension with `torch.squeeze()` the appropriate dim (1 or 2)
- Also, to double check if your inputs match with the expected input format for `nn.Conv3d`, otherwise permute it accordingly  
For more information: [Conv3d — PyTorch 2.4 documentation](https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html)

---

<div class="post-metadata">

**Author:** ![fatema\_hammad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fatema_hammad/32/72477_2.png) [@fatema\_hammad](https://discuss.pytorch.org/u/fatema_hammad)\
**Post date:** [September 3, 2024, 10:19am UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/10 "2024-09-03T10:19:47Z")

</div>

tahnks for quick replay, but I still have a problem.

I wrote this transform class

class ToTensorNIfTI:  
def **call** (self, img):  
img = np.array(img.get\_fdata(), dtype=np.float32) if hasattr(img, ‘get\_fdata’) else np.array(img, dtype=np.float32)

```
    #print(f"Original NIfTI image shape: {img.shape}")
    
    img -= img.min()
    img /= img.max()
    img = torch.tensor(img, dtype=torch.float32)
    #print(f"Tensor shape after ToTensorNIfTI: {img.shape}")

    # Add channel dimension
    if img.ndimension() == 3: # For 3D images
        img = img.unsqueeze(0) # Add channel dimension

    
    return img

```

so the error was as I said  
Expected 4D (unbatched) or 5D (batched) input to conv3d, but got input of size: [1, 1, 1, 160, 195, 170]

and when I removed this lines

# Add channel dimension

```
    if img.ndimension() == 3: # For 3D images
        img = img.unsqueeze(0) # Add channel dimension

```

the error became: expected 5D input (got 4D input)

Then I rewrote these lines.

# Add channel dimension

```
if img.ndimension() == 3: # For 3D images
    img = img.unsqueeze(0) # Add channel dimension

```

and used reshape()  
def train(model, dataloader, optimizer, criterion, device):  
model.train()  
running\_loss = 0.0  
corrects = 0  
total = 0

```
for inputs, labels in dataloader:
   # Print input shape before reshaping
   #print(f"Input shape before reshaping: {inputs.shape}")

   # Reshape the input tensor to [batch_size, channels, depth, height, width]
    # Remove the extra dimension (in this case, the dimension of size 1)
 if inputs.ndimension() == 6 and inputs.size(2) == 1:
  inputs = inputs.squeeze(2) # Remove the dimension with size 

```

but the error still  
expected 5D input (got 4D input)

so what should I do?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 3, 2024, 12:22pm UTC](https://discuss.pytorch.org/t/runtimeerror-expected-3d-unbatched-or-4d-batched-input-to-conv2d-but-got-input-of-size-1-1-374-402-3/154535/11 "2024-09-03T12:22:23Z")

</div>

It seems you cannot narrow down where exactly the image dimensions increase so could you post a minimal, formatted, and executable code snippet reproducing the issue?
