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

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. 从零开始学 Spring Boot

    1.下载 spring-tool-suite https://spring.io/tools3/sts/legacy 2.解压运行 sts-bundle\sts-3.9.7.RELEASE\STS.e ...

  2. HDU 6022---MG loves set(K-D树)

    题目链接 Problem Description MG is an intelligent boy. One day he was challenged by the famous master ca ...

  3. 国外程序员整理的 C++ 资源大全 (zt)

    关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...

  4. Windows抓屏技术

    Windows桌面共享中一些常见的抓屏技术 1. BitBlt   我想做Windows开发应该都知道这个API, 它能实现DC间的内容拷贝, 如果我们把源DC指定成Monitor DC或是桌面DC, ...

  5. 深度学习论文翻译解析(四):Faster R-CNN: Down the rabbit hole of modern object detection

    论文标题:Faster R-CNN: Down the rabbit hole of modern object detection 论文作者:Zhi Tian , Weilin Huang, Ton ...

  6. vscode使用汇总——常用插件、常用配置、常用快捷键

    一.代码提示快捷键设置:(keybindings.json) [ { "key": "ctrl+j", "command": "- ...

  7. SpringBoot(2) Json框架 -- Jackson返回结果处理

    一.常用框架 阿里 fastjson,谷歌gson等 JavaBean序列化为Json,性能:Jackson > FastJson > Gson > Json-lib 同个结构 Ja ...

  8. [NOI 2017]游戏

    Description 题库链接 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 A.B.C 表示.地图一共 ...

  9. WebForm 【Repeater】展示数据

       在 Webform 数据展示中      界面层  : HTLM 业务逻辑层 :只能用 C#  Repeater    重复器  能够用来循环展示数据 具有5种模板  HeaderTemplat ...

  10. Windows环境使用Nexus-3.x搭建Maven私服

    [前言] 本文主要讲解在Wiindows环境下搭建最新出的Nexus 3.x私服. 1.搭建私服的必要性 一般情况下,各个公司的开发团队为了提高开发效率,都会使用项目构建工具进行开发.常见的构建工具有 ...