# ValueError: Expected more than 1 value per channel when training, got input size torch.Size(\[1, 256, 1, 1\])

**URL:** <https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408>\
**Category:** Uncategorized\
**Created:** [June 4, 2023, 11:56pm UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408 "2023-06-04T23:56:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 4, 2023, 11:56pm UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/1 "2023-06-04T23:56:16Z")

</div>

I have a four gpu setup (24 GB each) where I am trying to train a DeepLabV3Plus model using [segmentation\_models\_pytorch](https://github.com/qubvel/segmentation_models.pytorch) library.   
  
I am facing these errors:  
`ValueError: Caught ValueError in replica 0 on device 0.`  
`ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 256, 1, 1])`

The batch size I am using is 8.  
Can you please help me resolve this issue?  
Thank you!

---

<div class="post-metadata">

**Author:** ![eqy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/32/46233_2.png) [@eqy](https://discuss.pytorch.org/u/eqy)\
**Post date:** [June 4, 2023, 11:57pm UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/2 "2023-06-04T23:57:39Z")

</div>

It looks like you are passing a grayscale image (single channel) when the model expects multiple channels.

Are you adjusting for this property when creating model e.g., via the `in_channels` argument?

```auto
model = smp.Unet(
...
    in_channels=1, # model input channels (1 for gray-scale images, 3 for RGB, etc.)
...
)

```

---

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 4, 2023, 11:59pm UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/3 "2023-06-04T23:59:16Z")

</div>

Actually, I am using all RGB images.

---

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 5, 2023, 12:00am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/4 "2023-06-05T00:00:43Z")

</div>

It works with the default settings. Now there is a pool of encoders among which we can select which one to use. I am experimenting with different encoders. For some it works, for some it doesn’t.

Thank you for replying so quickly!  
Really appreciate it.

---

<div class="post-metadata">

**Author:** ![eqy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/32/46233_2.png) [@eqy](https://discuss.pytorch.org/u/eqy)\
**Post date:** [June 5, 2023, 12:02am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/5 "2023-06-05T00:02:38Z")

</div>

Right, I misread the original error. It looks like your “encoder” might be downsampling the input too much. Could you check if e.g., downsampling is a setting that is available to you or workaround the issue by increasing the input size so that the spatial dimensions are not reduced to 1x1?

---

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 5, 2023, 12:05am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/6 "2023-06-05T00:05:42Z")

</div>

[These](https://smp.readthedocs.io/en/latest/models.html#id9) are all the available settings.  
And currently I am using everything default except for the `encoder_name`.

---

<div class="post-metadata">

**Author:** ![eqy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/32/46233_2.png) [@eqy](https://discuss.pytorch.org/u/eqy)\
**Post date:** [June 5, 2023, 12:07am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/7 "2023-06-05T00:07:08Z")

</div>

Right, so you might look into:  
decreasing `encoder_output_stride`, increasing `upsampling`, or increasing the resolution of the input image as possible solutions.

---

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 5, 2023, 12:08am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/8 "2023-06-05T00:08:04Z")

</div>

Okay. Thank you for the tips. I am gonna try these out, and will update on this thread!  
Thank you so much!

---

<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 5, 2023, 2:46am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/9 "2023-06-05T02:46:47Z")

</div>

@eqy already explained why the error might be raised, however it still doesn’t fit your description:

> I have a four gpu setup (24 GB each) where I am trying to train a DeepLabV3Plus  
> `got input size torch.Size([1, 256, 1, 1])`

I don’t know of you are using data parallel (I would assume so), which should yield a batch size of 2 for each of the 4 GPUs assuming the global batch size is 8. If the local batch size is set to 8 then of course each GPU should get 8 samples while the error indicates a single sample.

---

<div class="post-metadata">

**Author:** ![Dhruv](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dhruv/32/40249_2.png) [@Dhruv](https://discuss.pytorch.org/u/Dhruv)\
**Post date:** [June 5, 2023, 3:05am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/10 "2023-06-05T03:05:16Z")

</div>

Yes, I am using `DataParallel`.

---

<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 5, 2023, 7:41am UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/11 "2023-06-05T07:41:29Z")

</div>

Which would mean that each of the four GPUs should process 2 samples for a batch size of 8. Could you add `print` statements to the ` forward` method and post the shape of the input as well as all activation tensors? I guess you might either use an invalid reshaping operation in the `forward` or your batch size is not 8.

---

<div class="post-metadata">

**Author:** ![QingCheng24](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/qingcheng24/32/66040_2.png) [@QingCheng24](https://discuss.pytorch.org/u/QingCheng24)\
**Post date:** [December 5, 2023, 11:41pm UTC](https://discuss.pytorch.org/t/valueerror-expected-more-than-1-value-per-channel-when-training-got-input-size-torch-size-1-256-1-1/181408/12 "2023-12-05T23:41:36Z")

</div>

I had the same issue which was solved by passing `drop_last = True` to the Dataloader.  
The issue occurs when the last batch accidentally holds 1 sample.
