【问题标题】:TensorFlow: classify imageTensorFlow:对图像进行分类
【发布时间】:2020-02-09 08:30:25
【问题描述】:

我正在关注有关使用 TensorFlow 2.0 进行图像分类的教程:https://www.tensorflow.org/tutorials/images/classification

本教程展示了如何构建和训练模型,但我不明白如何实际使用该模型。

我正在寻找一种方法来传递图像(最好只是它的路径)并返回某种分类结果。像这样的:

result = model.evaluate('path/to/image.jpg')
# result == {'cat': 0.92, 'dog': 0.08}

如何实现?另外,模型保存在哪里,训练完成后如何访问?

【问题讨论】:

    标签: python tensorflow classification tensorflow-datasets tensorflow2.0


    【解决方案1】:

    对于打印出图像为 X% 猫、%Y 狗、this 的百分比概率结果的特定情况,特定的 tensorflow 教程可能更有用。

    在其中,他们确实介绍了如何绘制百分比可能性,以及使用 tensorflow 的大部分基础知识。

    训练模型后,您可以使用更多代码以图形方式显示结果,如教程中的以下代码:

    def plot_image(i, predictions_array, true_label, img):
      predictions_array, true_label, img = predictions_array, true_label[i], img[i]
      plt.grid(False)
      plt.xticks([])
      plt.yticks([])
    
      plt.imshow(img, cmap=plt.cm.binary)
    
      predicted_label = np.argmax(predictions_array)
      if predicted_label == true_label:
        color = 'blue'
      else:
        color = 'red'
    
      plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                    100*np.max(predictions_array),
                                    class_names[true_label]),
                                    color=color)
    
    def plot_value_array(i, predictions_array, true_label):
      predictions_array, true_label = predictions_array, true_label[i]
      plt.grid(False)
      plt.xticks(range(10))
      plt.yticks([])
      thisplot = plt.bar(range(10), predictions_array, color="#777777")
      plt.ylim([0, 1])
      predicted_label = np.argmax(predictions_array)
    
      thisplot[predicted_label].set_color('red')
      thisplot[true_label].set_color('blue')
    

    然后,使用以下代码,您可以绘制一些关于结果的图:

    至于访问您的模型并保存它,以下tensorflow tutorial 可能有用。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2018-07-23
      • 2016-04-19
      • 2018-03-07
      • 2017-07-18
      • 1970-01-01
      • 2016-10-16
      • 2019-08-07
      • 2018-08-27
      相关资源
      最近更新 更多