open-mmlab/mmdetection · error · ValueError
Please choose at least one loss in triplet loss and cross en
Error message
Please choose at least one loss in triplet loss and cross entropy loss.
What it means
LinearReIDHead needs at least one training loss. If loss_cls is None and loss_triplet is also None, __init__ raises ValueError because the head would have no objective to optimize.
Source
Thrown at mmdet/models/reid/linear_reid_head.py:76
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
self._init_layers()
View on GitHub (pinned to cfd5d3a985)
Solutions
- Add loss_triplet (e.g. dict(type='TripletLoss', margin=0.3, hard_mining=True)) if you only want metric learning
- Or add loss_cls=dict(type='CrossEntropyLoss', loss_weight=1.0) for classification-only training
- Or keep both for combined ReID training
Example fix
# before
head=dict(type='LinearReIDHead', num_classes=751)
# after
head=dict(type='LinearReIDHead', num_classes=751,
loss_triplet=dict(type='TripletLoss', margin=0.3, hard_mining=True)) Defensive patterns
Strategy: validation
Validate before calling
assert head_cfg.get('loss_cls') is not None or head_cfg.get('loss_triplet') is not None, 'reid head needs at least one loss' Prevention
- Always define loss_cls or loss_triplet in LinearReIDHead configs
- Prefer explicit losses over relying on defaults
When it happens
Trigger: Configuring the reid head with loss_cls=None and loss_triplet=None, or omitting both loss keys when they were expected to default; e.g. head=dict(type='LinearReIDHead', num_classes=..., ) with losses removed.
Common situations: Trimming loss configs to speed up training; migration from configs where loss defaults existed; disabling cross-entropy and forgetting to add loss_triplet.
Related errors
- The num_classes must be a current number, if there is cross
- LoadImageFromFile is not found in the test pipeline
- Visualization needs the "visualizer" termdefined in the conf
- config must be a filename or Config object, but got {type(co
- Unrecognized dataset: {dataset}
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/20de2f8be6f2fabb.
Report an issue: GitHub.