{"record":{"id":"51c945db2f5b7576","repo":"keras-team/keras","slug":"labels-property-method-has-not-been-implemented","errorCode":null,"errorMessage":"`labels` property method has not been implemented in {}.","messagePattern":"`labels` property method has not been implemented in (.+?)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":382,"sourceCode":"        else:\n            return batch_x\n        if self.sample_weight is None:\n            return batch_x, batch_y\n        else:\n            return batch_x, batch_y, self.sample_weight[index_array]\n\n    @property\n    def filepaths(self):\n        \"\"\"List of absolute paths to image files.\"\"\"\n        raise NotImplementedError(\n            \"`filepaths` property method has not \"\n            \"been implemented in {}.\".format(type(self).__name__)\n        )\n\n    @property\n    def labels(self):\n        \"\"\"Class labels of every observation.\"\"\"\n        raise NotImplementedError(\n            \"`labels` property method has not been implemented in {}.\".format(\n                type(self).__name__\n            )\n        )\n\n    @property\n    def sample_weight(self):\n        raise NotImplementedError(\n            \"`sample_weight` property method has not \"\n            \"been implemented in {}.\".format(type(self).__name__)\n        )\n\n\n@keras_export(\"keras._legacy.preprocessing.image.DirectoryIterator\")\nclass DirectoryIterator(BatchFromFilesMixin, Iterator):\n    \"\"\"Iterator capable of reading images from a directory on disk.\n\n    DEPRECATED.","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L364-L400","documentation":"The base Iterator class exposes labels as an abstract property implemented only by concrete iterators that know their targets (DirectoryIterator, DataFrameIterator). Accessing it on NumpyArrayIterator or an unimplemented custom subclass raises NotImplementedError.","triggerScenarios":"Calling iterator.labels after flow(x, y) with in-memory arrays, or on a custom Iterator subclass without the override; common in metrics/reporting code wanting ground-truth labels for all samples.","commonSituations":"Evaluation or confusion-matrix scripts written against DirectoryIterator later run on numpy-backed iterators; subclassing Iterator without implementing the three abstract properties (labels, filepaths, sample_weight).","solutions":["Keep a reference to your original y array instead of reading iterator.labels for NumpyArrayIterator","Use flow_from_directory or flow_from_dataframe, which implement labels","In custom subclasses, implement labels returning np.array of targets"],"exampleFix":"# before\ny_true = iterator.labels  # raises on NumpyArrayIterator\n\n# after\ntry:\n    y_true = iterator.labels\nexcept NotImplementedError:\n    y_true = y  # original array passed to flow()","handlingStrategy":"try-catch","validationCode":"labels = y if isinstance(iterator, NumpyArrayIterator) else iterator.labels","typeGuard":"def get_labels(it, fallback_y):\n    try:\n        return it.labels\n    except NotImplementedError:\n        return fallback_y","tryCatchPattern":"try:\n    y_true = iterator.labels\nexcept NotImplementedError:\n    y_true = y  # original array passed to flow()","preventionTips":["Keep the original y array alongside numpy iterators","Implement all abstract properties when subclassing Iterator"],"tags":["keras","iterator","not-implemented","abstract-method"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}