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

Arc*_*yno 0 python machine-learning keras imagenet pre-trained-model

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

base_model = InceptionV3(weights='imagenet', include_top=True)
Run Code Online (Sandbox Code Playgroud)

当我从生成的图像预测,我得到具有形状的输出向量(n,1000)n报错图像的数量。因此,现在如果我想解释结果,我需要用于训练模型的1000个输出类的名称...但是我找不到它!

任何的想法 ?

tod*_*day 5

您可以使用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), ...]
Run Code Online (Sandbox Code Playgroud)

源代码

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).
    """
Run Code Online (Sandbox Code Playgroud)

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

from keras.applications.imagenet_utils import decode_predictions
Run Code Online (Sandbox Code Playgroud)