原始论文中的网络结构如下图:

keras生成的网络结构如下图:

代码如下:

import numpy as np
from keras.preprocessing import image
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras.utils.vis_utils import plot_model
from keras.utils import np_utils # 从文件夹图像与标签文件载入数据
def create_x(filenum, file_dir):
train_x = []
for i in range(filenum):
img = image.load_img(file_dir + str(i) + ".bmp", target_size=(28, 28))
img = img.convert('L')
x = image.img_to_array(img)
train_x.append(x)
train_x = np.array(train_x)
train_x = train_x.astype('float32')
train_x /= 255
return train_x def create_y(classes, filename):
train_y = []
file = open(filename, "r")
for line in file.readlines():
train_y.append(int(line))
file.close()
train_y = np.array(train_y).astype('float32')
train_y = np_utils.to_categorical(train_y, classes)
return train_y classes = 10 X_train = create_x(55000, './train/')
X_test = create_x(10000, './test/') Y_train = create_y(classes, 'train.txt')
Y_test = create_y(classes, 'test.txt') # 从网络下载的数据集直接解析数据
'''
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST/", one_hot=True)
X_train, Y_train = mnist.train.images, mnist.train.labels
X_test, Y_test = mnist.test.images, mnist.test.labels
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
print(X_train.shape, X_test.shape)
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
print(X_train[0])
'''
model = Sequential()
model.add(Conv2D(filters=6, kernel_size=(5, 5), padding='valid', input_shape=(28, 28, 1), activation='tanh')) #C1
model.add(MaxPooling2D(pool_size=(2, 2)))    #S2
model.add(Conv2D(filters=16, kernel_size=(5, 5), padding='valid', activation='tanh'))  #C3
model.add(MaxPooling2D(pool_size=(2, 2)))    #S4
model.add(Flatten())
model.add(Dense(120, activation='tanh'))    #C5
model.add(Dense(84, activation='tanh'))    #F6
model.add(Dense(10, activation='softmax'))  #output
model.summary() model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
history = model.fit(X_train, Y_train, batch_size=500, epochs=50, verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0) test_result = model.predict(X_test)
result = np.argmax(test_result, axis=1) print(result)
print('Test score:', score[0])
print('Test accuracy:', score[1]) plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=False)

50次迭代,识别率在97%左右:

相关测试数据可以在这里下载到。

【Python】keras使用Lenet5识别mnist的更多相关文章

  1. Python实现bp神经网络识别MNIST数据集

    title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...

  2. 【Python】keras卷积神经网络识别mnist

    卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...

  3. Python+Keras+TensorFlow车牌识别

    这个是我使用的车牌识别开源项目的地址:https://github.com/zeusees/HyperLPR Python 依赖 Anaconda for Python 3.x on Win64 Ke ...

  4. 【Python】keras神经网络识别mnist

    上次用Matlab写过一个识别Mnist的神经网络,地址在:https://www.cnblogs.com/tiandsp/p/9042908.html 这次又用Keras做了一个差不多的,毕竟,现在 ...

  5. keras框架的MLP手写数字识别MNIST,梳理?

    keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...

  6. 数据挖掘入门系列教程(十一)之keras入门使用以及构建DNN网络识别MNIST

    简介 在上一篇博客:数据挖掘入门系列教程(十点五)之DNN介绍及公式推导中,详细的介绍了DNN,并对其进行了公式推导.本来这篇博客是准备直接介绍CNN的,但是想了一下,觉得还是使用keras构建一个D ...

  7. 机器学习(1) - TensorflowSharp 简单使用与KNN识别MNIST流程

    机器学习是时下非常流行的话题,而Tensorflow是机器学习中最有名的工具包.TensorflowSharp是Tensorflow的C#语言表述.本文会对TensorflowSharp的使用进行一个 ...

  8. RNN入门(一)识别MNIST数据集

    RNN介绍   在读本文之前,读者应该对全连接神经网络(Fully Connected Neural Network, FCNN)和卷积神经网络( Convolutional Neural Netwo ...

  9. TensorFlow 之 手写数字识别MNIST

    官方文档: MNIST For ML Beginners - https://www.tensorflow.org/get_started/mnist/beginners Deep MNIST for ...

随机推荐

  1. oracle中常见的查询操作

    普通查询:select * from t; 去除重复值:select distinct f1,f2 from t; between用法:select * from t where f1 not/bet ...

  2. 如何开始DDD

    在开始DDD之前,你需要了解DDD的一些基础知识,聚合(AggregateRoot).实体(Entity).值对象(ValueObject),工厂(Factory),仓储(Repository)和领域 ...

  3. 修改Spring Boot默认的上下文

    前言 默认情况下,Spring Boot使用的服务上下文为"/",我们可以通过"http://localhost:PORT/" 直接诶访问应用: 但是在生产环境 ...

  4. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十七):登录验证码实现(Captcha)

    登录验证码 登录验证是一般系统都会有的功能,验证的方式也多种多样,比如输入式验证码,拖动式验证条,拖动式验证拼图等等. 我们这里先实现常规的输入验证码的方式,右边显示验证码图片,点击可刷新,左边输入验 ...

  5. 使用 Linux 自带的 logrotate 程序来控制日志文件尺寸

    1. 编写配置文件,内容如下(以 Amadeus 系统为例): 编写配置文件,放在 /etc/logrotate.d/xxxx 下,其中 xxxx 是自己取的名字,无需后缀.例如 Amadeus 系统 ...

  6. ES启动报错之引导检测失败

    [--16T18::,][ERROR][o.e.b.Bootstrap ] [node-] node validation exception [] bootstrap checks failed [ ...

  7. ASP.NET MVC5 + EF6 + LayUI实战教程,通用后台管理系统框架(3)

    前言 本节将我们自己的CSS样式替换系统自带的 开始搭建 将脚本文件夹删掉,将内容文件夹里的内容删掉,将我们自己的CSS样式文件,全部复制到内容里边 新建家庭控制器 给家庭控制器添加索引视图 指数代码 ...

  8. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  9. 【IDEA&&Eclipse】5、IntelliJ IDEA常见配置

    [idea配置jdk] http://blog.csdn.net/tolcf/article/details/50803414 [idea intellij 如何配置tomcat]http://jin ...

  10. Matlab Euler's method

    % matlab script to test efficiency of % Euler's method, classical Runge-Kutta, and ode45 % on Arenst ...