{"record":{"id":"1fc1074d9939cc9a","repo":"open-mmlab/mmdetection","slug":"default-avg-non-ignore-is-false-if-you-would","errorCode":null,"errorMessage":"Default ``avg_non_ignore`` is False, if you would like to ignore the certain label and average loss over non-ignore labels, which is the same with PyTorch official cross_entropy, set ``avg_non_ignore=True``.","messagePattern":"Default ``avg_non_ignore`` is False, if you would like to ignore the certain label and average loss over non-ignore labels, which is the same with PyTorch official cross_entropy, set ``avg_non_ignore=True``\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"mmdet/models/losses/cross_entropy_loss.py","lineNumber":240,"sourceCode":"                Defaults to None.\n            ignore_index (int | None): The label index to be ignored.\n                Defaults to None.\n            loss_weight (float, optional): Weight of the loss. Defaults to 1.0.\n            avg_non_ignore (bool): The flag decides to whether the loss is\n                only averaged over non-ignored targets. Default: False.\n        \"\"\"\n        super(CrossEntropyLoss, self).__init__()\n        assert (use_sigmoid is False) or (use_mask is False)\n        self.use_sigmoid = use_sigmoid\n        self.use_mask = use_mask\n        self.reduction = reduction\n        self.loss_weight = loss_weight\n        self.class_weight = class_weight\n        self.ignore_index = ignore_index\n        self.avg_non_ignore = avg_non_ignore\n        if ((ignore_index is not None) and not self.avg_non_ignore\n                and self.reduction == 'mean'):\n            warnings.warn(\n                'Default ``avg_non_ignore`` is False, if you would like to '\n                'ignore the certain label and average loss over non-ignore '\n                'labels, which is the same with PyTorch official '\n                'cross_entropy, set ``avg_non_ignore=True``.')\n\n        if self.use_sigmoid:\n            self.cls_criterion = binary_cross_entropy\n        elif self.use_mask:\n            self.cls_criterion = mask_cross_entropy\n        else:\n            self.cls_criterion = cross_entropy\n\n    def extra_repr(self):\n        \"\"\"Extra repr.\"\"\"\n        s = f'avg_non_ignore={self.avg_non_ignore}'\n        return s\n\n    def forward(self,","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/losses/cross_entropy_loss.py#L222-L258","documentation":"CrossEntropyLoss (sigmoid variant, use_sigmoid=True) warns that when ignore_index is set, avg_non_ignore defaults to False, meaning the loss averages over ALL positions (including ignored ones) rather than only non-ignored ones — differing from PyTorch's official cross_entropy behavior.","triggerScenarios":"Constructing CrossEntropyLoss with use_sigmoid=True (or hitting this class's init) where ignore_index is not None, avg_non_ignore=False, and reduction='mean'.","commonSituations":"Segmentation/detection heads with ignore_index=255 for void labels; users surprised that ignored labels still shrink the mean loss.","solutions":["Set avg_non_ignore=True in the loss config for PyTorch-consistent averaging over non-ignored targets","Or accept the default and tune loss_weight accordingly"],"exampleFix":"# before\nloss_cls=dict(type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)  # ignore_index set elsewhere\n# after\nloss_cls=dict(type='CrossEntropyLoss', use_sigmoid=True, avg_non_ignore=True, loss_weight=1.0)","handlingStrategy":"validation","validationCode":"loss_cfg = dict(type='CrossEntropyLoss', use_sigmoid=True, avg_non_ignore=True, loss_weight=1.0)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set avg_non_ignore=True whenever ignore_index is used","Compare loss values against torch.nn.functional.cross_entropy to verify reduction semantics"],"tags":["mmdet","loss","cross-entropy","ignore-index"],"backgroundTag":"loss-reduction-mismatch","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}