簡體   English   中英

如何將 RGB 圖像轉換為灰度,擴展該灰度圖像的尺寸以在 InceptionV3 中使用?

[英]How to convert RGB images to grayscale, expand dimensions of that grayscale image to use in InceptionV3?

我正在訓練 Keras model 並且我有 RGB 格式的訓練圖像。 我想訓練我的 model 但在 InceptionV3 上的灰度圖像上,但它需要 RGB 圖像作為輸入。 我的問題是:如何先將 RGB 圖像轉換為灰度圖像,然后進行 3 維復制? 我正在使用 Keras 的TrainDataGenerator.flow_from_directory方法。

ImageDataGenerator中,您可以傳遞一個預處理 function。 使用函數tf.image.rgb_to_grayscaletf.image.grayscale_to_rgb進行轉換:

def to_grayscale_then_rgb(image):
    image = tf.image.rgb_to_grayscale(image)
    image = tf.image.grayscale_to_rgb(image)
    return image
tf.keras.preprocessing.image.ImageDataGenerator(
    rescale=1/255,
    preprocessing_function=to_grayscale_then_rgb
)

我認為最簡單的方法是使用TrainDataGenerator.flow_from_directory方法的color_mode ="grayscale"參數。

這是指定如何執行此操作的 keras 在線文檔https://keras.io/api/preprocessing/image/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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