{ }
View source on GitHub |
Generates a tf.data.Dataset
from image files in a directory.
tf.keras.preprocessing.image_dataset_from_directory(
directory,
labels='inferred',
label_mode='int',
class_names=None,
color_mode='rgb',
batch_size=32,
image_size=(256, 256),
shuffle=True,
seed=None,
validation_split=None,
subset=None,
interpolation='bilinear',
follow_links=False,
crop_to_aspect_ratio=False,
pad_to_aspect_ratio=False,
data_format=None,
verbose=True
)
Used in the notebooks
Used in the tutorials |
---|
If your directory structure is:
main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg
Then calling image_dataset_from_directory(main_directory,
labels='inferred')
will return a tf.data.Dataset
that yields batches of
images from the subdirectories class_a
and class_b
, together with labels
0 and 1 (0 corresponding to class_a
and 1 corresponding to class_b
).
Supported image formats: .jpeg
, .jpg
, .png
, .bmp
, .gif
.
Animated gifs are truncated to the first frame.
Returns |
---|
A tf.data.Dataset
object.
- If
label_mode
isNone
, it yieldsfloat32
tensors of shape(batch_size, image_size[0], image_size[1], num_channels)
, encoding images (see below for rules regardingnum_channels
). - Otherwise, it yields a tuple
(images, labels)
, whereimages
has shape(batch_size, image_size[0], image_size[1], num_channels)
, andlabels
follows the format described below.
Rules regarding labels format:
- if
label_mode
is"int"
, the labels are anint32
tensor of shape(batch_size,)
. - if
label_mode
is"binary"
, the labels are afloat32
tensor of 1s and 0s of shape(batch_size, 1)
. - if
label_mode
is"categorical"
, the labels are afloat32
tensor of shape(batch_size, num_classes)
, representing a one-hot encoding of the class index.
Rules regarding number of channels in the yielded images:
- if
color_mode
is"grayscale"
, there's 1 channel in the image tensors. - if
color_mode
is"rgb"
, there are 3 channels in the image tensors. - if
color_mode
is"rgba"
, there are 4 channels in the image tensors.