{"record":{"id":"e8c7c09bbd7302fb","repo":"keras-team/keras","slug":"invalid-class-mode-expected-one-of","errorCode":null,"errorMessage":"Invalid class_mode: {}; expected one of: {}","messagePattern":"Invalid class_mode: (.+?); expected one of: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":445,"sourceCode":"            data_format = backend.image_data_format()\n        if dtype is None:\n            dtype = backend.floatx()\n        super().set_processing_attrs(\n            image_data_generator,\n            target_size,\n            color_mode,\n            data_format,\n            save_to_dir,\n            save_prefix,\n            save_format,\n            subset,\n            interpolation,\n            keep_aspect_ratio,\n        )\n        self.directory = directory\n        self.classes = classes\n        if class_mode not in self.allowed_class_modes:\n            raise ValueError(\n                \"Invalid class_mode: {}; expected one of: {}\".format(\n                    class_mode, self.allowed_class_modes\n                )\n            )\n        self.class_mode = class_mode\n        self.dtype = dtype\n        # First, count the number of samples and classes.\n        self.samples = 0\n\n        if not classes:\n            classes = []\n            for subdir in sorted(os.listdir(directory)):\n                if os.path.isdir(os.path.join(directory, subdir)):\n                    classes.append(subdir)\n        self.num_classes = len(classes)\n        self.class_indices = dict(zip(classes, range(len(classes))))\n\n        pool = multiprocessing.pool.ThreadPool()","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L427-L463","documentation":"DirectoryIterator.__init__ validates class_mode against its allowed set (categorical, binary, sparse, input, other, None). class_mode decides the shape of returned labels, so an unrecognized string fails fast with this ValueError.","triggerScenarios":"flow_from_directory(..., class_mode='multiclass'), 'one_hot', 'binary ' with a trailing space, wrong casing, or the string 'none' instead of the Python object None.","commonSituations":"Confusing sparse (integer labels) with categorical (one-hot), using class_mode='none' (string) where None is required, copy-pasting mode names from other APIs.","solutions":["Use one of: 'categorical', 'binary', 'sparse', 'input', 'other', or None","For no labels use class_mode=None (Python None), not the string 'none'","For integer class indices use 'sparse'; one-hot uses 'categorical'"],"exampleFix":"# before\nit = gen.flow_from_directory(dir, class_mode='multiclass')\n\n# after\nit = gen.flow_from_directory(dir, class_mode='categorical')","handlingStrategy":"validation","validationCode":"allowed = {'categorical', 'binary', 'sparse', 'input', 'other', None}\nassert class_mode in allowed, f'bad class_mode: {class_mode}'","typeGuard":"def is_class_mode(v) -> bool: return v in {'categorical', 'binary', 'sparse', 'input', 'other'} or v is None","tryCatchPattern":"try:\n    it = gen.flow_from_directory(d, class_mode=class_mode)\nexcept ValueError as e:\n    if 'Invalid class_mode' in str(e):\n        class_mode = {'one_hot': 'categorical', 'multiclass': 'sparse'}.get(class_mode, class_mode)\n    else:\n        raise","preventionTips":["Use None (object) for 'no labels', never the string 'none'","sparse = integer labels, categorical = one-hot; pick deliberately per loss function"],"tags":["keras","directory-iterator","class-mode","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}