{"record":{"id":"67cea9d34eb50037","repo":"roboflow/supervision","slug":"the-sum-of-coco-rle-counts-must-match-the-image-ar","errorCode":null,"errorMessage":"The sum of COCO RLE counts must match the image area.","messagePattern":"The sum of COCO RLE counts must match the image area\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/compact_mask.py","lineNumber":831,"sourceCode":"                raise ValueError(\"Each RLE payload must contain 'size' and 'counts'.\")\n\n            try:\n                # COCO standard: size=[height, width] (h,w order per pycocotools spec)\n                rle_h, rle_w = rle[\"size\"]\n                rle_h = int(rle_h)\n                rle_w = int(rle_w)\n            except (TypeError, ValueError) as exc:\n                raise ValueError(\"RLE size must be [height, width].\") from exc\n\n            if (rle_h, rle_w) != (img_h, img_w):\n                raise ValueError(\n                    f\"RLE size {(rle_h, rle_w)} must match image_shape \"\n                    f\"{(img_h, img_w)}.\"\n                )\n\n            counts = _coco_rle_counts_to_array(rle[\"counts\"])\n            if int(np.sum(counts, dtype=np.int64)) != img_h * img_w:\n                raise ValueError(\n                    \"The sum of COCO RLE counts must match the image area.\"\n                )\n\n            x1, y1, x2, y2 = xyxy_arr[mask_idx]\n            x1i, y1i, x2i, y2i = int(x1), int(y1), int(x2), int(y2)\n            x1c = max(0, min(x1i, img_w - 1))\n            y1c = max(0, min(y1i, img_h - 1))\n\n            if (\n                x2i < x1i\n                or y2i < y1i\n                or x2i < 0\n                or y2i < 0\n                or x1i >= img_w\n                or y1i >= img_h\n            ):\n                crop_rles.append(np.array([1], dtype=np.int32))\n                crop_shapes_list.append((1, 1))","sourceCodeStart":813,"sourceCodeEnd":849,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/compact_mask.py#L813-L849","documentation":"Raised by CompactMask.from_coco_rle when the sum of the RLE counts does not equal height*width of image_shape. In COCO's uncompressed column-major RLE, the alternating run lengths must exactly cover the whole canvas; a sum mismatch means the counts were truncated, corrupted, or encoded for a different resolution.","triggerScenarios":"Passing counts=[0, 2, 2] for a 4x4 image (sum 4 != 16); dropping trailing runs; encoding on a differently sized canvas than image_shape claims; manually editing counts.","commonSituations":"Truncated JSON; custom encoders that stop at the last foreground run and omit the trailing background run; mixing annotations from a resized copy of the dataset.","solutions":["Regenerate counts from the full-image boolean mask with pycocotools.mask.encode — the output always sums to h*w.","If hand-building runs, append the final background run so the total equals h*w.","Check the annotation pipeline's resize step so encode size == image_shape."],"exampleFix":"# before — missing trailing run (sum 4 for 4x4=16)\ncounts = [0, 2, 2]\n\n# after\ncounts = [0, 2, 2, 12]  # runs sum to 16 == 4*4","handlingStrategy":"validation","validationCode":"import numpy as np\nh, w = image_shape\nfor r in rles:\n    total = int(np.sum(np.asarray(r[\"counts\"], dtype=np.int64)))\n    assert total == h * w, f\"counts sum {total} != area {h * w}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate counts exclusively via pycocotools.mask.encode — runs then always sum to h*w.","When hand-building runs, append the trailing background run to complete the canvas.","Round-trip test any custom encoder: encode(decode(x)) == x."],"tags":["coco","rle","compact-mask","corrupt-data","validation"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}