{"record":{"id":"0843636d55f3a5df","repo":"open-mmlab/mmdetection","slug":"detr-do-not-build-sampler","errorCode":null,"errorMessage":"DETR do not build sampler.","messagePattern":"DETR do not build sampler\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"mmdet/models/dense_heads/detr_head.py","lineNumber":105,"sourceCode":"            bg_cls_weight = loss_cls.get('bg_cls_weight', class_weight)\n            assert isinstance(bg_cls_weight, float), 'Expected ' \\\n                'bg_cls_weight to have type float. Found ' \\\n                f'{type(bg_cls_weight)}.'\n            class_weight = torch.ones(num_classes + 1) * class_weight\n            # set background class as the last indice\n            class_weight[num_classes] = bg_cls_weight\n            loss_cls.update({'class_weight': class_weight})\n            if 'bg_cls_weight' in loss_cls:\n                loss_cls.pop('bg_cls_weight')\n            self.bg_cls_weight = bg_cls_weight\n\n        if train_cfg:\n            assert 'assigner' in train_cfg, 'assigner should be provided ' \\\n                                            'when train_cfg is set.'\n            assigner = train_cfg['assigner']\n            self.assigner = TASK_UTILS.build(assigner)\n            if train_cfg.get('sampler', None) is not None:\n                raise RuntimeError('DETR do not build sampler.')\n        self.num_classes = num_classes\n        self.embed_dims = embed_dims\n        self.num_reg_fcs = num_reg_fcs\n        self.train_cfg = train_cfg\n        self.test_cfg = test_cfg\n        self.loss_cls = MODELS.build(loss_cls)\n        self.loss_bbox = MODELS.build(loss_bbox)\n        self.loss_iou = MODELS.build(loss_iou)\n\n        if self.loss_cls.use_sigmoid:\n            self.cls_out_channels = num_classes\n        else:\n            self.cls_out_channels = num_classes + 1\n\n        self._init_layers()\n\n    def _init_layers(self) -> None:\n        \"\"\"Initialize layers of the transformer head.\"\"\"","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/dense_heads/detr_head.py#L87-L123","documentation":"DETRHead.__init__ raises RuntimeError if train_cfg contains a 'sampler' entry. DETR uses hungarian (one-to-one) bipartite matching via the assigner only; a positive/negative sampler is meaningless and its presence indicates a misconfigured head.","triggerScenarios":"Copying a train_cfg with a RandomSampler/PseudoSampler block (from an anchor-based head like RetinaNet/ATSS) into a DETR config; leftover sampler key after converting a config to DETR.","commonSituations":"Reusing RPN or RCNN head train_cfg in DETR-style heads; editing configs from anchor-based models.","solutions":["Remove the 'sampler' key (or set it to None) from train_cfg for DETR heads","Keep only 'assigner' (HungarianAssigner) in train_cfg"],"exampleFix":"// before\ntrain_cfg=dict(assigner=dict(type='HungarianAssigner'), sampler=dict(type='PseudoSampler'))\n// after\ntrain_cfg=dict(assigner=dict(type='HungarianAssigner'), sampler=None)","handlingStrategy":"validation","validationCode":"if train_cfg:\\n    assert train_cfg.get('sampler') is None, 'DETR heads must not define a sampler'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use minimal train_cfg (assigner only) for DETR-family heads","Don't copy train_cfg from anchor-based heads"],"tags":["mmdet","detr","train-cfg","sampler","config"],"backgroundTag":"invalid-config-value","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}