如何用一张图像预测训练好的模型?



我想用一张图像(rgb)测试训练好的模型。但是我遇到了一个错误。在训练模型时,我使用了猫和狗的图像。此外,在创建模型时,我从resnet50获得了第一层。最后的图层是我自己创建的。在将数据集导出到模型之前,我做了一些初步工作并将类转换为0-1值。(编码器cat:0,dog:1)现在我想用狗的图像来测试这个模型。我希望它返回0或1,但是我有一个问题。

my code block

*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)*

错误:

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

错误告诉你输入的形状(1,750,3)与模型所期望的形状不匹配(None, 224, 224, 3)。

我建议你首先调整你的图像大小为224 × 224,然后使用255的除法将其规范化。然后展开维度,使它变成(1,224,224,3),然后再试一次。

有两种图片加载操作,您可以选择其中一种。

from keras.models import load_model
from keras.preprocessing import image
# load the model we saved
test_image = image.load_img('.../5c02ed550f25442260cff6ab.jpg', target_size=(224, 224))
test_image = image.img_to_array(test_image) / 255.0
model = load_model('.../Resnet50_Save_model.hdf5')
result = model.predict(test_image, batch_size=1)
print(result)

最新更新