gpt4 book ai didi

python - TensorFlow:对图像进行分类

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:34 24 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

对于打印出图像是 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')

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

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

希望对您有所帮助!

关于python - TensorFlow:对图像进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58347572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com