环境准备:IDE:pycharm

python版本:python3.8

外部库:tensorflow2.3、opencv4.0+、matplotlib3.5、sklearn

因为python3.9好像与opencv4.0不兼容还是什么问题,3.9导入之后导入不了opencv4.0的,所以使用3.8

导入库的话可以参考https://blog.csdn.net/weixin_50398435/article/details/124836546

用到的数据集:https://download.csdn.net/download/weixin_50398435/85825390

先上代码:这一个是主体代码,就是一整个模型训练测试的,

import os
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
import numpy as np
import tensorflow.python.keras as keras
from tensorflow.python.keras import layers
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from sklearn.model_selection import train_test_splitdef testdatainout():#这里是选择测试的文件#E:\testimage\image\a\img037-001.pngpath = input("文件路径:")x = []y = input("正确值:")print(path)img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)img = cv2.resize(img,(28,28))x.append(img)x = np.array(x)x = x/255.0x = x.reshape(28, 28, 1)plt.imshow(x)plt.show()return x,y#
def getdata():dir1 = 'E:\\testimage\\image'sub_dir_and_files = os.listdir(dir1)sub_dirs = []# 一共26个文件夹,a-zfor x in sub_dir_and_files:if os.path.isdir(dir1 + '/' + x):sub_dirs.append(x)print(sub_dirs)# 每个文件夹40个训练样本一共1040个# N总图片数量N = 0# 遍历每个文件夹a-zfor subdir in sub_dirs:N += len(os.listdir(dir1 + '/' + subdir))print(N)# X放所有图片X = []# y所有图片的真实内容标签a-zy = [''] * Ni = 0# 遍历每个文件夹读取图片放入Xfor subdir in sub_dirs:image_files = os.listdir(dir1 + '/' + subdir)# print(image_files)for image in image_files:filename = dir1 + '/' + subdir + '/' + image# 图片灰度化img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)# 缩小图片由(1200,900)到(28,28)img = cv2.resize(img, (28, 28), )X.append(img)# label用数字0-25代替字符串a-zy[i] = ord(subdir) - 97i += 1# X,y格式list->numpy.ndarrayX = np.array(X)y = np.array(y)return  X,y#建立模型训练模型
def traindata():X, y = getdata()X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=10)# 归一化X_train = X_train / 255.0X_test = X_test / 255.0X_train = X_train.reshape((728, 28, 28, 1))X_test = X_test.reshape((312, 28, 28, 1))# 将类别向量转换为二进制(只有0和1)的矩阵类型表示(将原有的类别向量转换为独热编码的形式)y_trainOnehot = to_categorical(y_train)y_testOnehot = to_categorical(y_test)# 建立模型model = Sequential()# 卷积层model.add(Conv2D(filters=256,kernel_size=(5, 5),padding='same',  # 保证卷积核大小,不够补零input_shape=(28, 28, 1),activation='relu'))# 池化层model.add(MaxPool2D(pool_size=(3, 3)))model.add(Dropout(0.25))# 卷积层model.add(Conv2D(filters=256, kernel_size=(5, 5), padding='same', activation='relu'))model.add(Conv2D(filters=256, kernel_size=(5, 5), padding='same', activation='relu'))model.add(MaxPool2D(pool_size=(3, 3)))#正则化去掉1/4的神经元model.add(Dropout(0.25))# 扁平层model.add(Flatten())# 全连接层激活函数relumodel.add(Dense(256, activation='relu'))model.add(Dropout(0.25))# 全连接层激活函数softmaxmodel.add(Dense(26, activation='softmax'))# 输出模型各层的参数状况model.summary()# 训练模型model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])model.fit(X_train, y_trainOnehot, epochs=20)# 返回损失和精度res = model.evaluate(X_test, y_testOnehot)print(model.metrics_names)print(res)#循环多次测试模型iftest = 'y'while iftest == 'y':xxx,yyy = testdatainout()xxx = (np.expand_dims(xxx, 0))prob = model.predict(xxx)print("预测值:", chr(np.argmax(prob) + 97))#print("真实值:", chr(yyy + 97))#意味着输入的真实值必须是0-23print("真实值:", yyy)iftest = input("是否继续测试:y/n")if __name__ == '__main__':# E:\testimage\image\a\img037-001.pngtraindata()print("helloworld")

