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

我在使用Keras的Inception_v3模型,该模型在Imagenet上进行了预训练:

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

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

有什么想法吗?


回答:

你可以使用decode_predictions方法:

from keras.applications.inception_v3 import decode_predictionspreds = model.predict(x)print('Predicted:', decode_predictions(preds, top=10))# Predicted: [(u'n02504013', u'Indian_elephant', 0.0042589349), ...]

源代码来看:

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注