Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 40 ยท Data Pipelines and Saving Models

Loading Images and Text Datasets

Load datasets from disk for Keras: image_dataset_from_directory, text_dataset_from_directory, CSV data, TFRecord files and train/validation splits, with Python stand-ins.

Tutorial datasets such as MNIST arrive as ready-made arrays. Real projects start with folders of photos, text files or CSV exports. Keras provides utilities that read these formats straight into a tf.data.Dataset, so you rarely write a loading loop yourself. This lesson covers the common cases, the folder conventions Keras expects, and what a TFRecord is. Pipeline steps like batch and prefetch were explained in tf.data Pipelines.

TensorFlow is not available in the browser runner. Keras and tf.data code is read-only; the runnable blocks reproduce the logic (folder labeling, splitting, encoding) in plain Python.

Images from folders

Put one folder per class. The folder name becomes the label.

TEXT
photos/
  cats/    c001.jpg c002.jpg ...
  dogs/    d001.jpg d002.jpg ...
  birds/   b001.jpg ...
Python
import keras

train_ds = keras.utils.image_dataset_from_directory(
    "photos",
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(180, 180),
    batch_size=32,
)
val_ds = keras.utils.image_dataset_from_directory(
    "photos",
    validation_split=0.2,
    subset="validation",
    seed=123,                 # SAME seed, or the splits overlap
    image_size=(180, 180),
    batch_size=32,
)
print(train_ds.class_names)   # ['birds', 'cats', 'dogs']  (alphabetical)

Important details:

  • Class indices follow alphabetical folder order. The label for cats is 1 here.
  • All images are resized to image_size; pixels stay in the 0 to 255 range as floats, so add a Rescaling(1/255) layer inside the model.
  • The same seed for both calls guarantees the training and validation subsets do not overlap.
  • label_mode can be "int" (default, for sparse loss), "categorical" (one-hot) or "binary".

The labeling rule is easy to reproduce:

Python Folder names become labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Splitting safely

Python A seeded train/validation split

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Text from folders

The same convention works for text: one folder per label with .txt files inside.

Python
train_ds = keras.utils.text_dataset_from_directory(
    "reviews/train", batch_size=32, validation_split=0.2,
    subset="training", seed=42,
)
# each element: (tensor of strings, tensor of integer labels)

Feed it to a TextVectorization layer (see Text Vectorization and Embeddings) by mapping the text part only:

Python
text_only = train_ds.map(lambda text, label: text)
vectorize.adapt(text_only)

Call adapt on training text only; adapting on validation or test data leaks information about them into the vocabulary.

CSV and tabular data

For small CSV files, load with pandas and slice:

Python
import pandas as pd
df = pd.read_csv("housing.csv")
target = df.pop("price")
ds = tf.data.Dataset.from_tensor_slices((dict(df), target)).batch(32)

Large CSVs can stream with tf.data.experimental.make_csv_dataset. A model for mixed columns uses preprocessing layers: Normalization for numeric columns, StringLookup or CategoryEncoding for categories, and Discretization or HashedCrossing for bucketing.

Python Encoding categorical and numeric columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

TFRecord files

A TFRecord is TensorFlow's binary container: a sequence of serialized records, each usually a tf.train.Example holding named features (bytes, floats, int64s). Reading one big sequential file is much faster than opening a million small image files, especially from network storage. You write it once, then stream it:

Python
def to_example(image_bytes, label):
    return tf.train.Example(features=tf.train.Features(feature={
        "image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes])),
        "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
    }))

with tf.io.TFRecordWriter("train.tfrecord") as writer:
    for path, label in files:
        writer.write(to_example(open(path, "rb").read(), label).SerializeToString())

spec = {"image": tf.io.FixedLenFeature([], tf.string),
        "label": tf.io.FixedLenFeature([], tf.int64)}
ds = (tf.data.TFRecordDataset("train.tfrecord")
      .map(lambda r: tf.io.parse_single_example(r, spec))
      .map(lambda d: (tf.io.decode_jpeg(d["image"]), d["label"])))

Reach for TFRecords when data is huge, stored in cloud buckets or read by many workers. For a few thousand images, folders are simpler.

Generators for odd formats

tf.data.Dataset.from_generator wraps any Python generator, at the cost of speed, since Python code runs in one thread:

Python
ds = tf.data.Dataset.from_generator(
    my_generator,
    output_signature=(tf.TensorSpec((64, 64, 3), tf.float32),
                      tf.TensorSpec((), tf.int32)),
)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does image_dataset_from_directory decide each image's label?

    Each subfolder is a class; labels are assigned by alphabetical folder order.
  2. 2. Why must the training and validation calls use the same seed with validation_split?

    Both calls shuffle file lists with the seed; different seeds can put the same image in both subsets.
  3. 3. On which data should TextVectorization.adapt() be called?

    Building the vocabulary from validation or test text would leak information into training.
  4. 4. What is a main advantage of TFRecord files?

    Sequential reads from big files beat opening millions of small files, particularly on network storage.

Summary

  • image_dataset_from_directory and text_dataset_from_directory turn folder-per-class layouts into batched datasets.
  • Labels follow alphabetical folder order; use the same seed for training and validation subsets.
  • Normalize, vectorize and encode with preprocessing layers fitted on training data only.
  • Use TFRecords for large, remote or parallel-read data and from_generator as a last resort for odd formats.
  • Pass the resulting dataset straight to model.fit.

Next: Saving and Loading Models