因为数据集图片不是模型理想大小所以第一步先处理数据集,然后因为数据集处理之后没有重新写回去,所以在模型测试时也要进行图片的处理

img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)img = cv2.resize(img,(28,28))

然后是制作标签就是遍历数据集目录下的子目录然后转成01来做标识

def getdata():dir1 = 'E:\\testimage\\image'sub_dir_and_files = os.listdir(dir1)sub_dirs = []# 一共26个文件夹,a-zfor x in sub_dir_and_files:if os.path.isdir(dir1 + '/' + x):sub_dirs.append(x)print(sub_dirs)# 每个文件夹40个训练样本一共1040个# N总图片数量N = 0# 遍历每个文件夹a-zfor subdir in sub_dirs:N += len(os.listdir(dir1 + '/' + subdir))print(N)# X放所有图片X = []# y所有图片的真实内容标签a-zy = [''] * Ni = 0# 遍历每个文件夹读取图片放入Xfor subdir in sub_dirs:image_files = os.listdir(dir1 + '/' + subdir)# print(image_files)for image in image_files:filename = dir1 + '/' + subdir + '/' + image# 图片灰度化img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)# 缩小图片由(1200,900)到(28,28)img = cv2.resize(img, (28, 28), )X.append(img)# label用数字0-25代替字符串a-zy[i] = ord(subdir) - 97i += 1# X,y格式list->numpy.ndarrayX = np.array(X)y = np.array(y)return  X,y

然后就是划分训练集和测试集:

X, y = getdata()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=10)

然后就是归一化

# 归一化X_train = X_train / 255.0X_test = X_test / 255.0X_train = X_train.reshape((728, 28, 28, 1))X_test = X_test.reshape((312, 28, 28, 1))# 将类别向量转换为二进制(只有0和1)的矩阵类型表示(将原有的类别向量转换为独热编码的形式)y_trainOnehot = to_categorical(y_train)y_testOnehot = to_categorical(y_test)

然后建立模型在进行训练,模型中一些参数如卷积核,学习率之类的按个人需要来规划就好

# 建立模型model = Sequential()# 卷积层model.add(Conv2D(filters=256,kernel_size=(5, 5),padding='same',  # 保证卷积核大小,不够补零input_shape=(28, 28, 1),activation='relu'))# 池化层model.add(MaxPool2D(pool_size=(3, 3)))model.add(Dropout(0.25))# 卷积层model.add(Conv2D(filters=256, kernel_size=(5, 5), padding='same', activation='relu'))model.add(Conv2D(filters=256, kernel_size=(5, 5), padding='same', activation='relu'))model.add(MaxPool2D(pool_size=(3, 3)))#正则化去掉1/4的神经元model.add(Dropout(0.25))# 扁平层model.add(Flatten())# 全连接层激活函数relumodel.add(Dense(256, activation='relu'))model.add(Dropout(0.25))# 全连接层激活函数softmaxmodel.add(Dense(26, activation='softmax'))# 输出模型各层的参数状况model.summary()

然后就是训练模型:这里是进行了二十次迭代训练比较慢(个人觉得)

# 训练模型model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])model.fit(X_train, y_trainOnehot, epochs=20)# 返回损失和精度res = model.evaluate(X_test, y_testOnehot)print(model.metrics_names)print(res)

然后是人工测试:

