所以,我运行了Keras中使用Inception-V3模型的示例代码,结果预测偏差很大。我猜测可能是权重有误。有人知道这是为什么吗?
我使用的是:Keras 2.0.4,Python 3.5(64位)
https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py
这是我运行的代码:
import numpy as npfrom keras.applications.inception_v3 import InceptionV3from keras.preprocessing import imagefrom keras.applications.imagenet_utils import preprocess_input, decode_predictionsif __name__ == '__main__': model = InceptionV3(include_top=True, weights='imagenet') img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:', decode_predictions(preds))
给出的结果是:
Predicted: [[('n01924916', 'flatworm', 0.99995065), ('n03047690', 'clog', 4.9389007e-05), ('n04366367', 'suspension_bridge', 1.075191e-08), ('n01665541', 'leatherback_turtle', 2.5111552e-10), ('n03950228', 'pitcher', 6.6290827e-11)]]
当我用相同的图像通过ResNet50模型运行时,它给出的结果是:
Predicted: [[('n02504458', 'African_elephant', 0.59942758), ('n01871265', 'tusker', 0.33637413), ('n02504013', 'Indian_elephant',0.061940487), ('n02397096', 'warthog', 0.0016048651), ('n02396427', 'wild_boar', 0.00016479047)]]
编辑
我在Inception-V3模型上测试了其他图像,它对每张不同的图像都给出了相同的预测。任何关于此问题的见解将不胜感激。
回答:
Inception
和 ResNet
有不同的预处理函数。为了对Inception
预处理输入,请尝试以下函数:
def preprocess_input(x): x /= 255. x -= 0.5 x *= 2. return x