tfdatasets provides R bindings for constructing and transforming TensorFlow data pipelines. It helps produce batched data and labels for Keras models while supporting parallel preprocessing and asynchronous prefetching. section 8.2.4
Build and transform a dataset
Create a dataset with tensor_slices_dataset(), batch it with dataset_batch(3), and reshape each element with dataset_map(). Iterate using as_iterator() and iter_next(), or pass the resulting Dataset directly to fit(). section 8.2.4
Common pipeline operations include:
- dataset_shuffle() randomizes elements within a buffer. section 8.2.4
- dataset_prefetch() prepares future batches to improve GPU utilization. section 8.2.4
- dataset_map() applies transformations to each yielded element and can use parallel calls. section 8.2.4
Listing 8.9 Using image_dataset_from_directory() to read images from directories
image_size <- shape(180, 180)
batch_size <- 32
train_dataset <-
image_dataset_from_directory(new_base_dir / "train",
image_size = image_size,
batch_size = batch_size)
validation_dataset <-
image_dataset_from_directory(new_base_dir / "validation",
image_size = image_size,
batch_size = batch_size)
test_dataset <-
image_dataset_from_directory(new_base_dir / "test",
image_size = image_size,
batch_size = batch_size)
Use image_dataset_from_directory() to create training, validation, and test datasets from directory-organized images. section 8.2.4