#循环多次测试模型iftest = 'y'while iftest == 'y':xxx,yyy = testdatainout()xxx = (np.expand_dims(xxx, 0))prob = model.predict(xxx)print("预测值:", chr(np.argmax(prob) + 97))#print("真实值:", chr(yyy + 97))#意味着输入的真实值必须是0-23print("真实值:", yyy)iftest = input("是否继续测试:y/n")

测试方法为:

def testdatainout():#这里是选择测试的文件#E:\testimage\image\a\img037-001.pngpath = input("文件路径:")x = []y = input("正确值:")print(path)img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)img = cv2.resize(img,(28,28))x.append(img)x = np.array(x)x = x/255.0x = x.reshape(28, 28, 1)plt.imshow(x)plt.show()return x,y

这一个是小工具类的代码主要是用于得到一个图片数据集的所有图片的路径:

import os
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
import numpy as np
import tensorflow.python.keras as keras
from tensorflow.python.keras import layers
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from sklearn.model_selection import train_test_splitdef test():path = 'E:\\testimage\\image\\a\\img037-001.png'x = []#y = input("正确值(0-25):")print(path)img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)img = cv2.resize(img, (28, 28))x.append(img)x = np.array(x)x = x / 255.0print(x)x = x.reshape(28, 28, 1)print(x)plt.imshow(x)plt.show()
//这一个是做那个图片展示窗口,因为不是展示原图的,所以做了两步处理def geturldata():dir1 = 'E:\\testimage\\image'sub_dir_and_files = os.listdir(dir1)print(sub_dir_and_files)sub_dirs = []for x in sub_dir_and_files:if os.path.isdir(dir1 + '/' + x):sub_dirs.append(dir1 + '\\'+x)print(sub_dirs)imurl = []for i in sub_dirs:if os.path.isdir(i):filename = os.listdir(i)for j in filename:imurl.append(i + '\\' + j)print(imurl)return imurldef savefilename():imgurl = geturldata()path = "E:\\testimage"filename = "filenamedata.txt"if not os.path.exists(path):os.makedirs(path)# 这里是复原文件,既写空fo = open(path + '\\' + filename, "w")fo.truncate()for i in imgurl:fo.write(i)fo.write('\n')fo.close()if __name__ == '__main__':geturldata()savefilename()

效果展示:

主体思路比较简单就是,你先确定模型时图片大小样式,然后是否需要把图片处理一下得到类似该实验中只有一个28*28的01矩阵,这里需要注意通道数问题,就是上面图片灰度化处理,把四通道变成三通道,处理好数据集之后(当然标签在处理数据集时因为循环问题一起完成)就是建立模型选择合适的损失率,卷积核等等,然后在测试模型。

对于其原理的我是参考了书本一些讲解如python机器学习,[美] 塞巴斯蒂安·拉施卡(Sebastian Raschka) 著,陈斌 译,中间几章内容,就是原来不难实现不易

