相同的输入两次导致不同的预测

时间:2019-09-24 04:17:07

标签: python keras

我从文件中读取图像,并调用Keras Inception v3模型的预测方法。我从一个输入中发现了两个不同的结果。

from keras.applications.inception_v3 import InceptionV3, decode_predictions
from keras.preprocessing import image
import numpy as np

def model():
    model = InceptionV3(weights='imagenet')
    def predict(x):
        x *=  2
        x -= 1
        return model.predict(np.array([x]))[0]
    return predict

img = image.load_img("2.jpg", target_size=(299, 299))
img = image.img_to_array(img)
img /= 255.

p = model()

print('Predicted:', decode_predictions(np.array([p(img)]), top=3)[0])
print('Predicted:', decode_predictions(np.array([p(img)]), top=3)[0])

输出为

Predicted: [('n01443537', 'goldfish', 0.98162466), ('n02701002', 'ambulance', 0.0010537759), ('n01440764', 'tench', 0.00027527584)]
Predicted: [('n02606052', 'rock_beauty', 0.69015616), ('n01990800', 'isopod', 0.039278224), ('n01443537', 'goldfish', 0.03365362)]

第一个结果正确的地方。

1 个答案:

答案 0 :(得分:0)

您可能会在img函数中修改输入(predict),而不仅仅是在本地。修改后的输入将在下一个预测中使用,然后再次对其进行修改。因此,您实际上在第一次调用predict时一次应用了修改,但在第二次调用中两次申请了修改。 您可以在此question中找到有关该行为的更多详细信息。

相关问题