Keras
flow_from_directory
sub-directories
data preprocessing
machine learning

Keras flow_from_directory read only from selected sub-directories

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Keras flow_from_directory reads class folders under a root directory, but you can restrict ingestion to selected sub-directories by controlling class names explicitly. This is useful when datasets include extra folders such as temporary exports, deprecated classes, or holdout subsets that should not enter training.

Short troubleshooting answers often solve the immediate error but miss maintainability concerns such as reproducibility, observability, and rollback safety. A complete implementation should make assumptions explicit, validate edge cases, and produce diagnostics that are useful during incidents.

When adapting snippets, verify version compatibility, runtime environment, and operational limits before rollout. Small contextual differences, such as framework version, deployment topology, or data shape, can change behavior significantly.

Core Sections

1. Establish a minimal correct solution

Use the classes argument to whitelist exactly which class folders are loaded. Keras then maps labels only for those folders, preserving deterministic class index ordering based on the list you provide.

python
1from tensorflow.keras.preprocessing.image import ImageDataGenerator
2
3datagen = ImageDataGenerator(rescale=1.0 / 255)
4
5train_gen = datagen.flow_from_directory(
6    directory='data/train',
7    classes=['cat', 'dog', 'rabbit'],
8    target_size=(224, 224),
9    batch_size=32,
10    class_mode='categorical',
11    shuffle=True
12)
13
14print(train_gen.class_indices)

This baseline should stay intentionally simple so correctness is easy to verify. Once the minimal behavior is confirmed, extend it with error handling and performance considerations rather than starting with complex abstractions.

2. Harden for production requirements

When your folder structure is irregular, move to a dataframe-based pipeline. Build a file list from selected directories and use flow_from_dataframe so inclusion logic is fully explicit and version-controlled.

python
1import os
2import pandas as pd
3from tensorflow.keras.preprocessing.image import ImageDataGenerator
4
5rows = []
6for cls in ['cat', 'dog']:
7    cls_dir = os.path.join('data/train', cls)
8    for name in os.listdir(cls_dir):
9        rows.append({'filename': os.path.join(cls_dir, name), 'label': cls})
10
11df = pd.DataFrame(rows)
12
13gen = ImageDataGenerator(rescale=1.0 / 255).flow_from_dataframe(
14    dataframe=df,
15    x_col='filename',
16    y_col='label',
17    class_mode='categorical',
18    target_size=(224, 224)
19)

Production hardening usually includes explicit validation, clear failure semantics, and safe resource lifecycle management. It also helps to centralize configuration and shared logic so behavior remains consistent across environments and teams.

3. Validate and operate with confidence

After data selection, verify class balance and file counts before training. Silent data leakage or accidental class exclusion can invalidate evaluation metrics. Keep dataset selection code in source control and tie it to experiment metadata for reproducibility across team members and CI jobs.

Add a practical verification loop with one happy-path test, one edge-case test, and one failure-path test. Pair tests with lightweight runtime signals such as error rates, latency percentiles, or startup checks so regressions are detected early.

Operational readiness includes rollback planning. Even correct code may fail under unexpected dependencies or data. Documenting rollback steps and fallback behavior reduces recovery time and deployment risk.

Implementation depth also includes long-term operability. Define clear ownership of configuration, data contracts, and failure handling so support engineers can diagnose issues without reverse engineering intent from old commits. Where possible, capture representative input and output examples in tests, because executable examples age better than prose-only documentation.

For production systems, add lightweight observability close to the critical path: structured logs for key decisions, counters for failure categories, and latency metrics around expensive operations. These signals should map to user impact directly so on-call responders can prioritize correctly under pressure. Strong observability turns debugging from guesswork into a bounded investigation.

Finally, prepare rollback and fallback behavior before deploying significant changes. Even technically correct updates can fail due to environment differences, data anomalies, or dependency upgrades. A preplanned rollback path, feature flag, or degraded-mode strategy reduces mean time to recovery and allows teams to iterate quickly without risking prolonged outages.

Common Pitfalls

  • Relying on folder presence alone and unintentionally loading unwanted classes.
  • Changing class list order and getting inconsistent label indices between runs.
  • Using one selection rule for training and another for validation.
  • Ignoring corrupt image files in selected directories.
  • Forgetting to document which class folders were intentionally excluded.

Summary

Restrict flow_from_directory with explicit class whitelists or move to dataframe-driven input for full control. Deterministic selection and validation checks are the keys to trustworthy training data. Pair implementation detail with testing and operational safeguards so the solution remains reliable as code, dependencies, and infrastructure evolve.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.