{"record":{"id":"20de2f8be6f2fabb","repo":"open-mmlab/mmdetection","slug":"please-choose-at-least-one-loss-in-triplet-loss-an","errorCode":null,"errorMessage":"Please choose at least one loss in triplet loss and cross entropy loss.","messagePattern":"Please choose at least one loss in triplet loss and cross entropy loss\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mmdet/models/reid/linear_reid_head.py","lineNumber":76,"sourceCode":"        if mmpretrain is None:\n            raise RuntimeError('Please run \"pip install openmim\" and '\n                               'run \"mim install mmpretrain\" to '\n                               'install mmpretrain first.')\n        super(LinearReIDHead, self).__init__(init_cfg=init_cfg)\n\n        assert isinstance(topk, (int, tuple))\n        if isinstance(topk, int):\n            topk = (topk, )\n        for _topk in topk:\n            assert _topk > 0, 'Top-k should be larger than 0'\n        self.topk = topk\n\n        if loss_cls is None:\n            if isinstance(num_classes, int):\n                warnings.warn('Since cross entropy is not set, '\n                              'the num_classes will be ignored.')\n            if loss_triplet is None:\n                raise ValueError('Please choose at least one loss in '\n                                 'triplet loss and cross entropy loss.')\n        elif not isinstance(num_classes, int):\n            raise TypeError('The num_classes must be a current number, '\n                            'if there is cross entropy loss.')\n        self.loss_cls = MODELS.build(loss_cls) if loss_cls else None\n        self.loss_triplet = MODELS.build(loss_triplet) \\\n            if loss_triplet else None\n\n        self.num_fcs = num_fcs\n        self.in_channels = in_channels\n        self.fc_channels = fc_channels\n        self.out_channels = out_channels\n        self.norm_cfg = norm_cfg\n        self.act_cfg = act_cfg\n        self.num_classes = num_classes\n\n        self._init_layers()\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/reid/linear_reid_head.py#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"Trimming loss configs to speed up training; migration from configs where loss defaults existed; disabling cross-entropy and forgetting to add loss_triplet.","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"],"exampleFix":"# before\nhead=dict(type='LinearReIDHead', num_classes=751)\n# after\nhead=dict(type='LinearReIDHead', num_classes=751,\n          loss_triplet=dict(type='TripletLoss', margin=0.3, hard_mining=True))","handlingStrategy":"validation","validationCode":"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'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always define loss_cls or loss_triplet in LinearReIDHead configs","Prefer explicit losses over relying on defaults"],"tags":["mmdetection","reid","loss","config"],"backgroundTag":"missing-required-config-field","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}