WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError

No ground-truth boxes available for one of the images during

Error message

No ground-truth boxes available for one of the images during training

What it means

Inside Matcher.__call__ (det_utils.py), when the IoU match_quality_matrix is empty, the code distinguishes two cases: zero rows (M=0, no ground-truth boxes) raises 'No ground-truth boxes available...'. Training the RPN/ROI heads requires at least one GT box per image to assign anchors/proposals to, so an image with no annotations is unsupported during training.

Source

Thrown at pytorch_object_detection/faster_rcnn/network_files/det_utils.py:317

        self.allow_low_quality_matches = allow_low_quality_matches

    def __call__(self, match_quality_matrix):
        """
        计算anchors与每个gtboxes匹配的iou最大值,并记录索引,
        iou<low_threshold索引值为-1, low_threshold<=iou<high_threshold索引值为-2
        Args:
            match_quality_matrix (Tensor[float]): an MxN tensor, containing the
            pairwise quality between M ground-truth elements and N predicted elements.

        Returns:
            matches (Tensor[int64]): an N tensor where N[i] is a matched gt in
            [0, M - 1] or a negative value indicating that prediction i could not
            be matched.
        """
        if match_quality_matrix.numel() == 0:
            # empty targets or proposals not supported during training
            if match_quality_matrix.shape[0] == 0:
                raise ValueError(
                    "No ground-truth boxes available for one of the images "
                    "during training")
            else:
                raise ValueError(
                    "No proposal boxes available for one of the images "
                    "during training")

        # match_quality_matrix is M (gt) x N (predicted)
        # Max over gt elements (dim 0) to find best gt candidate for each prediction
        # M x N 的每一列代表一个anchors与所有gt的匹配iou值
        # matched_vals代表每列的最大值,即每个anchors与所有gt匹配的最大iou值
        # matches对应最大值所在的索引
        matched_vals, matches = match_quality_matrix.max(dim=0)  # the dimension to reduce.
        if self.allow_low_quality_matches:
            all_matches = matches.clone()
        else:
            all_matches = None

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Remove background images (no positive GT boxes) from the training set / train.txt.
  2. Filter targets in the dataset __getitem__ and skip images with zero boxes.
  3. Sanity-check each target before batching: assert len(t['boxes']) > 0 for training samples.
  4. If using a collate_fn, drop empty-target images there.
  5. Patch the matcher to skip empty-GT images only if you understand the downstream loss impact.

Example fix

# before
targets = [dataset[i][1] for i in indices]
# after
targets = [t for t in (dataset[i][1] for i in indices) if t['boxes'].shape[0] > 0]
Defensive patterns

Strategy: validation

Validate before calling

for target in targets:
    assert target['boxes'].shape[0] > 0, "training image has no ground-truth boxes"

Type guard

def has_gt_boxes(target: dict) -> bool:
    boxes = target.get('boxes')
    return boxes is not None and boxes.ndim == 2 and boxes.shape[0] > 0

Try / catch

try:
    loss_dict = model(images, targets)
except ValueError as e:
    if 'ground-truth' in str(e):
        print('Dropping batch with empty GT boxes'); continue
    raise

Prevention

When it happens

Trigger: A training image whose target['boxes'] tensor is empty (shape [0,4]) is passed to the RPN or RoI heads while self.training is True.

Common situations: Datasets containing images with no annotated objects (background images), an over-aggressive filter dropping all boxes, or corrupted/empty annotations for some images.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/ac7bb61dc4045d7b. Report an issue: GitHub.