Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

python 3.x - How to predict trained model with one image?

I want to test the trained model with a single image(rgb). But I am encountering an error. I used cat and dog images while training the model.Also, while creating the model, I got the first layers from resnet50. I created the last layers myself. Before exporting the dataset to the model, I did some preliminary work and converted the classes to 0-1 values.(with encoder cat:0,dog:1) Now I want to test this model with a dog image. I expect it to return 0 or 1, but I have a problem.

my code blok:

*from keras.models import load_model
from keras.preprocessing import image
import numpy as np
from PIL import Image
import numpy as np
from skimage import transform
# dimensions of our images    -----   
img_width, img_height = 750, 422
# load the model we saved
model = load_model('.../Resnet50_Save_model.hdf5')
test_image = image.load_img('.../5c02ed550f25442260cff6ab.jpg', target_size=(img_width, img_height))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = Image.open('.../5c02ed550f25442260cff6ab.jpg')
test_image = test_image.resize((750,422))
test_image = test_image / 255.0
test_image = test_image.reshape(224,224)
result = model.predict(test_image, batch_size=1)
print(result)*

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-89-cd9abec3a0ce> in <module>()
     23 # test_image = test_image.reshape(224,224)
     24 
---> 25 result = model.predict(test_image, batch_size=1)
     26 print(result)

9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    975           except Exception as e:  # pylint:disable=broad-except
    976             if hasattr(e, "ag_error_metadata"):
--> 977               raise e.ag_error_metadata.to_exception(e)
    978             else:
    979               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1478 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1468 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1461 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:274 assert_input_compatibility
        ', found shape=' + display_shape(x.shape))

    ValueError: Input 0 is incompatible with layer sequential: expected shape=(None, 224, 224, 3), found shape=(1, 750, 3)

 - List item
question from:https://stackoverflow.com/questions/65840312/how-to-predict-trained-model-with-one-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is telling you that the shape of your input (1, 750, 3) doesn't match the expected shape by your model (None, 224, 224, 3).

I recommend you resize your image to 224 x 224 first, then normalize it using the division by 255. After that expand the dimensions so it becomes (1, 224, 224, 3) and try again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...