The tf.data API builds input pipelines that load, preprocess, and batch data for training. It is useful when loading everything into memory would be impractical or when preprocessing could leave an accelerator waiting for its next batch. section 12.2.3 section 14.4.1
Stream images for object detection
In R, start with image paths, turn them into a dataset, and map a function that reads each file, decodes its JPEG, and applies a preprocessor. Make a separate dataset of class and box labels, then zip images with labels. Batch the pairs, prefetch two batches, and take or skip batches to create validation and training datasets. Images are loaded as needed rather than all expanded into memory at once. section 12.2.3
Listing 12.8 Creating a dataset to train on
library(tfdatasets, exclude = c("shape"))
images <- metadata$path |> normalizePath() |>
tensor_slices_dataset() |>
dataset_map(\(path) {
path |>
tf$io$read_file() |> #1
tf$image$decode_jpeg(channels = 3L) |> #1
preprocessor() #1
}, num_parallel_calls = 8) #1
labels <- tensor_slices_dataset(list(
box = box_array, class = class_array
))
dataset <- zip_datasets(images, labels) |> #2
dataset_batch(16) |> dataset_prefetch(2)
val_dataset <- dataset |> dataset_take(500) #3
train_dataset <- dataset |> dataset_skip(500) #3
The R/tfdatasets listing shows the complete image-path-to-batch pipeline, including parallel mapping, labels, prefetching, and the validation split. section 12.2.3
The same pattern extends beyond images:
- For IMDb reviews, map a TextVectorization layer over a dataset to produce bag-of-words inputs while retaining their labels. Parallel CPU preprocessing helps keep a GPU or TPU supplied with data. section 14.4.1
- For English-to-Spanish translation, map sentence pairs into tokenized inputs, next-token targets, and sample weights that exclude padded labels. Cache, shuffle, batch, and prefetch the results. section 15.2.1
- For mini-GPT pretraining, interleave text-file shards so CPU workers can read and tokenize separate files. Form token sequences and prefetch batches for training. section 16.2
Although tf.data belongs to TensorFlow, a Keras training workflow can use its datasets with TensorFlow, JAX, or PyTorch backends; Keras converts input tensors for the selected backend. section 14.4.1

