我在這里的帖子有類似的問題:如何將 RGB 影像轉換為灰度,擴展該灰度影像的尺寸以在 InceptionV3 中使用?
本質上,我正在訓練使用遷移學習(使用 Inception)在 FER2013 上進行訓練,以構建用于預測圖片情緒的模型。不幸的是,影像是灰度的,Inception 模型使用 rgb 作為輸入。
我嘗試使用建議的解決方案,但它回傳一個錯誤,我沒有足夠的聲譽來評論原始解決方案。
這是原來的解決方案:
def to_grayscale_then_rgb(image):
image = tf.image.rgb_to_grayscale(image)
image = tf.image.grayscale_to_rgb(image)
return image
我將它插入到我的資料生成器中。我也試過最初只使用灰度到 rgb,但也回傳了一個錯誤。
train_rgb_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255,
preprocessing_function= to_grayscale_then_rgb ,
#preprocessing_function=tf.image.grayscale_to_rgb,
vertical_flip= True)
train_dataflow_rgb = train_rgb_datagen.flow_from_directory(train_root,
target_size = (48,48),
seed = seed_num)
test_rgb_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255,
preprocessing_function= to_grayscale_then_rgb,
#preprocessing_function=tf.image.grayscale_to_rgb,
vertical_flip= True)
test_dataflow_rgb = test_rgb_datagen.flow_from_directory(test_root,
target_size = (48,48),
shuffle = False,
seed = seed_num)
當我嘗試訓練模型時,出現以下錯誤:
epochs = 50
steps_per_epoch = 1000
tl_Incept_history = tl_Incept_model.fit(train_dataflow_rgb,
epochs = epochs,
validation_data=(test_dataflow_rgb),
#steps_per_epoch=steps_per_epoch,
callbacks=[early_callback, myCallback])
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10932/801602138.py in <module>
2 steps_per_epoch = 1000
3
----> 4 tl_Incept_history = tl_Incept_model.fit(train_dataflow_rgb,
5 epochs = epochs,
6 validation_data=(test_dataflow_rgb),
~\Venv\testpy39\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\Venv\testpy39\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
56 try:
57 ctx.ensure_initialized()
---> 58 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
59 inputs, attrs, num_outputs)
60 except core._NotOkStatusException as e:
InvalidArgumentError: input depth must be evenly divisible by filter depth: 1 vs 3
uj5u.com熱心網友回復:
預處理代碼很好,您只是看起來尺寸不匹配。它想要image_size[0], image_size[1], num_channels,其中 num_channels = 3 如果 rgb(每個,r,g,b),并且 = 1 如果灰度。
您有兩個實體target_size = (48,48),- 如果將它們更改為 ,它會起作用target_size = (48,48,3),嗎?
如果沒有,要進一步除錯,請def to_grayscale_then_rgb(image):分別嘗試使用影像并查看回傳的影像的尺寸。如果它是二維的(例如 image_size[0], image_size[1],),您可以像這樣探索在函式內重塑影像:XXX = tf.convert_to_tensor(XXX[:,:,:3])如https://stackoverflow.com/a/60212961/7420967 中所示,盡管grayscale_to_rgb應該輸出最終維度3...
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388269.html
標籤:Python 张量流 卷积神经网络 keras文件 迁移学习
上一篇:TypeError:unhashabletype:'numpy.ndarray'Python3.9影像分類使用tensorflow和keras
