snookso
snookso

Reputation: 385

ValueError: Failed to find data adapter that can handle input: <class 'NoneType'>, <class 'NoneType'> in keras model.predict

I have made a CNN model in Keras and saved it as 'model.h5'. It takes an input shape of 128x128. Now, I am in a new file and am trying to make predictions with this model.Here is what I have done so far:

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)

Here is my error:

---------------------------------------------------------------------------
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'>

Any help would be appreciated.

Thanks in advance

Upvotes: 4

Views: 7327

Answers (2)

cemsazara
cemsazara

Reputation: 1673

Importing Keras from Tensorflow solved the problem for me.

from tensorflow.keras.preprocessing.image import load_img, img_to_array 
from tensorflow.keras.models import load_model

Upvotes: 0

Kaushal Sharma
Kaushal Sharma

Reputation: 2300

You are trying to resize img array after this line:

img = img_to_array(img)

You might be trying to use reshape the array instead of using resize. If you want to resize the loaded image, you might want to do it before converting it to an array, i.e. before this line:

img = img_to_array(img) 

UPDATE:

You are trying to use resize function on an array that is meant for an image object. Hence it is returning NoneType, which in turn is causing the issue.

Another thing is your model expects a 4-dimension vector(inspected using the file provided by you) as input and you are passing it NoneType, also if you expect the resize function of PIL as you expected to reshape your array to 128 * 128, it will still be a 2-d vector, hence is giving the error on using reshape instead of the resize.

You can make your code work with the following change:

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)

Here, using reshape to convert the input array to a 4-dimensional array that is expected by your model.

I hope this helps. I am a newbie on StackOverflow. It would be motivating if you could give me an upvote if you find this answer helpful.

Upvotes: 4

Related Questions