4

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

2
  • As this depends on reading a file from your computer, your question fails the Reproducible criterion. You should verify that img is a 128x128 image. If you have Spyder, you can go to the variable explorer and see what it is. Other options would be printing it or printing img.shape. Commented Aug 14, 2020 at 3:41
  • I tried img.shape. It didnt wwork. Then I printed out img. It shwed None Commented Aug 14, 2020 at 3:44

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

That doesn't work. It gives a shape error. Earlier I had no shape error. Just one in predict. This means that it really didn't need resizing before making it an array
It would be really helpful if you can provide your model file and image file so that I can reproduce and localize the code and help you better.
Ok but there is no option to share files here
you can upload it on google drive and share the link
Ok Its a model that classifies hotdogs and not hotdogs. Here is the model drive.google.com/file/d/1MTnlmkVYKaDo8Jhee1mSK51vUy9Z2BIj/… and you can take any hotdog image from the internet
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.