samleoqh / MSCG-Net

Multi-view Self-Constructing Graph Convolutional Networks with Adaptive Class Weighting Loss for Semantic Segmentation
MIT License
68 stars 28 forks source link

Expected object of scalar type Long but got scalar type Float for argument #3 'index' #7

Closed yunxingluoyun closed 4 years ago

yunxingluoyun commented 4 years ago

Encountered an error problem:

File "train_R50.py", line 285, in <module>
    main()
  File "train_R50.py", line 147, in main
    main_loss = criterion(outputs, labels)
  File "/data/wdh/.conda/envs/AI_studywdh/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/data/wdh/MSCG-Net-master/MSCG-Net-master/lib/loss/acw_loss.py", line 23, in forward
    one_hot_label, mask = self.encode_one_hot_label(pred, target)
  File "/data/wdh/MSCG-Net-master/MSCG-Net-master/lib/loss/acw_loss.py", line 70, in encode_one_hot_label
    one_hot_label.scatter_(0, target.unsqueeze(1),1)
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #3 'index'
def encode_one_hot_label(self, pred, target):
    print(pred.size(),target.size())
    one_hot_label = pred.detach() * 0
    # print(one_hot_label)
    if self.ignore_index is not None:
        mask = (target == self.ignore_index)
        target = target.clone()
        target[mask] = 0
        # print(tptarget.unsqueeze(1))
        one_hot_label.scatter_(0, target.unsqueeze(1),1) #error
        mask = mask.unsqueeze(1).expand_as(one_hot_label)
        one_hot_label[mask] = 0
        return one_hot_label, mask
    else:
        one_hot_label.scatter_(1, target.unsqueeze(1), 1)
        return one_hot_label, None

pred:torch.Size([10, 7, 512, 512])

target:torch.Size([10, 512, 512])

This problem has bothered me for several days. At first I thought it was a label problem, so I changed the rgb label to a single-channel label. But still this error still exists. May I ask what causes this?