keras-team/keras · error · ValueError
Invalid class_mode: {}; expected one of: {}
Error message
Invalid class_mode: {}; expected one of: {} What it means
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.
Source
Thrown at keras/src/legacy/preprocessing/image.py:445
data_format = backend.image_data_format()
if dtype is None:
dtype = backend.floatx()
super().set_processing_attrs(
image_data_generator,
target_size,
color_mode,
data_format,
save_to_dir,
save_prefix,
save_format,
subset,
interpolation,
keep_aspect_ratio,
)
self.directory = directory
self.classes = classes
if class_mode not in self.allowed_class_modes:
raise ValueError(
"Invalid class_mode: {}; expected one of: {}".format(
class_mode, self.allowed_class_modes
)
)
self.class_mode = class_mode
self.dtype = dtype
# First, count the number of samples and classes.
self.samples = 0
if not classes:
classes = []
for subdir in sorted(os.listdir(directory)):
if os.path.isdir(os.path.join(directory, subdir)):
classes.append(subdir)
self.num_classes = len(classes)
self.class_indices = dict(zip(classes, range(len(classes))))
pool = multiprocessing.pool.ThreadPool()View on GitHub (pinned to 7a34a03db6)
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'
Example fix
# before it = gen.flow_from_directory(dir, class_mode='multiclass') # after it = gen.flow_from_directory(dir, class_mode='categorical')
Defensive patterns
Strategy: validation
Validate before calling
allowed = {'categorical', 'binary', 'sparse', 'input', 'other', None}
assert class_mode in allowed, f'bad class_mode: {class_mode}' Type guard
def is_class_mode(v) -> bool: return v in {'categorical', 'binary', 'sparse', 'input', 'other'} or v is None Try / catch
try:
it = gen.flow_from_directory(d, class_mode=class_mode)
except ValueError as e:
if 'Invalid class_mode' in str(e):
class_mode = {'one_hot': 'categorical', 'multiclass': 'sparse'}.get(class_mode, class_mode)
else:
raise Prevention
- Use None (object) for 'no labels', never the string 'none'
- sparse = integer labels, categorical = one-hot; pick deliberately per loss function
When it happens
Trigger: 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.
Common situations: Confusing sparse (integer labels) with categorical (one-hot), using class_mode='none' (string) where None is required, copy-pasting mode names from other APIs.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Invalid quantization mode. Expected one of {dtype_policies.Q
- Invalid value for argument `output_mode`. Expected one of {a
- `sparse` may only be true if `output_mode` is `"one_hot"`, `
- The `salt` argument for `Hashing` can only be a tuple of siz
- {self._VALUE_RANGE_VALIDATION_ERROR}Received: value_range={v
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e8c7c09bbd7302fb.
Report an issue: GitHub.