![](/img/trans.png)
[英]python_keras_how to augment images and masks together and save these data sets as a ndarray?
[英]Transforming images and masks together (Keras example)
此代碼片段取自 Keras API 參考/數據預處理。
部分:將圖像和蒙版一起轉換的示例。
鏈接: 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)
我知道這個片段將掩碼和圖像一起增加並創建生成器,但我不明白image_datagen.fit(images, ... )
& mask_datagen.fit(masks, ...)
在做什么?
我認為這里的images
和masks
是未定義的。 也請解釋一下。
謝謝你。
讓我們看一下fit的文檔。
如其中所述:“這會根據樣本數據數組計算與數據相關轉換相關的內部數據統計信息。僅當 featurewise_center 或 featurewise_std_normalization 或 zca_whitening 設置為 True 時才需要。”
至於圖像和蒙版,它們可能是在此片段之前定義的。
image_datagen.fit(images, ... )
& mask_datagen.fit(masks, ...)
在做什么? 如ImageDataGenerator
的第一個示例所示,如果應用 ZCA 白化,您需要擬合數據生成器以計算特征歸一化所需的量,例如標准、均值和主成分。 fit
只是意味着根據給定的數據計算這些屬性並將它們存儲在ImageDataGenerator
object 中以供進一步使用。
這是片段:
(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)
由於您有兩個不同的數據集( images
和masks
),因此您需要為每個單獨的生成器執行此操作。
你是對的,它們是未定義的。 在這種情況下,我們使用 float32 形狀的張量( batch_size, image_size[0], image_size[1], num_channels
),從頁面上的其他示例中提取),可以從tf.data.Dataset
獲得,來自直接加載數據並僅獲取圖像(刪除標簽)甚至從隨機數據(使用numpy
)。
使用上述方法,它們允許 3D 和 4D 數字張量(一個或多個圖像)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.