[英]ValueError: Failed to find data adapter that can handle input: <class 'exponents_processing_batches.Sequence_train'>, <class 'NoneType'>
[英]ValueError: Failed to find data adapter that can handle input: <class 'NoneType'>, <class 'NoneType'> in keras model.predict
我在 Keras 中制作了一個 CNN model 並將其保存為“model.h5”。 它采用 128x128 的輸入形狀。 現在,我在一個新文件中,並試圖用這個 model 進行預測。這是我到目前為止所做的:
import keras
from keras.preprocessing.image import load_img, img_to_array
from keras.models import load_model
import PIL
img = load_img("img.jpg")
img = img_to_array(img)
img = img.resize((128, 128))
model = load_model('model.h5')
model.summary()
abc = model.predict(img)
print(abc)
這是我的錯誤:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-e23dbdb3fe22> in <module>()
14 model.summary()
15
---> 16 abc = model.predict(img)
17
18 print(abc)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in select_data_adapter(x, y)
969 "Failed to find data adapter that can handle "
970 "input: {}, {}".format(
--> 971 _type_name(x), _type_name(y)))
972 elif len(adapter_cls) > 1:
973 raise RuntimeError(
ValueError: Failed to find data adapter that can handle input: <class 'NoneType'>, <class 'NoneType'>
任何幫助,將不勝感激。
提前致謝
您正在嘗試在此行之后調整 img 數組的大小:
img = img_to_array(img)
您可能正在嘗試使用重塑數組而不是使用調整大小。 如果要調整加載圖像的大小,您可能需要在將其轉換為數組之前執行此操作,即在此行之前:
img = img_to_array(img)
更新:
您正在嘗試在用於圖像 object 的數組上使用調整 function 的大小。 因此它返回 NoneType,這反過來又導致了問題。
另一件事是您的 model 需要一個 4 維向量(使用您提供的文件檢查)作為輸入,並且您將其傳遞給 NoneType,如果您希望調整 PIL 的 function 的大小,就像您期望將數組重塑為 128 * 12,它仍然是一個二維向量,因此在使用重塑而不是調整大小時會出現錯誤。
您可以通過以下更改使您的代碼工作:
img = load_img("img.jpg")
img = img.resize((128, 128))
img = img_to_array(img)
img = img.reshape( -1,128, 128,3)
print(img.shape)
model = load_model('hotdogs.h5')
model.summary()
abc = model.predict(img)
print(abc)
在這里,使用 reshape 將輸入數組轉換為 model 預期的 4 維數組。
我希望這有幫助。 我是 StackOverflow 的新手。 如果你覺得這個答案有幫助,如果你能給我一個贊成票,那將是一種激勵。
從 Tensorflow 導入 Keras 為我解決了這個問題。
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.models import load_model
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.