Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR #18160: Use prefetch() after batching // image_dataset.py #18219

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions keras/utils/image_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ def image_dataset_from_directory(
interpolation=interpolation,
crop_to_aspect_ratio=crop_to_aspect_ratio,
)
train_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)
val_dataset = val_dataset.prefetch(tf.data.AUTOTUNE)

if batch_size is not None:
if shuffle:
Expand All @@ -286,6 +284,9 @@ def image_dataset_from_directory(
buffer_size=1024, seed=seed
)

train_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)
val_dataset = val_dataset.prefetch(tf.data.AUTOTUNE)

# Users may need to reference `class_names`.
train_dataset.class_names = class_names
val_dataset.class_names = class_names
Expand Down Expand Up @@ -314,7 +315,7 @@ def image_dataset_from_directory(
interpolation=interpolation,
crop_to_aspect_ratio=crop_to_aspect_ratio,
)
dataset = dataset.prefetch(tf.data.AUTOTUNE)

if batch_size is not None:
if shuffle:
# Shuffle locally at each iteration
Expand All @@ -324,6 +325,8 @@ def image_dataset_from_directory(
if shuffle:
dataset = dataset.shuffle(buffer_size=1024, seed=seed)

dataset = dataset.prefetch(tf.data.AUTOTUNE)

# Users may need to reference `class_names`.
dataset.class_names = class_names

Expand Down