keras-team/keras · error · NotImplementedError

`labels` property method has not been implemented in {}.

Error message

`labels` property method has not been implemented in {}.

What it means

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.

Source

Thrown at keras/src/legacy/preprocessing/image.py:382

        else:
            return batch_x
        if self.sample_weight is None:
            return batch_x, batch_y
        else:
            return batch_x, batch_y, self.sample_weight[index_array]

    @property
    def filepaths(self):
        """List of absolute paths to image files."""
        raise NotImplementedError(
            "`filepaths` property method has not "
            "been implemented in {}.".format(type(self).__name__)
        )

    @property
    def labels(self):
        """Class labels of every observation."""
        raise NotImplementedError(
            "`labels` property method has not been implemented in {}.".format(
                type(self).__name__
            )
        )

    @property
    def sample_weight(self):
        raise NotImplementedError(
            "`sample_weight` property method has not "
            "been implemented in {}.".format(type(self).__name__)
        )


@keras_export("keras._legacy.preprocessing.image.DirectoryIterator")
class DirectoryIterator(BatchFromFilesMixin, Iterator):
    """Iterator capable of reading images from a directory on disk.

    DEPRECATED.

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Keep a reference to your original y array instead of reading iterator.labels for NumpyArrayIterator
  2. Use flow_from_directory or flow_from_dataframe, which implement labels
  3. In custom subclasses, implement labels returning np.array of targets

Example fix

# before
y_true = iterator.labels  # raises on NumpyArrayIterator

# after
try:
    y_true = iterator.labels
except NotImplementedError:
    y_true = y  # original array passed to flow()
Defensive patterns

Strategy: try-catch

Validate before calling

labels = y if isinstance(iterator, NumpyArrayIterator) else iterator.labels

Type guard

def get_labels(it, fallback_y):
    try:
        return it.labels
    except NotImplementedError:
        return fallback_y

Try / catch

try:
    y_true = iterator.labels
except NotImplementedError:
    y_true = y  # original array passed to flow()

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/51c945db2f5b7576. Report an issue: GitHub.