Bi-SRNet icon indicating copy to clipboard operation
Bi-SRNet copied to clipboard

RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [2, 512, 512, 3]

Open Moonquakes0 opened this issue 2 years ago • 10 comments

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算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]

Moonquakes0 avatar Mar 09 '22 11:03 Moonquakes0

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 avatar Mar 12 '22 13:03 ggsDing

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

ggsDing avatar Mar 12 '22 14:03 ggsDing

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

Moonquakes0 avatar Mar 13 '22 07:03 Moonquakes0

@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 avatar Mar 18 '22 07:03 Evan1127-Lin

@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 avatar Mar 18 '22 07:03 Evan1127-Lin

@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!

Evan1127-Lin avatar Mar 18 '22 09:03 Evan1127-Lin

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

DA-II avatar Mar 26 '22 16:03 DA-II

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

ggsDing avatar Apr 27 '22 13:04 ggsDing

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算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的标签。

Gchang9 avatar May 16 '22 09:05 Gchang9

您好,我尝试用您的代码训练的时候出现了如下问题。似乎是在计算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

ggsDing avatar May 16 '22 17:05 ggsDing