Data prefetching prepares future input elements asynchronously while the model processes current elements. Buffering keeps execution flowing instead of making each training step wait for loading or preprocessing. section 8.2.4 section 12.2.3
Prefetching an image dataset
In an R pipeline, batch the images and labels, then keep two batches ready: `dataset <- zip_datasets(images, labels) |> dataset_batch(16) |> dataset_prefetch(2)`. The model can process one batch while loading and resizing the next. For uncertain workloads, `dataset_prefetch(buffer_size = tf$data$AUTOTUNE)` lets TensorFlow choose a buffer size. section 12.2.3 section 18.2.2
Use prefetching to improve input-pipeline behavior:
- Pair it with parallel mapping when preprocessing includes disk reads, decoding, resizing, or tokenization. section 12.2.3 section 16.2
- Keep datasets streaming rather than loading an entire large image collection into memory. section 12.2.3
- Pass a Dataset object to `fit()`, especially for distributed training, to support efficient execution. section 18.2.2
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
Inspect a complete image pipeline that decodes and preprocesses files in parallel, batches labels with images, and applies `dataset_prefetch(2)`. section 12.2.3


