【Python】keras使用Lenet5识别mnist
原始论文中的网络结构如下图:

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的更多相关文章
- Python实现bp神经网络识别MNIST数据集
title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...
- 【Python】keras卷积神经网络识别mnist
卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...
- Python+Keras+TensorFlow车牌识别
这个是我使用的车牌识别开源项目的地址:https://github.com/zeusees/HyperLPR Python 依赖 Anaconda for Python 3.x on Win64 Ke ...
- 【Python】keras神经网络识别mnist
上次用Matlab写过一个识别Mnist的神经网络,地址在:https://www.cnblogs.com/tiandsp/p/9042908.html 这次又用Keras做了一个差不多的,毕竟,现在 ...
- keras框架的MLP手写数字识别MNIST,梳理?
keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...
- 数据挖掘入门系列教程(十一)之keras入门使用以及构建DNN网络识别MNIST
简介 在上一篇博客:数据挖掘入门系列教程(十点五)之DNN介绍及公式推导中,详细的介绍了DNN,并对其进行了公式推导.本来这篇博客是准备直接介绍CNN的,但是想了一下,觉得还是使用keras构建一个D ...
- 机器学习(1) - TensorflowSharp 简单使用与KNN识别MNIST流程
机器学习是时下非常流行的话题,而Tensorflow是机器学习中最有名的工具包.TensorflowSharp是Tensorflow的C#语言表述.本文会对TensorflowSharp的使用进行一个 ...
- RNN入门(一)识别MNIST数据集
RNN介绍 在读本文之前,读者应该对全连接神经网络(Fully Connected Neural Network, FCNN)和卷积神经网络( Convolutional Neural Netwo ...
- TensorFlow 之 手写数字识别MNIST
官方文档: MNIST For ML Beginners - https://www.tensorflow.org/get_started/mnist/beginners Deep MNIST for ...
随机推荐
- HoloLens开发手记 - HoloLens shell概述 HoloLens shell overview
使用HoloLens时,shell是由你周围的世界和来自系统的全息图像构成.我们将这种空间成为混合世界(mixed world). shell包含了一个可以让你将全息图像和应用放置在世界中的开始菜单( ...
- 1ink 与 @import 的区别
1ink与@import的区别 目录 1ink与@import的区别 差别1:归属关系的差别 差别2:加载顺序的差别 差别3:兼容性的差别 差别4:使用dom控制样式时的差别 1ink与@import ...
- iptables防DDOS攻击和CC攻击设置
防范DDOS攻击脚本 #防止SYN攻击 轻量级预防 iptables -N syn-flood iptables -A INPUT -p tcp --syn -j syn-flood iptables ...
- 201. Orchard学习 一、基础
一.项目介绍 Orchard是一个免费和开源的社区交流项目,致力于在ASP.NET平台开发应用程序和可重用性组件.它将创建用于ASP.Net应用和扩展的共享组件,以及修改这些组件以便使其应用于终端用户 ...
- simhash文章排重
原文链接:https://www.cnblogs.com/baochuan/p/9089244.html 背景 提升产品体验,节省用户感知度.——想想,如果看到一堆相似性很高的新闻,对于用户 ...
- Google Optimization Tools实现加工车间任务规划【Python版】
上一篇介绍了<使用.NET Core与Google Optimization Tools实现加工车间任务规划>,这次将Google官方文档python实现的版本的完整源码献出来,以满足喜爱 ...
- Vim 利剑常磨,见血封喉
年底了,故事总是会有很多. 刚了一波通宵加班,趁着有时间,过了一遍Vim教程,顺便汇总下常用命令. 对于以 OSX / Linux为开发环境的伙伴们,应该并不陌生.因其轻便,扩展性,可定制化,一直很受 ...
- 手把手教你学会用Spring AOP
用了Spring很长时间了,一直想写些AOP的东西,但一直没有空闲,直到现在项目稍微进入正轨了,才赶紧写写.废话不多说,先从AOP入门开始,后面再介绍AOP的原理(JDK动态代码和CGLIB动态代码的 ...
- Java并发编程笔记之StampedLock锁源码探究
StampedLock是JUC并发包里面JDK1.8版本新增的一个锁,该锁提供了三种模式的读写控制,当调用获取锁的系列函数的时候,会返回一个long 型的变量,该变量被称为戳记(stamp),这个戳记 ...
- REX:EOS资源租赁平台详解
关键字:REX,资源交易,资源租赁,系统费用,bancor,成熟期,EOS,eosio.system,voting EOSIO 智能合约在v1.6.0版本增加了一个system合约使用的例子,可提供E ...