使用TensorFlow Hub进行学习转移:使用单个测试图像?

时间:2020-07-07 14:45:36

标签: tensorflow keras

我已经成功学习了有关图像分类的官方教程,并进行了转移学习:https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub

现在,我的实验模型已保存,并可以在看到“好”绘画时识别。但是,我想用模型从未见过的图像对此进行测试。到目前为止,我只使用了已经将数据集划分为训练和测试文件夹的笔记本。但是,这里不是这种情况。

我认为我需要类似的东西

img = tf.keras.preprocessing.image.load_img("/content/mytestimage.jpeg", target_size=(224,224))
除其他外;但是,对于初学者而言,查看此类测试预测的示例将非常有用。到目前为止,我没有任何搜索结果-如果有人有任何建议,我非常高兴听到!

1 个答案:

答案 0 :(得分:1)

这是通过keras进行移动网络转移学习的方法,但是大多数代码应该相同。完整的转学教程可以在here中找到。我发现它非常有用。

from PIL import Image
from tensorflow.keras.models import load_model

model = load_model('path/to/model.h5')

img = Image.open(file)
array = np.asarray(img, dtype=np.float32)
  arrayexp = np.expand_dims(array, axis=0)
  arrayexp = (arrayexp/127)-1 #This is a normalization factor specifically for Mobilenet but I think it's used for many other networks
result = model.predict(arrayexp)
  print(np.argmax(result)) #Prints class with highest confidence
  print(result[0][np.argmax(result)]) #Prints confidence for the highest