Keras Inception-V3模型预测的方式

时间:2017-06-06 16:42:33

标签: python-3.x machine-learning neural-network deep-learning keras

因此,我运行了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 np
from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input, decode_predictions

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

修改

我在初始-v3模型上测试了其他图像,它为每个不同的图像提供相同的预测。任何关于这个问题的见解都会被贬低。

2 个答案:

答案 0 :(得分:3)

InceptionResNet具有不同的预处理功能。为了预处理Inception的输入,请尝试以下函数:

def preprocess_input(x):
    x /= 255.
    x -= 0.5
    x *= 2.
    return x

答案 1 :(得分:0)

您必须从preprocess_input模块导入xception

from keras.applications.xception import preprocess_input

或致电preprocess_input(images, mode='tf')