图像处理之卷积神经网络手写体识别

   之前经常使用tensorflow 和pytorch 搭建GPU图像识别系统,那速度不是一般的快,后来又放到JestonNano中实现了边缘计算垃圾分拣。两年前好像一直用的时tf1.14 numpy1.15什么的。今天又有问到tf 的 helloworld 程序 构建手写体。我在网上github 下了个例子,准备构建个环境执行下,也折腾了一个多小时才成功。
   首先我先安装了tf,现在默认安装是2.7.安装后我就感觉最新版肯定不好用,就谢了,在清华镜像指定安装了2.3版本pip install tensorflow==2.3 -i ,https://pypi.tuna.tsinghua.edu.cn/simple。这版本也不低了,其他numpy matplotlib 都自动。我开始以为cpu版的通常安装非常简单,安装完就能用。首先安装tf要先安装vc_redist.x64.exe,或者安装vs2015,有其他版本还很可能装不上,要卸载掉才能装,第二,安装完后居然需要cudart64_101,我去官网下载了这个小动态链接库https://www.dll-files.com/cudart64_101.dll.html,官网并不稳定,我翻墙下载的。之后放到system32下面,第一个 import tensorflow 的问题解决了。开始调试时我先执行了测试集的导入测试程序。https://blog.csdn.net/qq_43060552/article/details/103189040为了解决No module named ‘tensorflow.examples.tutorials 的问题。

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets ( ‘MNIST_data/’, one_hot=True)
这两行代码课以自动下载 mnist数据集。这个例子是将tf 中examples 目录下 MINST tutorial 下载到当前工作目录,但是如果你tf 没有tutorial 就会提示 No module…这时可以自行下载tutorial 放在你所在环境的lib 下 tensorflow\examples,打开后就知道怎么放置下载的tutorials了。也可以用keras 自动下载数据集
import tensorflow as tf
tf.version
mint=tf.keras.datasets.mnist
(x_,y_),(x_1,y_1)=mint.load_data()
import matplotlib.pyplot as plt
plt.imshow(x_[0], cmap=“binary”)
plt.show()
这代码没有装keras也能运行成功,数据集下载到C盘你的用户名下.keras下的datasets. 不过下载的扩展名是npz,复制到工作目录无法读入,可以查看https://zhuanlan.zhihu.com/p/163203298
最终我还是把下载tuorial 放在examples比较方便。https://download.csdn.net/download/buaaweibin/40844865
这时可以运行以下代码了
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(“MNIST_data”, one_hot=True)
print ( ’ 输入数据:’, mnist.train.images)
print ( ’ 输入数据打shape :’, mnist.train.images.shape)
import pylab
im = mnist.train.images[1]
im = im.reshape(-1 ,28)
pylab.imshow(im)
pylab.show()
在这里插入图片描述
下一步要构建神经网络了,
https://github.com/zhuangchen-nlp/mnist-tf
这里边两个py 一个是传统的,一个是卷积神经网络。

因为大部分tf的例子是1.1时代的,所以前面还要有个兼容性设置。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

这些设置好后,采用什么程序都不会有问题了
这里有个比较全面的程序,还涉及到最后的手写识别过程,可以参考https://www.cnblogs.com/XDU-Lakers/p/10526748.html
基于keras的
https://zhuanlan.zhihu.com/p/158907160

猜你喜欢

转载自blog.csdn.net/buaaweibin/article/details/121297832
今日推荐