我已经构建了一个使用CIFAR-10
数据集的图像分类模型,采用了卷积神经网络
或CNNs
。
模型已经完全训练完成,准确率约为59%,但我的问题是如何从模型中获取预测的标签。模型可以预测以下10个类别:
['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
我想说的是,例如,如果我们给模型一张飞机的图片,它应该能够预测并显示预测后的标签,但我不明白如何获取预测的标签。
这是我尝试过的方法,但我不知道输出是什么意思:
with tf.Session() as sess: sess.run(init) print(sess.run(tf.argmax(y_pred, 1), feed_dict={x:ch.test_images, y_true:ch.test_labels, hold_prob:1.0})) print(sess.run(tf.argmax(y_true, 1), feed_dict={x:ch.test_images, y_true:ch.test_labels, hold_prob:1.0}))
输出:
[0 0 0 ... 0 3 3][3 8 8 ... 5 1 7]
规格:
TensorFlow版本:1.15.2
编辑器:Google Colab
操作系统:Windows 7
Google Colab文件链接:https://drive.google.com/file/d/1NpYGWvo9bNG0SJsFJ6R3se46b1ovDUX8/view?usp=sharing注意:链接已过期
如果你需要更多信息或有任何关于此问题的疑问,请告诉我!
回答:
你得到的是类别的索引
,即每个类别所代表的数字。在你的例子中,0表示’airplane’,1表示’automobile’,依此类推。
要获取类别的名称,你只需要访问类别名称列表即可。
classes=['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']with tf.Session() as sess: sess.run(init) idxs = sess.run(tf.argmax(y_pred, 1), feed_dict={x:ch.test_images, y_true:ch.test_labels, hold_prob:1.0})) labels = [classes[idx] for idx in idxs] print(labels)