簡體   English   中英

一起轉換圖像和蒙版(Keras 示例)

[英]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, ...)在做什么?

我認為這里的imagesmasks是未定義的。 也請解釋一下。

謝謝你。

讓我們看一下fit的文檔。

如其中所述:“這會根據樣本數據數組計算與數據相關轉換相關的內部數據統計信息。僅當 featurewise_center 或 featurewise_std_normalization 或 zca_whitening 設置為 True 時才需要。”

  • 他們為什么使用它:所以我們只有在你的代碼中使用 featurewise_center 或 featurewise_std_normalization 或 zca_whitening 時才需要使用它,你有 featurewise_center=True 所以這就是他們使用它的原因
  • 它的作用:featurewise_center 和 featurewise_std_normalization 將數據集特征居中,使其均值為 0,標准值為 1。 為了做到這一點,我們需要在數據上運行並計算這個均值和標准差,以便規范化數據后綴。 這正是 fit 方法所做的。 它計算這些值,因此當您在數據上運行時,您可以相應地對數據進行規范化。

至於圖像和蒙版,它們可能是在此片段之前定義的。

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)

由於您有兩個不同的數據集( imagesmasks ),因此您需要為每個單獨的生成器執行此操作。


我認為這里的圖像和面具是未定義的。 也請解釋一下。

你是對的,它們是未定義的。 在這種情況下,我們使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM