如何用一张图片预测训练好的模型?
How to predict trained model with one image?
我想用单张图像 (rgb) 测试训练好的模型。但是我遇到了一个错误。我在训练 model.Also 时使用了猫狗图像,在创建模型时,我从 resnet50 获得了第一层。我自己创建了最后一层。在将数据集导出到模型之前,我做了一些初步工作并将 类 转换为 0-1 值。(使用编码器 cat:0,dog:1)现在我想用狗测试这个模型图片。我希望它是 return 0 或 1,但我遇到了问题。
我的代码块:
*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 x 224,然后使用除以 255 对其进行归一化。然后扩大尺寸使其变为 (1, 224, 224, 3),然后重试。
有两种图片加载操作,您可以select其中一种。
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)
我想用单张图像 (rgb) 测试训练好的模型。但是我遇到了一个错误。我在训练 model.Also 时使用了猫狗图像,在创建模型时,我从 resnet50 获得了第一层。我自己创建了最后一层。在将数据集导出到模型之前,我做了一些初步工作并将 类 转换为 0-1 值。(使用编码器 cat:0,dog:1)现在我想用狗测试这个模型图片。我希望它是 return 0 或 1,但我遇到了问题。
我的代码块:
*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 x 224,然后使用除以 255 对其进行归一化。然后扩大尺寸使其变为 (1, 224, 224, 3),然后重试。
有两种图片加载操作,您可以select其中一种。
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)