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":表示标签(只能有两个)编码为 float32 标量,值为 0 或 1(例如,用于 binary_crossentropy)。None(无标签)。labels 为 "inferred" 时有效。这是类别名称的显式列表(必须与子目录的名称匹配)。用于控制类别的顺序(否则使用字母数字顺序)。"grayscale"(灰度)、"rgb"(RGB)或 "rgba"(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"。默认值为 "bilinear"。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 张量,表示类索引的独热编码。关于生成图像通道数的规则
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"(RGB)或 "rgba"(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() 的其他关键字参数。