我在使用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