image_dataset_from_directory
函数tf_keras.utils.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,
**kwargs
)
从目录中的图像文件生成一个 tf.data.Dataset
。
如果你的目录结构是
main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg
那么调用 image_dataset_from_directory(main_directory, labels='inferred')
将返回一个 tf.data.Dataset
,它会产生来自子目录 class_a
和 class_b
的图像批次,以及标签 0 和 1(0 对应于 class_a
,1 对应于 class_b
)。
支持的图像格式:.jpeg
、.jpg
、.png
、.bmp
、.gif
。动画 GIF 会被截断为第一帧。
参数
labels
是 "inferred"
,它应该包含子目录,每个子目录包含一个类的图像。否则,目录结构将被忽略。"inferred"
(标签从目录结构生成)、None
(无标签),或者与目录中找到的图像文件数量相同大小的整数标签列表/元组。标签应该根据图像文件路径的字母数字顺序(通过 Python 中的 os.walk(directory)
获得)进行排序。labels
编码的字符串。选项有"int"
:表示标签被编码为整数(例如,用于 sparse_categorical_crossentropy
损失)。"categorical"
表示标签被编码为类别向量(例如,用于 categorical_crossentropy
损失)。"binary"
表示标签(只能有 2 个)被编码为值为 0 或 1 的 float32
标量(例如,用于 binary_crossentropy
)。None
(无标签)。labels
为 "inferred"
时有效。这是类名称的显式列表(必须与子目录的名称匹配)。用于控制类的顺序(否则使用字母数字顺序)。"grayscale"
、"rgb"
、"rgba"
之一。默认为 "rgb"
。图像是否会转换为具有 1、3 或 4 个通道。None
,则数据不会被批处理(数据集将产生单个样本)。默认为 32。(height, width)
。由于管道处理必须具有相同大小的图像批次,因此必须提供此项。默认为 (256, 256)
。True
。如果设置为 False
,则按字母数字顺序对数据进行排序。"training"
、"validation"
或 "both"
之一。仅在设置 validation_split
时使用。当 subset="both"
时,该实用程序返回一个包含两个数据集(分别是训练数据集和验证数据集)的元组。"bilinear"
。支持 "bilinear"
、"nearest"
、"bicubic"
、"area"
、"lanczos3"
、"lanczos5"
、"gaussian"
、"mitchellcubic"
。False
。True
,则调整图像大小而不产生纵横比失真。当原始纵横比与目标纵横比不同时,将裁剪输出图像,以便返回与目标纵横比匹配的图像中最大可能窗口(大小为 image_size
)。默认情况下(crop_to_aspect_ratio=False
),纵横比可能不会被保留。返回值
一个 tf.data.Dataset
对象。
label_mode
为 None
,则它会产生形状为 (batch_size, image_size[0], image_size[1], num_channels)
的 float32
张量,用于编码图像(有关 num_channels
的规则,请参见下文)。(images, labels)
,其中 images
的形状为 (batch_size, image_size[0], image_size[1], num_channels)
,而 labels
遵循如下所述的格式。关于标签格式的规则
label_mode
为 "int"
,则标签是形状为 (batch_size,)
的 int32
张量。label_mode
为 "binary"
,则标签是形状为 (batch_size, 1)
的 1 和 0 的 float32
张量。label_mode
为 "categorical"
,则标签是形状为 (batch_size, num_classes)
的 float32
张量,表示类索引的 one-hot 编码。关于生成的图像中通道数的规则
color_mode
为 "grayscale"
,则图像张量中有 1 个通道。color_mode
为 "rgb"
,则图像张量中有 3 个通道。color_mode
为 "rgba"
,则图像张量中有 4 个通道。load_img
函数tf_keras.utils.load_img(
path,
grayscale=False,
color_mode="rgb",
target_size=None,
interpolation="nearest",
keep_aspect_ratio=False,
)
将图像加载为 PIL 格式。
用法
image = tf.keras.utils.load_img(image_path)
input_arr = tf.keras.utils.img_to_array(image)
input_arr = np.array([input_arr]) # Convert single image to a batch.
predictions = model.predict(input_arr)
参数
color_mode="grayscale"
。"grayscale"
、"rgb"
、"rgba"
之一。默认值:"rgb"
。所需的图像格式。None
(默认为原始大小)或整数元组 (img_height, img_width)
。"nearest"
、"bilinear"
和 "bicubic"
。如果安装了 PIL 1.1.3 或更新版本,则也支持 "lanczos"
。如果安装了 PIL 3.4.0 或更新版本,则也支持 "box"
和 "hamming"
。默认情况下,使用 "nearest"
。返回值
一个 PIL Image 实例。
引发
img_to_array
函数tf_keras.utils.img_to_array(img, data_format=None, dtype=None)
将 PIL Image 实例转换为 Numpy 数组。
用法
from PIL import Image
img_data = np.random.random(size=(100, 100, 3))
img = tf.keras.utils.array_to_img(img_data)
array = tf.keras.utils.image.img_to_array(img)
参数
"channels_first"
或 "channels_last"
。None
表示使用全局设置 tf.keras.backend.image_data_format()
(除非您更改了它,否则它使用 "channels_last"
)。默认为 None
。None
使使用全局设置 tf.keras.backend.floatx()
(除非您更改了它,否则它使用 "float32"
)。默认为 None
。返回值
一个 3D Numpy 数组。
引发
img
或 data_format
。save_img
函数tf_keras.utils.save_img(
path, x, data_format=None, file_format=None, scale=True, **kwargs
)
将存储为 Numpy 数组的图像保存到路径或文件对象。
参数
"channels_first"
或 "channels_last"
。[0, 255]
范围内。PIL.Image.save()
的其他关键字参数。