[英]How does the Keras method image_dataset_from_directory() distinguish X and Y data?
I'm using the ADE20K dataset to train a Unet like model for segmentation in Keras.我正在使用 ADE20K 数据集来训练像 model 这样的 Unet,以便在 Keras 中进行分割。
The dataset has over 1000 classes.数据集有超过 1000 个类。 I'm trying to use the Keras method
image_dataset_from_directory()
to load the dataset into a tf.dataset
object.我正在尝试使用 Keras 方法
image_dataset_from_directory()
将数据集加载到tf.dataset
object 中。
The following documentation shows you how to load and pass this dataset object into your model: https://keras.io/api/preprocessing/以下文档向您展示如何加载此数据集 object 并将其传递到您的 model: https://keras.io/api/preprocessing/
# directory for training data
training_data/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg
etc.
from tensorflow import keras
from tensorflow.keras.preprocessing import image_dataset_from_directory
train_ds = image_dataset_from_directory(
directory='training_data/',
labels='inferred',
label_mode='categorical',
batch_size=32,
image_size=(256, 256))
validation_ds = image_dataset_from_directory(
directory='validation_data/',
labels='inferred',
label_mode='categorical',
batch_size=32,
image_size=(256, 256))
model = keras.applications.Xception(weights=None, input_shape=(256, 256, 3), classes=10)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit(train_ds, epochs=10, validation_data=validation_ds)
In the above example, it built a dataset object based on the folder structure provided, where each class is a folder in the directory.在上面的示例中,它基于提供的文件夹结构构建了一个数据集 object,其中每个 class 是目录中的一个文件夹。 In my case, I have a directory like this:
就我而言,我有一个这样的目录:
ADE20k_Data/
...cars/
......image_1.jpg
......image_1_segmentation.png
......image_2.jpg
......image_2_segmentation.png
...resteraunt/
......image_1.jpg
......image_1_segmentation.png
......image_2.jpg
......image_2_segmentation.png
etc.
Where in each class folder I have both X and Y (or the raw image and the segmented image).在每个 class 文件夹中,我都有 X 和 Y(或原始图像和分段图像)。
If I load my dataset according to the above example, and pass it into the.fit() method, how is X and Y distinguished?如果我按照上面的例子加载我的数据集,并将其传递给 .fit() 方法,那么 X 和 Y 是如何区分的?
I guess that is where my confusion lies.我想这就是我的困惑所在。 How to properly arrange your data's directory structure for image segmentation.
如何正确安排数据的目录结构以进行图像分割。
The way you use it will prepare the data for classification, not for segmentation.您使用它的方式将为分类而不是分段准备数据。 It will use images as X and "resteraunt", "cars" as labels for classification in Y.
它将使用图像作为 X 和“resteraunt”、“汽车”作为标签在 Y 中进行分类。
I suggest you to create your own tf.Dataset我建议你创建自己的 tf.Dataset
Considering your folder structure and assuming all your images are "*.jpg" and each has a "*_segmentation.png" pair you can use the following code to find all the images and corresponding segmentation masks.考虑您的文件夹结构并假设您的所有图像都是“*.jpg”并且每个图像都有一个“*_segmentation.png”对,您可以使用以下代码查找所有图像和相应的分割掩码。
import glob
jpgs = glob.glob('ADE20k_Data/*/*.jpg')
pngs = [f.split('.jpg')[0] + "_segmentation" + ".png" for f in jpgs]
Then you can create your Dataset from this data然后你可以从这个数据创建你的数据集
import tensorflow as tf
dataset = tf.data.Dataset.from_tensor_slices((jpgs, pngs))
At this point if you would do something like在这一点上,如果你想做类似的事情
for pair in dataset.take(1):
print(pair)
It will give you one pair of tensors, first contains path to the image, second contains path to the corresponding segmentation mask.它会给你一对张量,第一个包含图像的路径,第二个包含对应分割掩码的路径。
Further you can read image from the path, for example like this此外,您可以从路径中读取图像,例如这样
def read_images(img_path, segmentation_mask_path):
img_data = tf.io.read_file(img_path)
img = tf.io.decode_jpeg(img_data)
segm_data = tf.io.read_file(segmentation_mask_path)
segm_mask = tf.io.decode_png(segm_data)
return img, segm_mask
dataset = dataset.map(read_images)
Next you can do some preprocessing for your model接下来你可以为你的 model 做一些预处理
HEIGHT = 256
WIDTH = 256
def prepare_images(img, semg_mask):
img = tf.image.resize(img, [HEIGHT, WIDTH])
semg_mask = tf.image.resize(semg_mask, [HEIGHT, WIDTH], method='nearest')
return img, semg_mask
dataset = dataset.map(prepare_images)
At this point if you would take one instance from your dataset此时,如果您要从数据集中获取一个实例
for pair in dataset.take(1):
print(pair)
It will give you a pair of tensors, first contain input image and second contains segmentation mask as your output.它会给你一对张量,第一个包含输入图像,第二个包含分割掩码作为你的 output。
Obviously you will need lots of other stuff, like selecting the right network architecture, normalising input images (just divide img by 255), splitting your dataset into train/val/test, shuffling the training data, batching.显然,您将需要很多其他的东西,例如选择正确的网络架构、标准化输入图像(只需将 img 除以 255)、将数据集拆分为训练/验证/测试、打乱训练数据、批处理。 But you can achieve this using tf.data api, for example
dataset = dataset.batch(batch_size)
will generate you X and Y in batches as your model require.但是您可以使用 tf.data api 来实现这一点,例如
dataset = dataset.batch(batch_size)
将根据您的 model 的要求批量生成 X 和 Y。 https://www.tensorflow.org/api_docs/python/tf/data/Dataset https://www.tensorflow.org/api_docs/python/tf/data/Dataset
And then just pass your dataset into the fit method as you already do model.fit(daseset, epoches=10)
然后就像你已经做的那样将你的数据集传递给 fit 方法
model.fit(daseset, epoches=10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.