{"record":{"id":"feebf46872d105bc","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"targets-pos-matched-idxs-mask-logits-cannot-be-n","errorCode":null,"errorMessage":"targets, pos_matched_idxs, mask_logits cannot be None when training","messagePattern":"targets, pos_matched_idxs, mask_logits cannot be None when training","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/mask_rcnn/network_files/roi_head.py","lineNumber":546,"sourceCode":"                # during training, only focus on positive boxes\n                num_images = len(proposals)\n                mask_proposals = []\n                pos_matched_idxs = []\n                for img_id in range(num_images):\n                    pos = torch.where(labels[img_id] > 0)[0]  # 寻找对应gt类别大于0，即正样本\n                    mask_proposals.append(proposals[img_id][pos])\n                    pos_matched_idxs.append(matched_idxs[img_id][pos])\n            else:\n                pos_matched_idxs = None\n\n            mask_features = self.mask_roi_pool(features, mask_proposals, image_shapes)\n            mask_features = self.mask_head(mask_features)\n            mask_logits = self.mask_predictor(mask_features)\n\n            loss_mask = {}\n            if self.training:\n                if targets is None or pos_matched_idxs is None or mask_logits is None:\n                    raise ValueError(\"targets, pos_matched_idxs, mask_logits cannot be None when training\")\n\n                gt_masks = [t[\"masks\"] for t in targets]\n                gt_labels = [t[\"labels\"] for t in targets]\n                rcnn_loss_mask = maskrcnn_loss(mask_logits, mask_proposals, gt_masks, gt_labels, pos_matched_idxs)\n                loss_mask = {\"loss_mask\": rcnn_loss_mask}\n            else:\n                labels = [r[\"labels\"] for r in result]\n                mask_probs = maskrcnn_inference(mask_logits, labels)\n                for mask_prob, r in zip(mask_probs, result):\n                    r[\"masks\"] = mask_prob\n\n            losses.update(loss_mask)\n\n        return result, losses\n","sourceCodeStart":528,"sourceCodeEnd":561,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/mask_rcnn/network_files/roi_head.py#L528-L561","documentation":"When computing the mask loss in training mode, RoIHeads.forward requires targets, pos_matched_idxs, and mask_logits to all be present; any None makes maskrcnn_loss impossible to compute. The guard raises ValueError naming the missing pieces before unpacking gt_masks/gt_labels.","triggerScenarios":"Invoking RoIHeads.forward with self.training=True but targets=None, pos_matched_idxs=None, or mask_logits=None — typically from a hand-rolled training loop that skips select_training_samples.","commonSituations":"Custom training pipelines calling roi_heads internals; forgetting to return/propagate pos_matched_idxs from proposal matching; targets dropped by the dataloader.","solutions":["Ensure targets (with 'masks' and 'labels') is passed in training mode","Propagate pos_matched_idxs from select_training_samples into the mask-head call","Verify mask_logits are produced before loss computation (mask_head/mask_predictor ran)","Run inference through model.eval() if targets are unavailable"],"exampleFix":"// before\nroi_heads.forward(images, detections, shapes, targets=None, pos_matched_idxs=None)\n// after\nproposals, matched_idxs, ..., pos_matched_idxs = roi_heads.select_training_samples(proposals, targets)\nroi_heads.forward(images, detections, shapes, targets=targets, pos_matched_idxs=pos_matched_idxs)","handlingStrategy":"validation","validationCode":"if roi_heads.training:\n    assert targets is not None and pos_matched_idxs is not None and mask_logits is not None\n    loss_mask = maskrcnn_loss(mask_logits, mask_proposals, [t['masks'] for t in targets], [t['labels'] for t in targets], pos_matched_idxs)","typeGuard":"def mask_loss_inputs_ok(targets, pos_matched_idxs, mask_logits):\n    return None not in (targets, pos_matched_idxs, mask_logits)","tryCatchPattern":"try:\n    result, losses = roi_heads(images, detections, shapes, targets, matched_idxs)\nexcept ValueError as e:\n    if 'mask_logits' in str(e): logger.error('mask head produced no logits; check has_mask() and inputs')\n    raise","preventionTips":["Keep targets with 'masks' and 'labels' keys in training","Thread pos_matched_idxs through your custom loop","Assert head inputs non-None before loss computation"],"tags":["python","mask-rcnn","training"],"backgroundTag":"missing-required-argument","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}