2、模型部署与应用开发-图像识别系统,顺序模型和网格结构模型指定图片分类的简单实现

在深度学习当中,因为模型在训练时使用的方法不同,所以到后面应用预测时也就需要采用不同的方法,本次介绍2种不同网格结构的模型进行应用,希望对大家入门有所帮助

运行环境:
keras 2.3.1
opencv-python 4.1.2.30
numpy 1.17.4
tensorflow-gpu 1.13.1

使用的图片可以网上找:
在这里插入图片描述
在这里插入图片描述
目录结构:
在这里插入图片描述
Model 网络结构模型
recognition1.py

import cv2
from keras.models import load_model
import numpy as np
img=cv2.imread('image/frog.jpg')
img=cv2.resize(img,(32,32),interpolation=cv2.INTER_AREA)
categories=['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
img=(img.reshape(1,32,32,3))/255
model=load_model('model1.h5')
forecast=model.predict(img)
forecast=np.argmax(forecast)
forecast=categories[forecast]
print('预测结果:',forecast)

运行结果:
在这里插入图片描述

Sequential 顺序模型
recognition2.py

import cv2
from keras.models import load_model
img=cv2.imread('image/panda.jpg')
img=cv2.resize(img,(300,300))
img=(img.reshape(1,-1))/255
model=load_model('model2.h5')
categories=['cat','panda']
forecast=categories[int(model.predict_classes(img))]
print('预测结果:',forecast)

运行结果:
在这里插入图片描述
小结:
2个代码文件预测使用的代码分别是:

forecast=model.predict(img)
forecast=np.argmax(forecast)
forecast=categories[forecast]

forecast=categories[int(model.predict_classes(img))]

在模型预测时, 我们可以使用predict结合numpy的argmax方法预测,而网络结构模型Model class不能使用predict_classes()方法,顺序模型Sequential class 2种方法都适用的

第一个网格结构模型代码文件如果使用predict_classes方法可能会报错:AttributeError: ‘Model’ object has no attribute ‘predict_classes’

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值