原理
卷积神经网络参考:https://www.cnblogs.com/chensheng-zhou/p/6380738.html
LeNet神经网络参考:https://my.oschina.net/u/876354/blog/1632862
LeNet的简单介绍
LeNet是一个最典型的卷积神经网络,由卷积层、池化层、全连接层组成。其中卷积层与池化层配合,组成多个卷积组,逐层提取特征,最终通过若干个全连接层完成分类,其结构如下图。
Minist数据集的特点
MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据
Python代码实现
手写字体识别
# coding:utf8
import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
sess = tf.InteractiveSession()
def getTrain():
trai n =[[] ,[]] # 指定训练集的格式,一维为输入数据,一维为其标签
# 读取所有训练图像,作为训练集
train_roo t ="mnist_train"
labels = os.listdir(train_root)
for label in labels:
imgpaths = os.listdir(os.path.join(train_root ,label))
for imgname in imgpaths:
img = cv2.imread(os.path.join(train_root ,label ,imgname) ,0)
array = np.array(img).flatten() # 将二维图像平铺为一维图像
arra y =MaxMinNormalization(array)
train[0].append(array)
label_ = [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0]
label_[int(label)] = 1
train[1].append(label_)
train = shuff(t