Keras使用VGG16模型预测自己的图片


Keras使用VGG16模型预测自己的图片

环境
Win10
Miniconda3
Pycharm2018.02


代码如下

from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
import keras
import tensorflow as tf

# 模型手动下载然后放到目录C:\Users\用户名\.keras\models下,下载地址如下:
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5

# 载入模型并打印
model = VGG16()
print(model.summary())

# 加载一副测试图片
image = load_img("dog.jpg", target_size=(224, 224))

# 转为数组
image = img_to_array(image)

# 重塑成4D
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

# 预处理图片
image = preprocess_input(image)

# 预测
predict_result = model.predict(image)

# 解析预测结果
label = decode_predictions(predict_result)

# 打印出三个概率最大的分类
for idx in range(0, 3):
    print("类别:%s        概率:%0.4f"%(label[0][idx][1], label[0][idx][2]))

# 清理
keras.backend.clear_session()
tf.reset_default_graph()


效果

在这里插入图片描述

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

觉皇嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值