问题描述
我在keras中使用了预先训练的模型,最终预测类索引是一些整数值,但是我似乎不明白如何打印这些类的名称?
I am using a pre-trained model in keras and ended up predicting the class indices to be some integer values, but I don't seem to understand how do I print the names those classes?
我正在使用的模型是ResNet 50.
The model I am using is ResNet 50.
仍然无法弄清楚,对我来说,获得编辑答复是非常重要的.
Still can't figure it out and it's really important for me to get it answered to editing it.
推荐答案
如果您不使用imagenet数据,而是按照自己的方式进行重新训练:使用ImageDataGenerator
的keras的flow_from_directory
时,它将方便地保存以下标签:您在.class_indices
中可访问的dict
中的文件夹!字典的值与model.predict
输出的位置相对应.
If you are not using imagenet data but retrain on your own set: When using keras' flow_from_directory
of an ImageDataGenerator
, it conveniently saves the labels of your folders in a dict
accessible within .class_indices
! The values of the dict correspond to the positions of your model.predict
output.
粗暴的例子:
preds = model.predict(x)
for cls in training_generator.class_indices:
print(cls+": "+preds[0][training_generator.class_indices[cls]])
另请参阅: https://keras.io/preprocessing/image/
这篇关于如何使用keras在神经网络中打印预测类的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!