簡體   English   中英

ValueError:未能找到可以處理輸入的數據適配器:<class 'nonetype'> ,<class 'nonetype'> 在 keras model.predict</class></class>

[英]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

ValueError:未能找到可以處理輸入的數據適配器:<class 'numpy.ndarray'> , <class 'scipy.sparse.csr.csr_matrix'< div><div id="text_translate"><p> 請幫我解決這個問題</p><pre>X_train = np.asarray(X_train) y_train = np.asarray(y_train) X_test = np.asarray(X_test) y_test = np.asarray(y_test) history = model.fit(X_train, y_train, epochs=75, batch_size=batch_size, verbose=2, validation_data=(X_test, y_test), callbacks= [lrate])</pre><p> ValueError:無法找到可以處理輸入的數據適配器:&lt;class 'numpy.ndarray'&gt;, &lt;class 'scipy.sparse.csr.csr_matrix' 即使我轉換為 numpy 數組,但出現錯誤。 請幫忙。 謝謝你。</p></div></class></class>

[英]ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'scipy.sparse.csr.csr_matrix'

暫無
暫無

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

相關問題 ValueError:未能找到可以處理輸入的數據適配器:<class 'exponents_processing_batches.Sequence_train'> ,<class 'NoneType'> 無法找到可以處理輸入的數據適配器:<class 'tensorflow.python.keras.preprocessing.image.ImageDataGenerator'> ,<class 'NoneType'> 來自 TF 的 Keras:損失為 NaN 並且無法找到可以處理輸入的數據適配器:<class 'pandas.core.frame.dataframe'> ,<class 'nonetype'></class></class> Keras,可以處理輸入的數據適配器:<class 'function'> ,<class 'nonetype'> ” 在批量訓練中</class></class> Tensorflow | ValueError:未能找到可以處理輸入的數據適配器:<class 'numpy.ndarray'></class> Keras 擬合生成器 - ValueError:無法找到可以處理輸入的數據適配器 ValueError:無法找到可以處理輸入的數據適配器:<class 'numpy.ndarray'> ,<class 'pandas.core.frame.DataFrame'> ValueError:未能找到可以處理輸入的數據適配器:<class 'numpy.ndarray'> , <class 'sklearn.preprocessing._encoders.onehotencoder'></class></class> ValueError:未能找到可以處理輸入的數據適配器:<class 'numpy.ndarray'> , <class 'scipy.sparse.csr.csr_matrix'< div><div id="text_translate"><p> 請幫我解決這個問題</p><pre>X_train = np.asarray(X_train) y_train = np.asarray(y_train) X_test = np.asarray(X_test) y_test = np.asarray(y_test) history = model.fit(X_train, y_train, epochs=75, batch_size=batch_size, verbose=2, validation_data=(X_test, y_test), callbacks= [lrate])</pre><p> ValueError:無法找到可以處理輸入的數據適配器:&lt;class 'numpy.ndarray'&gt;, &lt;class 'scipy.sparse.csr.csr_matrix' 即使我轉換為 numpy 數組,但出現錯誤。 請幫忙。 謝謝你。</p></div></class></class> Tensorflow ValueError: 未能找到可以處理輸入的數據適配器
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM