tensorflow/models · error · ValueError
No class subdirectories found in train_directory
Error message
No class subdirectories found in train_directory: {train_directory} What it means
Raised by compute_class_weights when the training directory contains zero subdirectories. The function builds class weights by iterating immediate child directories of train_directory (PyTorch ImageFolder layout); with no subdirectories there are no classes to weight, so the resulting tensor would be empty and downstream training invalid. Typical causes: pointing at the wrong directory, a flat directory of files with no per-class folders, or a not-yet-populated dataset path.
Solutions
- Verify train_directory points to the dataset root that contains one subdirectory per class (ImageFolder format)
- Create per-class subdirectories and move images into them, e.g. train/class_a/*.jpg, train/class_b/*.jpg
- Check for path typos, relative-path issues, or unmounted storage that leaves the directory empty
- Confirm dataset download/extraction completed before computing class weights
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at official/projects/waste_identification_ml/fine_tuning/Dinov3_image_classifier/datasets.py:226 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/4832c813987f1423.
Report an issue: GitHub.
Appendix: source
Thrown at official/projects/waste_identification_ml/fine_tuning/Dinov3_image_classifier/datasets.py:226
Args:
train_directory: Path to the training directory. Expected to contain
one subdirectory per class, in PyTorch ImageFolder format.
Returns:
A 1D `torch.Tensor` of dtype `float32` and shape `(number_of_classes,)`
containing the class weight for each class, ordered alphabetically by
class name.
Raises:
ValueError: If `train_directory` contains no class subdirectories or if
any class subdirectory contains no files.
"""
class_names = sorted(
entry.name for entry in train_directory.iterdir() if entry.is_dir()
)
if not class_names:
raise ValueError(
f"No class subdirectories found in train_directory: {train_directory}"
)
class_counts = []
for class_name in class_names:
class_path = train_directory / class_name
number_of_files = sum(
1 for entry in class_path.iterdir() if entry.is_file()
)
if number_of_files == 0:
raise ValueError(f"Class directory is empty: {class_path}")
class_counts.append(number_of_files)
number_of_classes = len(class_counts)
total_samples = sum(class_counts)
weights = [
total_samples / (number_of_classes * count) for count in class_counts
]View on GitHub (pinned to e006f5f0d5)