{"record":{"id":"d4ea2f67598104a8","repo":"open-mmlab/mmdetection","slug":"the-num-classes-must-be-a-current-number-if-there","errorCode":null,"errorMessage":"The num_classes must be a current number, if there is cross entropy loss.","messagePattern":"The num_classes must be a current number, if there is cross entropy loss\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mmdet/models/reid/linear_reid_head.py","lineNumber":79,"sourceCode":"                               '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\n    def _init_layers(self):\n        \"\"\"Initialize fc layers.\"\"\"\n        self.fcs = nn.ModuleList()","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/reid/linear_reid_head.py#L61-L97","documentation":"When loss_cls (cross-entropy) is configured in LinearReIDHead, num_classes must be an int because the classification layer needs a fixed class count. Passing a non-int num_classes (or leaving a default like None) raises this TypeError.","triggerScenarios":"head=dict(type='LinearReIDHead', loss_cls=dict(type='CrossEntropyLoss'), num_classes=None) or num_classes as a string/tuple while cross-entropy is enabled.","commonSituations":"Copy-pasted ReID configs where num_classes was removed for triplet-only training but loss_cls kept; dataset reid_classes not injected into the head variable.","solutions":["Set num_classes to the integer number of tracklet identities (e.g. 751 for MOT17-half)","If training triplet-only, remove loss_cls and keep num_classes omitted (a warning notes it is ignored)"],"exampleFix":"# before\nhead=dict(type='LinearReIDHead', loss_cls=dict(type='CrossEntropyLoss'))\n# after\nhead=dict(type='LinearReIDHead', num_classes=751, loss_cls=dict(type='CrossEntropyLoss'))","handlingStrategy":"type-guard","validationCode":"if head_cfg.get('loss_cls') is not None:\n    assert isinstance(head_cfg.get('num_classes'), int), 'num_classes int required with loss_cls'","typeGuard":"def valid_reid_head_cfg(c: dict) -> bool: return c.get('loss_cls') is None or isinstance(c.get('num_classes'), int)","tryCatchPattern":null,"preventionTips":["Inject num_classes from dataset reid_classes when generating configs programmatically","Remove num_classes only when doing triplet-only training"],"tags":["mmdetection","reid","config","type-error"],"backgroundTag":"invalid-config-value","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}