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

Keras: get labels name of pre-trained models on 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