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: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [2, 512, 512, 3] #1

Open
Moonquakes0 opened this issue Mar 9, 2022 · 10 comments

Comments

@Moonquakes0
Copy link

Moonquakes0 commented Mar 9, 2022

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算loss的时候target应该是三维,但是debug进去发现label却还是四维,不知道该如何解决呢

Traceback (most recent call last):
File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 238, in
main()
File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 65, in main
train(train_loader, net, criterion, optimizer, scheduler, args, val_loader)
File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 105, in train
loss_seg = criterion(outputs_A, labels_A) * 0.5 + criterion(outputs_B, labels_B) * 0.5
File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/data/yuanyifeng/Bi-SRNet-main/utils/loss.py", line 13, in forward
return self.nll_loss(F.log_softmax(inputs, dim=1), targets)
File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 213, in forward
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/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: : [2, 512, 512, 3]

@ggsDing
Copy link
Owner

ggsDing commented Mar 12, 2022

Please check if you have used my codes to load the data. Also please check if you have loaded the RGB labels or the index labels.

@ggsDing
Copy link
Owner

ggsDing commented Mar 12, 2022

If you prefer to use RGB labels, you can use the Colorls2Index function in RS_ST.py

@Moonquakes0
Copy link
Author

@ggsDing Thanks for your reply, I will try again.

@Evan1127-Lin
Copy link

@ggsDing Thanks for your reply, I will try again.

Hello, I also encounter the same problem as you met. Have you succeeded addressing this issue?

@Evan1127-Lin
Copy link

@ggsDing Excuse me, I have tried adding the following codes:
labels_A = RS.Colorls2Index(labels_A)
labels_B = RS.Colorls2Index(labels_B)
after the code part:
if args['gpu']:
imgs_A = imgs_A.cuda().float()
imgs_B = imgs_B.cuda().float()
labels_bn = (labels_A>0).unsqueeze(1).cuda().float()
labels_A = labels_A.cuda().long()
labels_B = labels_B.cuda().long()
in train_SCD.py. But another problem arises when running this file as shown below:
File "/home/lyz/code/supervised/Bi-SRNet/Bi-SRNet-main/train_SCD.py", line 100, in train
labels_A = RS.Colorls2Index(labels_A)
File "/home/lyz/code/supervised/Bi-SRNet/Bi-SRNet-main/datasets/RS_ST.py", line 33, in Colorls2Index
IndexMap = Color2Index(data)
File "/home/lyz/code/supervised/Bi-SRNet/Bi-SRNet-main/datasets/RS_ST.py", line 38, in Color2Index
data = ColorLabel.astype(np.int32)
AttributeError: 'Tensor' object has no attribute 'astype'
Could you please give me some specific help? Thank you in advance!

@Evan1127-Lin
Copy link

@ggsDing I have succeeded solving the issue mentioned above. The solution is modifying "RS_ST.py" file by adding 2 code lines at the last of function "read_RSimages":
labels_A = Colorls2Index(labels_A)
labels_B = Colorls2Index(labels_B)
Thank you all the same!

@DA-II
Copy link

DA-II commented Mar 26, 2022

之前粘贴错了位置 感谢Evan1127-Lin提出的方法 注意一定要粘贴到read_RSimages的尾部部不是read_RSimages 内 for循环尾部 Colorls2Index这个函数的作用是把rgb的labels(512,512,3)转化为以类为标签(512,512)型数据

@ggsDing
Copy link
Owner

ggsDing commented Apr 27, 2022

@ggsDing I have succeeded solving the issue mentioned above. The solution is modifying "RS_ST.py" file by adding 2 code lines at the last of function "read_RSimages": labels_A = Colorls2Index(labels_A) labels_B = Colorls2Index(labels_B) Thank you all the same!

Glad that you found a solution. I have added codes for rgb labels in the updated RS_ST.py lines 93-94.

@Gchang9
Copy link

Gchang9 commented May 16, 2022

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算loss的时候target应该是三维,但是debug进去发现label却还是四维,不知道该如何解决呢

Traceback (most recent call last): File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 238, in main() File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 65, in main train(train_loader, net, criterion, optimizer, scheduler, args, val_loader) File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 105, in train loss_seg = criterion(outputs_A, labels_A) * 0.5 + criterion(outputs_B, labels_B) * 0.5 File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/data/yuanyifeng/Bi-SRNet-main/utils/loss.py", line 13, in forward return self.nll_loss(F.log_softmax(inputs, dim=1), targets) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 213, in forward return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/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: : [2, 512, 512, 3]

我也遇到了这个问题,请问您解决了吗?如果解决了烦请赐教!我用的是不是RGB的标签。

@ggsDing
Copy link
Owner

ggsDing commented May 16, 2022

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算loss的时候target应该是三维,但是debug进去发现label却还是四维,不知道该如何解决呢
Traceback (most recent call last): File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 238, in main() File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 65, in main train(train_loader, net, criterion, optimizer, scheduler, args, val_loader) File "/data/yuanyifeng/Bi-SRNet-main/train_SCD.py", line 105, in train loss_seg = criterion(outputs_A, labels_A) * 0.5 + criterion(outputs_B, labels_B) * 0.5 File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/data/yuanyifeng/Bi-SRNet-main/utils/loss.py", line 13, in forward return self.nll_loss(F.log_softmax(inputs, dim=1), targets) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 213, in forward return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction) File "/home/yuanyifeng/anaconda3/envs/ASN3/lib/python3.7/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: : [2, 512, 512, 3]

我也遇到了这个问题,请问您解决了吗?如果解决了烦请赐教!我用的是不是RGB的标签。

For RGB labels please uncomment lines 93-94 in RS_ST.py

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

No branches or pull requests

5 participants