{"record":{"id":"93e8fdc2a7c41413","repo":"facebookresearch/detectron2","slug":"bbox-has-to-be-1-dimensional-got-shape-bbox-shap","errorCode":null,"errorMessage":"bbox has to be 1-dimensional. Got shape={bbox.shape}.","messagePattern":"bbox has to be 1-dimensional\\. Got shape=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"detectron2/data/datasets/coco.py","lineNumber":368,"sourceCode":"    for image_id, image_dict in enumerate(dataset_dicts):\n        coco_image = {\n            \"id\": image_dict.get(\"image_id\", image_id),\n            \"width\": int(image_dict[\"width\"]),\n            \"height\": int(image_dict[\"height\"]),\n            \"file_name\": str(image_dict[\"file_name\"]),\n        }\n        coco_images.append(coco_image)\n\n        anns_per_image = image_dict.get(\"annotations\", [])\n        for annotation in anns_per_image:\n            # create a new dict with only COCO fields\n            coco_annotation = {}\n\n            # COCO requirement: XYWH box format for axis-align and XYWHA for rotated\n            bbox = annotation[\"bbox\"]\n            if isinstance(bbox, np.ndarray):\n                if bbox.ndim != 1:\n                    raise ValueError(f\"bbox has to be 1-dimensional. Got shape={bbox.shape}.\")\n                bbox = bbox.tolist()\n            if len(bbox) not in [4, 5]:\n                raise ValueError(f\"bbox has to has length 4 or 5. Got {bbox}.\")\n            from_bbox_mode = annotation[\"bbox_mode\"]\n            to_bbox_mode = BoxMode.XYWH_ABS if len(bbox) == 4 else BoxMode.XYWHA_ABS\n            bbox = BoxMode.convert(bbox, from_bbox_mode, to_bbox_mode)\n\n            # COCO requirement: instance area\n            if \"segmentation\" in annotation:\n                # Computing areas for instances by counting the pixels\n                segmentation = annotation[\"segmentation\"]\n                # TODO: check segmentation type: RLE, BinaryMask or Polygon\n                if isinstance(segmentation, list):\n                    polygons = PolygonMasks([segmentation])\n                    area = polygons.area()[0].item()\n                elif isinstance(segmentation, dict):  # RLE\n                    area = mask_util.area(segmentation).item()\n                else:","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/data/datasets/coco.py#L350-L386","documentation":"convert_to_coco_dict requires each annotation['bbox'] to be a 1-D array. If bbox is an np.ndarray with ndim != 1 (e.g. shape (1,4) or (N,4)), it cannot be interpreted as a single box and a ValueError is raised.","triggerScenarios":"Exporting predictions via convert_to_coco_json when Instances.pred_boxes.tensor rows (or user-built annotation dicts) are stored with an extra leading dimension, e.g. bbox = np.array([[x,y,w,h]]) instead of [x,y,w,h].","commonSituations":"Looping over batches/Instances without squeezing; stacking boxes into 2-D arrays and assigning them as single annotation bboxes; custom evaluators constructing annotation dicts from numpy arrays.","solutions":["Squeeze/flatten the box before assignment: bbox = np.asarray(box).reshape(-1) or box.squeeze(0)","Emit one annotation dict per box row when iterating a (N,4) tensor","Ensure lists are passed instead of nested arrays"],"exampleFix":"# before\nann['bbox'] = boxes.tensor.numpy()[:1]  # shape (1,4)\n# after\nann['bbox'] = boxes.tensor.numpy()[0]  # shape (4,)","handlingStrategy":"type-guard","validationCode":"import numpy as np\nbbox = np.asarray(annotation['bbox'])\nassert bbox.ndim == 1, f'bbox must be 1-D, got {bbox.shape}'","typeGuard":"def is_flat_bbox(b) -> bool:\n    import numpy as np\n    return not isinstance(b, np.ndarray) or b.ndim == 1","tryCatchPattern":null,"preventionTips":["Flatten/squeeze boxes before assigning to annotation dicts","Iterate per-row when exporting batches of pred_boxes","Prefer plain Python lists for bbox fields"],"tags":["detectron2","coco-export","numpy","bbox-shape"],"backgroundTag":"array-shape-mismatch","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}