使用tensorflow实现手写字母识别->python相关推荐

  1. 用tensorflow实现手写字母的识别

    用tensorflow 实现手写字母的识别 第一步对一张图片进行预处理 进行预处理,缩小它的大小为28*28像素,并转变为灰度图,进行二值化处理. 代码如下: #include <opencv2 ...

  2. python手写英文识别_pytorch三层全连接层实现手写字母识别方式

    先用最简单的三层全连接神经网络,然后添加激活层查看实验结果,最后加上批标准化验证是否有效 首先根据已有的模板定义网络结构SimpleNet,命名为net.py import torch from to ...

  3. 实战六:手把手教你用TensorFlow进行手写数字识别

    手把手教你用TensorFlow进行手写数字识别 github下载地址 目录 手写体数字MNIST数据集介绍 MNIST Softmax网络介绍 实战MNIST Softmax网络 MNIST CNN ...

  4. 基于tensorflow的手写数字识别

    基于tensorflow的手写数字识别 数据准备 引入包 加载数据 查看数据信息 查看一张图片 数据预处理 搭建网络模型 模型的预测与评价 模型的展示 对一张图片进行预测 准确率 数据准备 引入包 i ...

  5. 使用MATLAB实现基于BP神经网络训练的手写字母识别程序

    前言 大三的时候利用MATLAB搭建了一个基于BP神经网络框架的手写字母识别程序,其中使用了EMNIST数据集进行训练和测试,可实时对手写输入样本进行识别,并返回两个最可能的结果,过程中可继续添加样本 ...

  6. python手写字母识别_机器学习--kNN算法识别手写字母

    本文主要是用kNN算法对字母图片进行特征提取,分类识别.内容如下: kNN算法及相关Python模块介绍 对字母图片进行特征提取 kNN算法实现 kNN算法分析 一.kNN算法介绍 K近邻(kNN,k ...

  7. 基于tensorflow、keras利用emnist数据集构建CNN卷积神经网络进行手写字母识别

    EMNIST 数据集是一个包含手写字母,数字的数据集,它具有和MNIST相同的数据格式.The EMNIST Dataset | NIST 引用模块介绍: import tensorflow as t ...

  8. 利用Tensorflow实现手写数字识别(附python代码)

    手写识别的应用场景有很多,智能手机.掌上电脑的信息工具的普及,手写文字输入,机器识别感应输出:还可以用来识别银行支票,如果准确率不够高,可能会引起严重的后果.当然,手写识别也是机器学习领域的一个Hel ...

  9. lenet5实现手写数字识别---python

    ​​​​​​lenet5实现手写数字识别 一. 导入数据集 下载数据集 查看第一张图片的尺寸 输出数据集规模 可知图片尺寸为28*28像素,通道数为1.训练集包含55000张图片,验证集包含5000张 ...

最新文章

  1. 快速排序的递归和非递归
  2. Jenkins+Pipeline+Docker部署SpringBoot项目到远程服务器
  3. 内核代号101 — 动手写自己的内核
  4. 爆款不是运气:网易产品布局背后的6大标准框架
  5. 字节码分析finally块对return返回值的影响
  6. sqlite3命令大全
  7. beats耳机用安卓手机影响音效么_感受清晰细腻音质,实用有线入耳式耳机推荐...
  8. 了解Callable和Spring DeferredResult
  9. F5 BIGip 负载均衡 IP算法解密工具
  10. 完美解决Mac电脑睡眠之后苹果电脑没有声音的方法
  11. 织梦DeDeCMS友情链接文字显示不全
  12. 《设计模式详解》创建型模式 - 原型模式
  13. docker入门实践之数据卷管理
  14. python 遍历_Python中遍历列表的方法总结
  15. 关于VSCode编码:自动猜测编码字符集
  16. This is a CONNECT tunnel, through which encrypted HTTPS traffic flows.
  17. Java利用dom4j解析XML任意节点和属性
  18. jenkins配置企业微信机器人通知,自定义通知内容
  19. swift脚本编程:一键生成AppIcon
  20. 免费OA办公系统如何获得企业的信赖?

热门文章

  1. 郑州大学计算机学科导论,郑州大学远程教育2017年网上学习导论考试参考答案(2017年第171期)...
  2. 起泡法排序(十个数)
  3. MicroStrategy与Cognos的比较
  4. 研发组织中的“长尾类”问题如何看待和消除?
  5. Matlab中kmeans函数用法
  6. 唐僧师徒的成功之道:四个人,四种秘诀!
  7. 美团外卖全国各类商户数据167万(2018年10月份采集更新)
  8. 关于学生使用 学生证 申请免费的 Jet Brains 软件使用许可 没有教育邮箱/学生邮箱 clion pycharm intellij idea goland phpstorm rider
  9. IDEA学习笔记——文件资源定位图标。小齿轮的显示和隐藏(Autoscroll from Source)
  10. 七日杀服务器修改武器,七日杀物品修改代码大全 七日杀合成表修改教程及代码...