简体   繁体   English

使用张量流迁移学习模型预测单个图像文件

[英]Prediction for single image file using tensor flow transfer learning model

I am new to image classification and sorry if this problem seems too naive.我是图像分类的新手,如果这个问题看起来太幼稚,我很抱歉。 I am using tf transfer learning model for my recent work.我在最近的工作中使用了 tf 迁移学习模型。 Ref: https://www.tensorflow.org/tutorials/images/transfer_learning .参考: https : //www.tensorflow.org/tutorials/images/transfer_learning

Here there is clear mention of how to use this model to prediction on batch prediction for images.这里清楚地提到了如何使用这个模型来预测图像的批量预测。 But i am having a hard time figuring out how to do prediction for single image using this.但是我很难弄清楚如何使用它对单个图像进行预测。

I tried with this:我试过这个:

np_image = Image.open(image_path)
np_image = np.array(np_image).astype('float32')/255
np_image = transform.resize(np_image, (800, 700, 3))
np_image = np.expand_dims(np_image, axis=0)
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict_proba(np_image)

But this is giving 1 as result for all images.但这对所有图像都给出了 1 作为结果。 I want probability prediction at an image level using this model.我想要使​​用此模型在图像级别进行概率预测。

Got a solution for this problem.得到了这个问题的解决方案。

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
image = load_img(image_path, target_size=(800, 700))


image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
predictions = tf.nn.sigmoid(new_model.predict(image))
predictions=np.array(predictions)[0][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM