python - Keras:获取 imagenet 上预训练模型的标签名称

标签 python machine-learning keras pre-trained-model imagenet

我正在使用在 Imagenet 上预训练的 Keras Inception_v3:

base_model = InceptionV3(weights='imagenet', include_top=True)

当我根据生成的图像进行预测时,我得到形状为 (n,1000) 的输出向量,其中 n 是给定的图像数量。所以现在如果我想解释结果,我需要用于训练模型的 1000 个输出类的名称...但我找不到它!

有什么想法吗?

最佳答案

您可以使用decode_predictions方法:

from keras.applications.inception_v3 import decode_predictions

preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=10))

# Predicted: [(u'n02504013', u'Indian_elephant', 0.0042589349), ...]

来自source code :

def decode_predictions(preds, top=5, **kwargs):
    """Decodes the prediction of an ImageNet model.
    # Arguments
        preds: Numpy tensor encoding a batch of predictions.
        top: Integer, how many top-guesses to return.
    # Returns
        A list of lists of top class prediction tuples
        `(class_name, class_description, score)`.
        One list of tuples per sample in batch input.
    # Raises
        ValueError: In case of invalid shape of the `pred` array
            (must be 2D).
    """

显然它不是特定于 Inception_V3 的。您可以导入它并将其用于 Imagenet 上的任何预训练模型。或者,您可以使用以下方式导入它:

from keras.applications.imagenet_utils import decode_predictions

关于python - Keras:获取 imagenet 上预训练模型的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018226/

相关文章:

python - 在 Numpy 日期时间数组中查找唯一日期

python - 使用 Python 请求发送 SOAP 请求

machine-learning - 如何选择和使用不同数据类型的特征?

python - 使用Python训练后,神经网络未提供预期的输出

python - 如何使用 Tensorflow 数据集进行 CNN 模型训练

python - 针对架构的 XML (.xsd) 提要验证

python - 如何通过https python请求下载文件

machine-learning - Dlib的svm_c_linear_trainer什么时候比svm_c_linear_dcd_trainer更好?

python - 具有随机权重的神经网络无法学习

python - NoneType'对象没有属性 '_inbound_nodes'