{"record":{"id":"54c2eeb309523231","repo":"facebookresearch/detectron2","slug":"cannot-find-field-in-the-given-instances-54c2ee","errorCode":null,"errorMessage":"Cannot find field '{}' in the given Instances!","messagePattern":"Cannot find field '(.+?)' in the given Instances!","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"detectron2/structures/instances.py","lineNumber":66,"sourceCode":"            self.set(k, v)\n\n    @property\n    def image_size(self) -> Tuple[int, int]:\n        \"\"\"\n        Returns:\n            tuple: height, width\n        \"\"\"\n        return self._image_size\n\n    def __setattr__(self, name: str, val: Any) -> None:\n        if name.startswith(\"_\"):\n            super().__setattr__(name, val)\n        else:\n            self.set(name, val)\n\n    def __getattr__(self, name: str) -> Any:\n        if name == \"_fields\" or name not in self._fields:\n            raise AttributeError(\"Cannot find field '{}' in the given Instances!\".format(name))\n        return self._fields[name]\n\n    def set(self, name: str, value: Any) -> None:\n        \"\"\"\n        Set the field named `name` to `value`.\n        The length of `value` must be the number of instances,\n        and must agree with other existing fields in this object.\n        \"\"\"\n        with warnings.catch_warnings(record=True):\n            data_len = len(value)\n        if len(self._fields):\n            assert (\n                len(self) == data_len\n            ), \"Adding a field of length {} to a Instances of length {}\".format(data_len, len(self))\n        self._fields[name] = value\n\n    def has(self, name: str) -> bool:\n        \"\"\"","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/structures/instances.py#L48-L84","documentation":"Instances stores per-image fields in a dict; attribute access is forwarded to fields. Accessing an attribute that was never set (e.g. pred_masks before mask inference, gt_keypoints on a boxes-only dataset) raises AttributeError.","triggerScenarios":"Reading instances.pred_masks when the mask head was disabled (MODEL.ROI_MASK_HEAD disabled or MODEL.MASK_ON=False); accessing any field name not present in instances._fields.","commonSituations":"Custom evaluation loops assuming all outputs exist; enabling/disabling heads in config but not updating downstream code; accessing 'scores' vs 'score' naming differences across versions.","solutions":["Guard with hasattr(instance, 'pred_masks') or check available fields via instance.get_fields().keys()","Enable the relevant head: cfg.MODEL.MASK_ON = True / MODEL.KEYPOINT_ON = True","Check field name spelling against the head that produces it (pred_classes, scores, pred_masks, pred_keypoints)"],"exampleFix":"# before\nmasks = outputs['instances'].pred_masks\n# after\ninst = outputs['instances']\nmasks = inst.pred_masks if hasattr(inst, 'pred_masks') else None","handlingStrategy":"type-guard","validationCode":"fields = instances.get_fields()\nassert 'pred_masks' in fields, f'available: {list(fields)}'","typeGuard":"def has_field(instances, name: str) -> bool:\n    return name in instances.get_fields()","tryCatchPattern":"try:\n    val = instances.pred_masks\nexcept AttributeError:\n    val = None  # head disabled or field absent","preventionTips":["Use hasattr/get_fields().keys() before reading optional outputs","Keep MODEL.MASK_ON/KEYPOINT_ON flags in sync with downstream consumers"],"tags":["detectron2","instances","attribute-error"],"backgroundTag":"missing-attribute-on-dynamic-object","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}