使用pre_trained模型进行VGG预测

第一步:导入VGG模型以及实例化

from tensorflow.python.keras.applications.vgg16 import VGG16

model=VGG16()

第二步:读入图片,并把图片转换成数组格式

from tensorflow.python.keras.applications.image import load_img
from tensorflow.python.keras.applications.image import img_to_array

img=load_img("filepath",target_size=(224,224))
img=img_to_array(img)

第三步:图片的预处理

from tensorflow.python.keras.applications.vgg16 import preprocess_input 

img=img.reshape(1,img.shape[0],img.shape[1],img.shape[2])
img=preprocessing_input(img)

第四步:开始预测

y_predict=model.predict(img)
print(y_predict)
print(np.argmax(y_predict,axis=1))
label=decode_predictions(y_predict)
print("预测类别是:%s,并且预测概率为:%f"%(label[0][0][1],label[0][0][2]))

完整代码

import numpy as np
from tensorflow.python.keras.applications.vgg16 import VGG16,decode_predictions,preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img
from tensorflow.python.keras.preprocessing.image import img_to_array
import numpy
def predict():
    model=VGG16()
    #print(model.summary())
    #预测一张图
    #加载图片并输入到模型当中
    img=load_img(r"C:\Users\xy\Desktop/laohu.jpg",target_size=(224,224))#vgg的输入要求是224 224的
    img=img_to_array(img)

    #输入到卷积中,需要四维结构,四维结构 用1去填充
    img=img.reshape(1,img.shape[0],img.shape[1],img.shape[2])

    #预测之前做图像的数据处理,归一化处理
    img=preprocess_input(img)
    y_predictions=model.predict(img)
    print(y_predictions)
    print(np.argmax(y_predictions,axis=1))
    #vgg16当中能直接进行解码
    label=decode_predictions(y_predictions)
    print(label)
if __name__ == '__main__':
    predict()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值