Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError #350

Closed
wxm123wxm opened this issue Apr 3, 2022 · 7 comments · May be fixed by #359
Closed

RuntimeError #350

wxm123wxm opened this issue Apr 3, 2022 · 7 comments · May be fixed by #359

Comments

@wxm123wxm
Copy link

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]

@Gouvernathor
Copy link
Contributor

#123 again (occurs to me too)

@Virgilzz
Copy link

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...

@Gouvernathor
Copy link
Contributor

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

@zengjie617789
Copy link

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.

@Virgilzz
Copy link

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

@zengjie617789
Copy link

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

@Gouvernathor
Copy link
Contributor

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

@milesial milesial closed this as completed Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants