PIDNet: Error if target consists of only background class

Hi, thank you for sharing your work. I tried to train the model on my custom dataset derived from CamVid and I’m getting error during calculating the loss function when target consists of only background class (255). The error appears on 73th line in utils/criterion.py.

pred = pred.gather(1, tmp_target.unsqueeze(1))
pred, ind = pred.contiguous().view(-1,)[mask].contiguous().sort()
min_value = pred[min(self.min_kept, pred.numel() - 1)] # --- here is the error because pred is an empty array and mask only consist of False values
threshold = max(min_value, self.thresh)

About this issue

Most upvoted comments

Hi, thank you for sharing your work. I tried to train the model on my custom dataset derived from CamVid and I’m getting error during calculating the loss function when target consists of only background class (255). The error appears on 73th line in utils/criterion.py.

pred = pred.gather(1, tmp_target.unsqueeze(1))
pred, ind = pred.contiguous().view(-1,)[mask].contiguous().sort()
min_value = pred[min(self.min_kept, pred.numel() - 1)] # --- here is the error because pred is an empty array and mask only consist of False values
threshold = max(min_value, self.thresh)

Jul

你好,请问这个错误找到解决方法了吗?我也遇到这个问题了,想请教一下

这个问题在难例挖掘ohem loss里几乎都会遇到。 此处你们出现此问题主要出现在这一行代码中:https://github.com/XuJiacong/PIDNet/blob/f0ac91cdea7bf0cb2077b65e960c5b98b9173b0f/utils/utils.py#L53 code:bd_label = torch.where(torch.sigmoid(outputs[-1][:,0,:,:])>0.8, labels, filler) 注意,此处的0.8为作者设置的难例阈值,ohem官方给的阈值为0.7。当bd_label中元素值全为1,即表示outputs的第三张张量(分割头d的输出)在0.8阈值情况下没有难例,这种情况下传输到ohem的forward中计算,就会出现pred为空的情况。

我的理解是,如果无难例情况,替换成将整个labels输入ohem loss中进行计算,改动如下:

try:
    bd_label = torch.where(torch.sigmoid(outputs[-1][:,0,:,:])>0.7, labels, filler)
    loss_sb = self.sem_loss([outputs[-2]], bd_label)
except:
    loss_sb = self.sem_loss([outputs[-2]], labels)

(此处阈值我使用的官方0.7)

I also noticed this error. My current workaround:

       if pred.numel() > 0:
            min_value = pred[min(self.min_kept, pred.numel() - 1)]
            threshold = max(min_value, self.thresh)
        else:
            threshold = self.thresh

For me, this is an infrequent error, so the workaround does not seem to affect training negatively. However, if you have many images with void/ignore pixels, it may not be the right solution.