open-mmlab/mmdetection · warning

Since cross entropy is not set, the num_classes will be igno

Error message

Since cross entropy is not set, the num_classes will be ignored.

What it means

Warning from LinearReIDHead.__init__: when loss_cls is None (no cross-entropy head), any integer num_classes passed in the config is meaningless and is ignored. The head needs at least one of loss_triplet or loss_cls; with only triplet loss there are no class logits, so num_classes plays no role.

Source

Thrown at mmdet/models/reid/linear_reid_head.py:73

                 topk: Union[int, Tuple[int]] = (1, ),
                 init_cfg: Union[dict, List[dict]] = dict(
                     type='Normal', layer='Linear', mean=0, std=0.01, bias=0)):
        if mmpretrain is None:
            raise RuntimeError('Please run "pip install openmim" and '
                               'run "mim install mmpretrain" to '
                               'install mmpretrain first.')
        super(LinearReIDHead, self).__init__(init_cfg=init_cfg)

        assert isinstance(topk, (int, tuple))
        if isinstance(topk, int):
            topk = (topk, )
        for _topk in topk:
            assert _topk > 0, 'Top-k should be larger than 0'
        self.topk = topk

        if loss_cls is None:
            if isinstance(num_classes, int):
                warnings.warn('Since cross entropy is not set, '
                              'the num_classes will be ignored.')
            if loss_triplet is None:
                raise ValueError('Please choose at least one loss in '
                                 'triplet loss and cross entropy loss.')
        elif not isinstance(num_classes, int):
            raise TypeError('The num_classes must be a current number, '
                            'if there is cross entropy loss.')
        self.loss_cls = MODELS.build(loss_cls) if loss_cls else None
        self.loss_triplet = MODELS.build(loss_triplet) \
            if loss_triplet else None

        self.num_fcs = num_fcs
        self.in_channels = in_channels
        self.fc_channels = fc_channels
        self.out_channels = out_channels
        self.norm_cfg = norm_cfg
        self.act_cfg = act_cfg
        self.num_classes = num_classes

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Remove num_classes from the head config when loss_cls is None
  2. Or set loss_cls=dict(type='CrossEntropyLoss', ...) if classification is actually wanted
  3. Keep at least one of loss_triplet / loss_cls to avoid the companion ValueError

Example fix

# before
num_classes=751, loss_cls=None, loss_triplet=dict(type='TripletLoss')
# after
loss_triplet=dict(type='TripletLoss')  # num_classes removed
Defensive patterns

Strategy: validation

Validate before calling

if reid_head_cfg.get('loss_cls') is None:
    assert 'num_classes' not in reid_head_cfg, 'num_classes ignored when loss_cls is None'

Prevention

When it happens

Trigger: Configuring a ReID head with loss_cls=None (or omitted) while still supplying num_classes=751 (or any int). Only triplet loss is used, so num_classes is dead config.

Common situations: Building triplet-only person-ReID models (e.g., on Market-1501 style datasets) and leaving num_classes from a CE-based template in the config.

Related errors


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