简体   繁体   English

一起转换图像和蒙版(Keras 示例)

[英]Transforming images and masks together (Keras example)

This code snippet has been taken from Keras API reference/Data Preprocessing.此代码片段取自 Keras API 参考/数据预处理。

Section: Example of transforming images and masks together.部分:将图像和蒙版一起转换的示例。

link: https://keras.io/api/preprocessing/image/链接: https://keras.io/api/preprocessing/image/

# we create two instances with the same arguments
data_gen_args = dict(featurewise_center=True,
                     featurewise_std_normalization=True,
                     rotation_range=90,
                     width_shift_range=0.1,
                     height_shift_range=0.1,
                     zoom_range=0.2)
image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)
# Provide the same seed and keyword arguments to the fit and flow methods
seed = 1
image_datagen.fit(images, augment=True, seed=seed)
mask_datagen.fit(masks, augment=True, seed=seed)
image_generator = image_datagen.flow_from_directory(
    'data/images',
    class_mode=None,
    seed=seed)
mask_generator = mask_datagen.flow_from_directory(
    'data/masks',
    class_mode=None,
    seed=seed)
# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)
model.fit_generator(
    train_generator,
    steps_per_epoch=2000,
    epochs=50)

I understand that this snippet is augmenting both masks and images together and creating generators but I don't understand what is the image_datagen.fit(images, ... ) & mask_datagen.fit(masks, ...) doing?我知道这个片段将掩码和图像一起增加并创建生成器,但我不明白image_datagen.fit(images, ... ) & mask_datagen.fit(masks, ...)在做什么?

And I think here images & masks are undefined.我认为这里的imagesmasks是未定义的。 Please explain those too.也请解释一下。

Thank you.谢谢你。

Lets look at the documentation at fit .让我们看一下fit的文档。

As stated in there: "This computes the internal data stats related to the data-dependent transformations, based on an array of sample data. Only required if featurewise_center or featurewise_std_normalization or zca_whitening are set to True."如其中所述:“这会根据样本数据数组计算与数据相关转换相关的内部数据统计信息。仅当 featurewise_center 或 featurewise_std_normalization 或 zca_whitening 设置为 True 时才需要。”

  • Why they use it: So we need to use this only if we use featurewise_center or featurewise_std_normalization or zca_whitening in your code you have featurewise_center=True so this is the reason why they use it .他们为什么使用它:所以我们只有在你的代码中使用 featurewise_center 或 featurewise_std_normalization 或 zca_whitening 时才需要使用它,你有 featurewise_center=True 所以这就是他们使用它的原因
  • What does it do: featurewise_center and featurewise_std_normalization centers the dataset features to have mean zero and std one.它的作用:featurewise_center 和 featurewise_std_normalization 将数据集特征居中,使其均值为 0,标准值为 1。 In order to do that we need to run on the data and compute this mean and std in order to normalize the data afterword.为了做到这一点,我们需要在数据上运行并计算这个均值和标准差,以便规范化数据后缀。 This is exactly what the fit method do.这正是 fit 方法所做的。 It computes those values so when you run on the data you could normalize the data accordingly.它计算这些值,因此当您在数据上运行时,您可以相应地对数据进行规范化。

As for images and masks they probably were defined before this snippet.至于图像和蒙版,它们可能是在此片段之前定义的。

What are image_datagen.fit(images, ... ) & mask_datagen.fit(masks, ...) doing? image_datagen.fit(images, ... ) & mask_datagen.fit(masks, ...)在做什么?

As shown in the first example of ImageDataGenerator , you need to fit the data generator in order to compute quantities required for featurewise normalization such as std, mean, and principal components if ZCA whitening is applied.ImageDataGenerator的第一个示例所示,如果应用 ZCA 白化,您需要拟合数据生成器以计算特征归一化所需的量,例如标准、均值和主成分。 fit just means calculate these properties based on the given data and store them in the ImageDataGenerator object for further use. fit只是意味着根据给定的数据计算这些属性并将它们存储在ImageDataGenerator object 中以供进一步使用。

Here is the snippet:这是片段:

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)

# THE IMPORTANT PART! 
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)

Since you have two distinct datasets ( images and masks ), you need to perform this operation for each individual generator.由于您有两个不同的数据集( imagesmasks ),因此您需要为每个单独的生成器执行此操作。


And I think here images & masks are undefined.我认为这里的图像和面具是未定义的。 Please explain those too.也请解释一下。

You're right, they are undefined.你是对的,它们是未定义的。 On this case we're treating with float32 tensors of shape ( batch_size, image_size[0], image_size[1], num_channels ), extracted from other examples on the page), which can be obtained from a tf.data.Dataset , from loading data directly and getting only the images (removing the labels) or even from random data (using numpy ).在这种情况下,我们使用 float32 形状的张量( batch_size, image_size[0], image_size[1], num_channels ),从页面上的其他示例中提取),可以从tf.data.Dataset获得,来自直接加载数据并仅获取图像(删除标签)甚至从随机数据(使用numpy )。

Playing a bit with the aforementioned methods, they allow both 3D and 4D numeric tensors (one or multiple images).使用上述方法,它们允许 3D 和 4D 数字张量(一个或多个图像)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM