open-mmlab/mmdetection · warning

Default ``avg_non_ignore`` is False, if you would like to ig

Error message

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

What it means

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.

Source

Thrown at mmdet/models/losses/cross_entropy_loss.py:240

                Defaults to None.
            ignore_index (int | None): The label index to be ignored.
                Defaults to None.
            loss_weight (float, optional): Weight of the loss. Defaults to 1.0.
            avg_non_ignore (bool): The flag decides to whether the loss is
                only averaged over non-ignored targets. Default: False.
        """
        super(CrossEntropyLoss, self).__init__()
        assert (use_sigmoid is False) or (use_mask is False)
        self.use_sigmoid = use_sigmoid
        self.use_mask = use_mask
        self.reduction = reduction
        self.loss_weight = loss_weight
        self.class_weight = class_weight
        self.ignore_index = ignore_index
        self.avg_non_ignore = avg_non_ignore
        if ((ignore_index is not None) and not self.avg_non_ignore
                and self.reduction == 'mean'):
            warnings.warn(
                '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``.')

        if self.use_sigmoid:
            self.cls_criterion = binary_cross_entropy
        elif self.use_mask:
            self.cls_criterion = mask_cross_entropy
        else:
            self.cls_criterion = cross_entropy

    def extra_repr(self):
        """Extra repr."""
        s = f'avg_non_ignore={self.avg_non_ignore}'
        return s

    def forward(self,

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Set avg_non_ignore=True in the loss config for PyTorch-consistent averaging over non-ignored targets
  2. Or accept the default and tune loss_weight accordingly

Example fix

# before
loss_cls=dict(type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)  # ignore_index set elsewhere
# after
loss_cls=dict(type='CrossEntropyLoss', use_sigmoid=True, avg_non_ignore=True, loss_weight=1.0)
Defensive patterns

Strategy: validation

Validate before calling

loss_cfg = dict(type='CrossEntropyLoss', use_sigmoid=True, avg_non_ignore=True, loss_weight=1.0)

Prevention

When it happens

Trigger: 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'.

Common situations: Segmentation/detection heads with ignore_index=255 for void labels; users surprised that ignored labels still shrink the mean loss.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/1fc1074d9939cc9a. Report an issue: GitHub.