Pytorch-UNet icon indicating copy to clipboard operation
Pytorch-UNet copied to clipboard

RuntimeError

Open wxm123wxm opened this issue 2 years ago • 7 comments

How to resolve "RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [1, 640, 959, 3]" The details are as followed: Epoch 1/5: 0%| | 0/4580 [00:08<?, ?img/s] Traceback (most recent call last): File "/home/ubuntu/workplace/Pytorch_Unet/train.py", line 185, in train_net(net=net, File "/home/ubuntu/workplace/Pytorch_Unet/train.py", line 93, in train_net loss = criterion(masks_pred, true_masks)
File "/home/ubuntu/.conda/envs/gpu/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/ubuntu/.conda/envs/gpu/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 961, in forward return F.cross_entropy(input, target, weight=self.weight, File "/home/ubuntu/.conda/envs/gpu/lib/python3.8/site-packages/torch/nn/functional.py", line 2468, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "/home/ubuntu/.conda/envs/gpu/lib/python3.8/site-packages/torch/nn/functional.py", line 2266, in nll_loss ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [1, 640, 959, 3]

wxm123wxm avatar Apr 03 '22 02:04 wxm123wxm

#123 again (occurs to me too)

Gouvernathor avatar Apr 11 '22 14:04 Gouvernathor

Hi,targets of size: : [1, 640, 959, 3] should be [1, 640, 959].Input of 3 channels image should be 1 channel label like 0,1,2,3...

Virgilzz avatar Apr 13 '22 06:04 Virgilzz

I don't understand, which part of the code is supposed to do that ?

Gouvernathor avatar Apr 13 '22 12:04 Gouvernathor

the shape of your label is not matched. you should covert your current mask file into gray mode and get the shape like below: img size: (4000, 2248) img shape: (2248, 4000) which has only two channels.

zengjie617789 avatar Apr 14 '22 08:04 zengjie617789

I don't understand, which part of the code is supposed to do that ?

https://blog.csdn.net/weixin_57203027/article/details/124148410?spm=1001.2014.3001.5501 Use it to recreate your targets

Virgilzz avatar Apr 14 '22 10:04 Virgilzz

emmm, I think she will not understand chinese ...

zengjie617789 avatar Apr 15 '22 01:04 zengjie617789

No, I don't but I found the culprit code anyway. Thanks for your answers.

Gouvernathor avatar Apr 15 '22 02:04 Gouvernathor