深度学习VGG16模型应用

5 篇文章 0 订阅
5 篇文章 0 订阅
import matplotlib.pyplot as plt
# 导入VGG16模型
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np

model = VGG16(weights="imagenet", include_top=True)
img_path = "image/man.jpg"
img = image.load_img(img_path, target_size=(224, 224))  # 读取图片并将尺寸转换为(224 x 224)

x = image.img_to_array(img)  # 将读取的图片转化成
x = np.expand_dims(x, axis=0)  # 转换为张量size为(1, 224, 224, 3)
x = preprocess_input(x)

features = model.predict(x)  # 预测,取得features,维度为(1, 1000)

# 取得前5个最可能的类别及概率
pred = decode_predictions(features, top=5)[0]

# 整理预测结果
values = []
bar_label = []
for element in pred:
    values.append(element[2])
    bar_label.append(element[1])


def percent(value):
    return "%.2f" % (value * 100)


# 绘图
fig = plt.figure("预测结果")
ax = fig.add_subplot(111)
plt.ylabel("probability")
plt.bar(range(len(values)), values, tick_label=bar_label, width=0.5, fc="g")
plt.title("top-5")
for a, b in zip(range(len(values)), values):
    plt.text(a, b + 0.0005, percent(b), ha='center', va='bottom', fontsize=7)
print(percent(b))
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值