TensorFlow
image validation
machine learning
deep learning
AI debugging

CRITICAL tensorflowCategory has no images - validation

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A message like CRITICAL: ... Category has no images - validation usually means your validation dataset contains at least one class directory with zero usable images. TensorFlow image-loading tools expect every class participating in validation to have actual readable image files. When one class is empty, mislabeled, or unreadable, dataset construction or validation splitting breaks.

What the Error Is Really Telling You

The phrase "category has no images" is usually not about the model architecture. It is a data-layout problem.

Typical causes include:

  • an empty class folder in the validation set
  • the wrong directory path
  • files with unsupported extensions
  • images filtered out by your loading logic
  • a split process that sent all images of one class to training and none to validation

That is why the fix starts with the filesystem, not with TensorFlow layers.

Check the Directory Structure First

A common image dataset layout looks like this:

text
1data/
2  train/
3    cats/
4    dogs/
5  validation/
6    cats/
7    dogs/

Every class directory inside validation must contain at least one valid image file if the loader expects that class there.

If validation/dogs exists but contains no images, the validation dataset is incomplete.

Reproduce the Problem with a Small Check

Before running TensorFlow, inspect the file counts explicitly.

python
1from pathlib import Path
2
3validation_dir = Path("data/validation")
4
5for class_dir in validation_dir.iterdir():
6    if class_dir.is_dir():
7        count = sum(1 for p in class_dir.iterdir() if p.suffix.lower() in {".jpg", ".jpeg", ".png"})
8        print(class_dir.name, count)

If one class prints 0, that is the category the error is complaining about.

Example with image_dataset_from_directory

A typical TensorFlow input pipeline might look like this:

python
1import tensorflow as tf
2
3val_ds = tf.keras.utils.image_dataset_from_directory(
4    "data/validation",
5    image_size=(224, 224),
6    batch_size=32,
7)

This works only if the directory tree actually contains readable class images. If one class folder is empty, the validation dataset is invalid for the expected class set.

Splitting the Data More Safely

Sometimes the directory layout is not pre-split into train and validation. In that case, a safer pattern is to let TensorFlow split one parent directory consistently.

python
1import tensorflow as tf
2
3train_ds = tf.keras.utils.image_dataset_from_directory(
4    "data/all_images",
5    validation_split=0.2,
6    subset="training",
7    seed=42,
8    image_size=(224, 224),
9    batch_size=32,
10)
11
12val_ds = tf.keras.utils.image_dataset_from_directory(
13    "data/all_images",
14    validation_split=0.2,
15    subset="validation",
16    seed=42,
17    image_size=(224, 224),
18    batch_size=32,
19)

This reduces the chance of manually creating an empty validation class by mistake.

Watch for Non-Image Files

Another subtle issue is that a folder may appear non-empty in Finder or Explorer but still contain zero valid images from TensorFlow's perspective.

Examples:

  • '.DS_Store'
  • text files
  • corrupt images
  • files with unexpected extensions

A directory full of such files can still behave like an empty image class during dataset loading.

A quick Pillow-based readability check can help:

python
1from pathlib import Path
2from PIL import Image
3
4for image_path in Path("data/validation").rglob("*"):
5    if image_path.suffix.lower() in {".jpg", ".jpeg", ".png"}:
6        try:
7            with Image.open(image_path) as img:
8                img.verify()
9        except Exception as exc:
10            print("bad file:", image_path, exc)

A Practical Fix Sequence

When you see this error, use a simple order of operations:

  1. verify the root path is correct
  2. count valid images per validation class
  3. remove or replace corrupt files
  4. ensure each validation class has at least one image
  5. rerun the dataset loader before touching model code

Most of the time, the issue is resolved before you ever retrain the model.

Common Pitfalls

  • Debugging the model architecture when the real issue is an empty validation class directory.
  • Assuming a folder is fine because it contains files, even though none of them are supported images.
  • Performing a manual train-validation split that leaves one class with zero validation examples.
  • Using the wrong directory root so TensorFlow scans a partially built dataset tree.
  • Ignoring corrupt files that look like images by name but fail during loading.

Summary

  • 'Category has no images - validation is usually a dataset layout problem, not a model problem.'
  • At least one validation class directory has zero readable images.
  • Count usable files per class before training.
  • Let TensorFlow split one parent directory when possible to reduce manual mistakes.
  • Fix paths, file extensions, and corrupt images before changing model code.

Course illustration
Course illustration

All Rights Reserved.