[英]Getting a prediction from a loaded tensor flow classification model, .pd file
Apologies if this is a trivial question.如果这是一个微不足道的问题,请道歉。 I just can't quite figure it out.我只是想不通。 I followed the tutorial found here: https://www.tensorflow.org/tutorials/images/classification .我按照此处找到的教程进行操作: https : //www.tensorflow.org/tutorials/images/classification 。 Built the model and trained it.建立模型并训练它。 I saved the model to disk with: <model_name>.save(<directory location>)
我将模型保存到磁盘: <model_name>.save(<directory location>)
This saved the following files in 'directory location':这将以下文件保存在“目录位置”中:
A folder 'assets', this folder is empty.一个文件夹'assets',这个文件夹是空的。 A folder 'variables' and 'saved_model.pb'文件夹“variables”和“saved_model.pb”
Using:使用:
loaded_model = tf.keras.models.load_model('directory location', compile=True)
I load the saved model.我加载保存的模型。 Then with然后用
loaded_model.summary()
I get:我得到:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 150, 150, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 75, 75, 16) 0
_________________________________________________________________
dropout (Dropout) (None, 75, 75, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 75, 75, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 37, 37, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 37, 37, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 18, 18, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 18, 18, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 20736) 0
_________________________________________________________________
dense (Dense) (None, 512) 10617344
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 10,641,441
Trainable params: 10,641,441
Non-trainable params: 0
This makes me think the model loaded correctly.这让我认为模型加载正确。 Here I get stuck.在这里我卡住了。 How do I feed this model a single image and get a prediction, in this case cat or dog?我如何为这个模型提供单个图像并获得预测,在这种情况下是猫还是狗? I have searched the web and various answers here and I got as far as:我在这里搜索了网络和各种答案,我得到了:
prediction = loaded_model.predict(???)
I do not know what the ???我不知道是什么??? needs to be.需要是。 A concrete example of how to get a prediction for an image file would be greatly appreciated.将不胜感激如何获得图像文件预测的具体示例。
According to the following example the input shape for the model is 150,150,3 so in order to make a prediction we need read the image and resize it to make a prediction.根据以下示例,模型的输入形状为 150,150,3,因此为了进行预测,我们需要读取图像并调整其大小以进行预测。 (Assuming your image is in RGB or BGR) (假设您的图像是 RGB 或 BGR)
import cv2
preprocessed_image = cv2.imread("Path to Image")
preprocessed_image = cv2.resize(preprocessed_image,(150,150))
preprocessed_image = preprocessed_image.reshape(1,150,150,3)
prediction = loaded_model.predict(preprocessed_image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.