Loading and preprocessing a batch can leave a model waiting for data. Prefetching overlaps that work with model execution: while the model handles one batch, the input pipeline prepares upcoming batches. section 8.2.4
Keep image batches ready
For disk-loaded JPEGs, map the image-loading function, pair images with labels, then finish the pipeline with `dataset = tf.data.Dataset.zip(images, labels).batch(16).prefetch(2)`. The `prefetch(2)` call keeps two batches buffered so training need not stop at every batch to load and resize images. section 12.2.3
Prefetching works alongside other `tf.data.Dataset` operations: `map()` transforms elements, `batch()` groups them, and `prefetch()` prepares batches ahead of use. A Dataset can be passed directly to Keras `fit()`. For distributed training, call `dataset.prefetch(buffer_size)` before `fit()`; if the buffer size is unclear, try `dataset.prefetch(tf.data.AUTOTUNE)`. section 8.2.4 section 18.2.2
Listing 12.9 Creating a dataset to train on
import tensorflow as tf def load_image(path): #1 x = tf.io.read_file(path) x = tf.image.decode_jpeg(x, channels=3) return preprocessor(x) images = tf.data.Dataset.from_tensor_slices([x["path"] for x in metadata]) images = images.map(load_image, num_parallel_calls=8) labels = {"box": box_array, "class": class_array} labels = tf.data.Dataset.from_tensor_slices(labels) dataset = tf.data.Dataset.zip(images, labels).batch(16).prefetch(2) #2 val_dataset, train_dataset = dataset.take(500), dataset.skip(500) #3
Follow the complete image pipeline from file paths through parallel loading, labels, batching, and prefetching. section 12.2.3


