Python Opencv使用ann神经网络识别手写数字功能
作者:Keras深度学习 时间:2023-11-03 02:44:52
opencv中也提供了一种类似于Keras的神经网络,即为ann,这种神经网络的使用方法与Keras的很接近。
关于mnist数据的解析,读者可以自己从网上下载相应压缩文件,用python自己编写解析代码,由于这里主要研究knn算法,为了图简单,直接使用Keras的mnist手写数字解析模块。
本次代码运行环境为:
python 3.6.8
opencv-python 4.4.0.46
opencv-contrib-python 4.4.0.46
下面的代码为使用ann进行模型的训练:
from keras.datasets import mnist
from keras import utils
import cv2
import numpy as np
#opencv中ANN定义神经网络层
def create_ANN():
ann=cv2.ml.ANN_MLP_create()
#设置神经网络层的结构 输入层为784 隐藏层为80 输出层为10
ann.setLayerSizes(np.array([784,64,10]))
#设置网络参数为误差反向传播法
ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
#设置激活函数为sigmoid
ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
#设置训练迭代条件
#结束条件为训练30次或者误差小于0.00001
ann.setTermCriteria((cv2.TermCriteria_EPS|cv2.TermCriteria_COUNT,100,0.0001))
return ann
#计算测试数据上的识别率
def evaluate_acc(ann,test_images,test_labels):
#采用的sigmoid激活函数,需要对结果进行置信度处理
#对于大于0.99的可以确定为1 对于小于0.01的可以确信为0
test_ret=ann.predict(test_images)
#预测结果是一个元组
test_pre=test_ret[1]
#可以直接最大值的下标 (10000,)
test_pre=test_pre.argmax(axis=1)
true_sum=(test_pre==test_labels)
return true_sum.mean()
if __name__=='__main__':
#直接使用Keras载入的训练数据(60000, 28, 28) (60000,)
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
#变换数据的形状并归一化
train_images=train_images.reshape(train_images.shape[0],-1)#(60000, 784)
train_images=train_images.astype('float32')/255
test_images=test_images.reshape(test_images.shape[0],-1)
test_images=test_images.astype('float32')/255
#将标签变为one-hot形状 (60000, 10) float32
train_labels=utils.to_categorical(train_labels)
#测试数据标签不用变为one-hot (10000,)
test_labels=test_labels.astype(np.int)
#定义神经网络模型结构
ann=create_ANN()
#开始训练
ann.train(train_images,cv2.ml.ROW_SAMPLE,train_labels)
#在测试数据上测试准确率
print(evaluate_acc(ann,test_images,test_labels))
#保存模型
ann.save('mnist_ann.xml')
#加载模型
myann=cv2.ml.ANN_MLP_load('mnist_ann.xml')
训练100次得到的准确率为0.9376,可以接着增加训练次数或者提高神经网络的层次结构深度来提高准确率。
使用ann神经网络的模型结构非常小,因为只是保存了权重参数。
可以看到整个模型文件的大小才1M,而svm的大小为十多兆,knn的为几百兆,因此使用ann神经网络更加适合部署在客户端上。
接下来使用ann进行图片的测试识别:
import cv2
import numpy as np
if __name__=='__main__':
#读取图片
img=cv2.imread('shuzi.jpg',0)
img_sw=img.copy()
#将数据类型由uint8转为float32
img=img.astype(np.float32)
#图片形状由(28,28)转为(784,)
img=img.reshape(-1,)
#增加一个维度变为(1,784)
img=img.reshape(1,-1)
#图片数据归一化
img=img/255
#载入ann模型
ann=cv2.ml.ANN_MLP_load('minist_ann.xml')
#进行预测
img_pre=ann.predict(img)
#因为激活函数sigmoid,因此要进行置信度处理
ret=img_pre[1]
ret[ret>0.9]=1
ret[ret<0.1]=0
print(ret)
cv2.imshow('test',img_sw)
cv2.waitKey(0)
运行程序,结果如下,可见该模型正确识别了数字0.
来源:https://keras-lx.blog.csdn.net/article/details/111694841
标签:python,opencv,识别手写数字,ann,神经网络
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python动态加载模块的3种方法
2023-12-22 21:33:29
VS2019如何查看类图的方法实现
2023-08-22 04:12:01
![](https://img.aspxhome.com/file/2023/2/132462_0s.png)
javascript引导程序
2024-04-16 10:31:16
SQL实现相邻两行数据的加减乘除操作
2024-01-24 23:12:34
![](https://img.aspxhome.com/file/2023/6/118146_0s.jpg)
一分钟带你掌握Python中pip的安装与使用方法
2021-02-10 10:38:12
![](https://img.aspxhome.com/file/2023/1/83921_0s.jpg)
利用numba让python速度提升百倍
2022-06-12 14:31:02
![](https://img.aspxhome.com/file/2023/9/77879_0s.png)
透明度设置
2009-12-12 18:51:00
详解MySQL导出指定表中的数据的实例
2024-01-15 05:23:09
php ajax异步读取rss文档数据
2023-10-17 19:59:02
python文件读写操作与linux shell变量命令交互执行的方法
2022-10-24 06:37:55
Golang学习笔记之延迟函数(defer)的使用小结
2024-02-03 07:56:04
Python远程视频监控程序的实例代码
2021-02-08 14:26:22
matplotlib grid()设置网格线外观的实现
2021-08-26 08:39:13
![](https://img.aspxhome.com/file/2023/2/90632_0s.png)
python openpyxl使用方法详解
2021-12-23 14:49:18
一文详解Golang中的切片数据类型
2024-02-12 18:04:19
![](https://img.aspxhome.com/file/2023/5/105145_0s.png)
ubuntu 16.04下mysql5.7.17开放远程3306端口
2024-01-17 13:00:03
详解Python垃圾回收机制和常量池的验证
2022-12-28 09:58:29
Python Web程序搭建简单的Web服务器
2022-08-07 12:33:04
![](https://img.aspxhome.com/file/2023/2/124682_0s.png)
go高并发时append方法偶现错误解决分析
2024-02-13 20:30:57
Python3 json模块之编码解码方法讲解
2021-07-15 17:29:15