您的位置:首页 > 其它

Keras —— 基于Vgg16模型(含全连接层)的图片识别

2018-02-20 19:59 531 查看

一、加载并显示图片

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
plt.imshow(img)
plt.show()




二、图片预处理

#将图片转成numpy array
x = image.img_to_array(img)
#扩展维度,因为preprocess_input需要4D的格式
x = np.expand_dims(x, axis=0)
#对张量进行预处理
x = preprocess_input(x)


三、加载VGG16模型(包含全连接层)

model = VGG16(include_top=True, weights='imagenet')
preds = model.predict(x)
# 将结果解码成一个元组列表(类、描述、概率)(批次中每个样本的一个这样的列表)
print('Predicted:', decode_predictions(preds, top=3)[0])


四、预测结果

Predicted: [('n01871265', 'tusker', 0.77154845), ('n02504458', 'African_elephant', 0.15930885), ('n02504013', 'Indian_elephant', 0.06914174)]


由预测结果可知,图片有百分之77的概率是tusker,百分之16的概率是African_elephant,百分之6的概率是Indian_elephant

源码地址:

https://github.com/Zheng-Wenkai/Keras_Demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  keras demo
相关文章推荐