繁体   English   中英

Keras Inception-V3模型预测的方式

[英]Keras Inception-V3 model predictions way off

因此,我运行了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模型上测试了其他图像,它为每个不同的图像提供相同的预测。 任何有关该问题的见解都将得到解决。

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

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

您必须从xception模块导入preprocess_input

from keras.applications.xception import preprocess_input

或者调用preprocess_input(images, mode='tf')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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