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

Accuracy metric computing doesn't ignore "ignore index" #1209

Closed
jiehuang165 opened this issue Jan 15, 2022 · 5 comments
Closed

Accuracy metric computing doesn't ignore "ignore index" #1209

jiehuang165 opened this issue Jan 15, 2022 · 5 comments

Comments

@jiehuang165
Copy link

Describe the bug
Shouldn't "ignore_index" be ignored when computing accuracy?

Model training will calculate "acc_seg" when computing loss.

loss['acc_seg'] = accuracy(seg_logit, seg_label)

It calls accuracy funciton
def accuracy(pred, target, topk=1, thresh=None):

and the accuracy metric is calculated as
res.append(correct_k.mul_(100.0 / target.numel()))

However, the denominator target.numel() includes areas with ignore_index. I mean accuracy=(num of correct) / (all num without ignore). Ignore samples should not present in both numerator and denominator, right? The above implementation causes the accuracy metric to be lower than expected. I think maybe the correct codes are:

loss['acc_seg'] = accuracy(seg_logit, seg_label, ignore_index=self.ignore_index)

def accuracy(pred, target, topk=1, thresh=None, ignore_index=-100):
    ...
    res.append(correct_k.mul_(100.0 / target[target != ignore_index].numel()))
    ...
@HJoonKwon
Copy link
Contributor

HJoonKwon commented Jan 28, 2022

@jiehuang165 I have the same issue and agree with you. In the function 'losses' in the decode_head.py, other losses like loss_decode receives the ignore_index as input, but accuracy does not.

        if not isinstance(self.loss_decode, nn.ModuleList):
            losses_decode = [self.loss_decode]
        else:
            losses_decode = self.loss_decode
        for loss_decode in losses_decode:
            if loss_decode.loss_name not in loss:
                loss[loss_decode.loss_name] = loss_decode(
                    seg_logit,
                    seg_label,
                    weight=seg_weight,
                    ignore_index=self.ignore_index)
            else:
                loss[loss_decode.loss_name] += loss_decode(
                    seg_logit,
                    seg_label,
                    weight=seg_weight,
                    ignore_index=self.ignore_index)

        loss['acc_seg'] = accuracy(seg_logit, seg_label)
        return loss

@muyuuuu
Copy link

muyuuuu commented Mar 5, 2022

I have puzzled by this problem...

  File "/home/20031211375/DFC/mmsegmentation/mmseg/models/losses/accuracy.py", line 52, in accuracy
    correct_k.mul_(100.0 / target[target != ignore_index].numel()))
ZeroDivisionError: float division by zero

@HJoonKwon
Copy link
Contributor

@muyuuuu #1336 proposes a solution to this problem

@muyuuuu
Copy link

muyuuuu commented Mar 6, 2022

thank you.

@MeowZheng
Copy link
Collaborator

solved

wjkim81 pushed a commit to wjkim81/mmsegmentation that referenced this issue Dec 3, 2023
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

4 participants