{"record":{"id":"874dbf4c8c442919","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"no-ground-truth-boxes-available-for-one-of-the-ima-874dbf","errorCode":null,"errorMessage":"No ground-truth boxes available for one of the images during training","messagePattern":"No ground-truth boxes available for one of the images during training","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/retinaNet/network_files/det_utils.py","lineNumber":316,"sourceCode":"        self.allow_low_quality_matches = allow_low_quality_matches\n\n    def __call__(self, match_quality_matrix):\n        \"\"\"\n        计算anchors与每个gtboxes匹配的iou最大值，并记录索引，\n        iou<low_threshold索引值为-1， low_threshold<=iou<high_threshold索引值为-2\n        Args:\n            match_quality_matrix (Tensor[float]): an MxN tensor, containing the\n            pairwise quality between M ground-truth elements and N predicted elements.\n\n        Returns:\n            matches (Tensor[int64]): an N tensor where N[i] is a matched gt in\n            [0, M - 1] or a negative value indicating that prediction i could not\n            be matched.\n        \"\"\"\n        if match_quality_matrix.numel() == 0:\n            # empty targets or proposals not supported during training\n            if match_quality_matrix.shape[0] == 0:\n                raise ValueError(\n                    \"No ground-truth boxes available for one of the images \"\n                    \"during training\")\n            else:\n                raise ValueError(\n                    \"No proposal boxes available for one of the images \"\n                    \"during training\")\n\n        # match_quality_matrix is M (gt) x N (predicted)\n        # Max over gt elements (dim 0) to find best gt candidate for each prediction\n        # M x N 的每一列代表一个anchors与所有gt的匹配iou值\n        # matched_vals代表每列的最大值，即每个anchors与所有gt匹配的最大iou值\n        # matches对应最大值所在的索引\n        matched_vals, matches = match_quality_matrix.max(dim=0)  # the dimension to reduce.\n        if self.allow_low_quality_matches:\n            all_matches = matches.clone()\n        else:\n            all_matches = None\n","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/retinaNet/network_files/det_utils.py#L298-L334","documentation":"Inside Matcher.__call__ (det_utils.py), if the IoU match-quality matrix is empty and its row count (number of ground-truth boxes) is 0, training cannot proceed: RetinaNet's assignment of anchors to targets requires at least one GT box per image. The library raises a ValueError distinguishing 'no ground-truth' from 'no proposals'.","triggerScenarios":"Training loop calls retinanet(images, targets) where some target['boxes'] has shape [0, 4] (empty annotation), so AnchorSampler/Matcher receives zero GT rows and match_quality_matrix.numel()==0 with shape[0]==0.","commonSituations":"VOC/COCO annotations where an image has objects but all are marked difficult/iscrowd and filtered out; label files generated incorrectly leaving boxes empty; negative-sample images (background only) included in training without handling; a bad dataset split including unlabeled images.","solutions":["Filter out samples with zero boxes from the training set (or replace boxes with a dummy and rely on classification ignore).","Fix the annotation pipeline: ensure difficult/iscrowd filtering does not drop every object for an image.","Verify targets after transforms: assert t['boxes'].shape[0] > 0 for every target before forwarding.","Remove pure-background images from train.txt / annotation list, since this implementation does not support them."],"exampleFix":"// before\nloss_dict = model(images, targets)\n// after\ntargets = [t for t in targets if t[\"boxes\"].shape[0] > 0]\nimages = images.tensors[[i for i, t in enumerate(targets)]]  # keep in sync\nloss_dict = model(images, targets)","handlingStrategy":"validation","validationCode":"for t in targets:\n    if t[\"boxes\"].shape[0] == 0:\n        raise SkipSample(\"target has no ground-truth boxes\")","typeGuard":"def has_gt_boxes(target: dict) -> bool:\n    import torch\n    boxes = target.get(\"boxes\")\n    return isinstance(boxes, torch.Tensor) and boxes.ndim == 2 and boxes.shape[0] > 0","tryCatchPattern":"try:\n    loss_dict = model(images, targets)\nexcept ValueError as e:\n    if \"No ground-truth boxes\" in str(e):\n        log.warning(\"skipping batch with empty GT\", exc_info=True)\n        continue  # in training loop\n    raise","preventionTips":["Filter background-only/unlabeled images from the training split.","Check that difficult/iscrowd filtering never empties an image's boxes.","Assert non-empty boxes in Dataset.__getitem__ or collate.","Log dataset statistics (images with 0 boxes) before training."],"tags":["pytorch","object-detection","training","empty-targets"],"backgroundTag":"empty-ground-truth-boxes","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}