WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
targets, pos_matched_idxs, mask_logits cannot be None when t
Error message
targets, pos_matched_idxs, mask_logits cannot be None when training
What it means
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.
Source
Thrown at pytorch_object_detection/mask_rcnn/network_files/roi_head.py:546
# during training, only focus on positive boxes
num_images = len(proposals)
mask_proposals = []
pos_matched_idxs = []
for img_id in range(num_images):
pos = torch.where(labels[img_id] > 0)[0] # 寻找对应gt类别大于0,即正样本
mask_proposals.append(proposals[img_id][pos])
pos_matched_idxs.append(matched_idxs[img_id][pos])
else:
pos_matched_idxs = None
mask_features = self.mask_roi_pool(features, mask_proposals, image_shapes)
mask_features = self.mask_head(mask_features)
mask_logits = self.mask_predictor(mask_features)
loss_mask = {}
if self.training:
if targets is None or pos_matched_idxs is None or mask_logits is None:
raise ValueError("targets, pos_matched_idxs, mask_logits cannot be None when training")
gt_masks = [t["masks"] for t in targets]
gt_labels = [t["labels"] for t in targets]
rcnn_loss_mask = maskrcnn_loss(mask_logits, mask_proposals, gt_masks, gt_labels, pos_matched_idxs)
loss_mask = {"loss_mask": rcnn_loss_mask}
else:
labels = [r["labels"] for r in result]
mask_probs = maskrcnn_inference(mask_logits, labels)
for mask_prob, r in zip(mask_probs, result):
r["masks"] = mask_prob
losses.update(loss_mask)
return result, losses
View on GitHub (pinned to 1ec3fe6f37)
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
Example fix
// before roi_heads.forward(images, detections, shapes, targets=None, pos_matched_idxs=None) // after proposals, matched_idxs, ..., pos_matched_idxs = roi_heads.select_training_samples(proposals, targets) roi_heads.forward(images, detections, shapes, targets=targets, pos_matched_idxs=pos_matched_idxs)
Defensive patterns
Strategy: validation
Validate before calling
if roi_heads.training:
assert targets is not None and pos_matched_idxs is not None and mask_logits is not None
loss_mask = maskrcnn_loss(mask_logits, mask_proposals, [t['masks'] for t in targets], [t['labels'] for t in targets], pos_matched_idxs) Type guard
def mask_loss_inputs_ok(targets, pos_matched_idxs, mask_logits):
return None not in (targets, pos_matched_idxs, mask_logits) Try / catch
try:
result, losses = roi_heads(images, detections, shapes, targets, matched_idxs)
except ValueError as e:
if 'mask_logits' in str(e): logger.error('mask head produced no logits; check has_mask() and inputs')
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: Custom training pipelines calling roi_heads internals; forgetting to return/propagate pos_matched_idxs from proposal matching; targets dropped by the dataloader.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- target should not be None.
- if in training, matched_idxs should not be None
- num_classes should be None when mask_predictor is specified
- expected stages_repeats as list of 3 positive ints
- expected stages_out_channels as list of 5 positive ints
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/feebf46872d105bc.
Report an issue: GitHub.