简体   繁体   中英

Same input results in different prediction in two times

I read an image from file and call predict method of Keras Inception v3 model. And I found two different results from one input.

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])

The output is

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)]

where the first result is correct.

You are modifying your input ( img ) in the predict function not just locally, as you might expect. That modified input is used in the next predict, where it again is modified. So you are effectively appying the modifications once for in your first call to predict , but twice in the second call. You can find more details about that behavior in this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM