[Keras] mnist with cnn

典型的卷积神经网络。

  • Keras傻瓜式读取数据:自动下载,自动解压,自动加载。
  • # X_train:
array([[[[ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         ...,
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.]]],

       ..., 

       [[[ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         ...,
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.]]]], dtype=float32)
  • # y_train:
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

但需要二值化作为output:np_utils.to_categorical(y_train, nb_classes)

  • # Y_train:
Y_train[0]
Out[56]: array([ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.])

Y_train[1]
Out[57]: array([ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

Y_train[2]
Out[58]: array([ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.])

Code:

#coding:utf-8

import os
from PIL import Image
import numpy as np

#读取文件夹mnist下的42000张图片,图片为灰度图,所以为1通道,
#如果是将彩色图作为输入,则将1替换为3,并且data[i,:,:,:] = arr改为data[i,:,:,:] = [arr[:,:,0],arr[:,:,1],arr[:,:,2]]
def load_data():
    data = np.empty((42000,1,28,28),dtype="float32")
    label = np.empty((42000,),dtype="uint8")

    imgs = os.listdir("./mnist")
    num = len(imgs)
    for i in range(num):
        img = Image.open("./mnist/"+imgs[i])
        arr = np.asarray(img,dtype="float32")
        data[i,:,:,:] = arr
        label[i] = int(imgs[i].split(‘.‘)[0])
    return data,label

读取原始图片



  Code: a Multilayer Perceptron

import numpy as np
np.random.seed(1337) # for reproducibility

import os
from keras.datasets import mnist    #自动下载

# import 套路
from keras.models import Sequential 
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import RMSprop
from keras.utils import np_utils

batch_size = 128 #Number of images used in each optimization step
nb_classes = 10 #One class per digit
nb_epoch = 12 #Number of times the whole data is used to learn
 (X_train, y_train), (X_test, y_test) = mnist.load_data()

#Flatten the data, MLP doesn‘t use the 2D structure of the data. 784 = 28*28
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)

#Make the value floats in [0;1] instead of int in [0;255] --> [归一化]
X_train = X_train.astype(‘float32‘)
X_test  = X_test.astype(‘float32‘)
X_train /= 255
X_test /= 255

#Display the shapes to check if everything‘s ok
print(X_train.shape[0], ‘train samples‘)
print(X_test.shape[0], ‘test samples‘)

# convert class vectors to binary class matrices (ie one-hot vectors)Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test  = np_utils.to_categorical(y_test, nb_classes)

#Define the model achitecture
model = Sequential()########################################################################################
model.add(Dense(512, input_shape=(784,)))
model.add(Activation(‘relu‘))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation(‘relu‘))
model.add(Dropout(0.2))
model.add(Dense(10)) #Last layer with one output per class
model.add(Activation(‘softmax‘)) #We want a score simlar to a probability for each class
########################################################################################
#Use rmsprop to do the gradient descent see http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf
#and http://cs231n.github.io/neural-networks-3/#ada
rms = RMSprop()
#The function to optimize is the cross entropy between the true label and the output (softmax) of the model
model.compile(loss=‘categorical_crossentropy‘, optimizer=rms, metrics=["accuracy"])

#Make the model learn --> [Training]
model.fit(X_train, Y_train,
batch_size=batch_size, nb_epoch=nb_epoch,
verbose=2,
validation_data=(X_test, Y_test))

#Evaluate how the model does on the test set
score = model.evaluate(X_test, Y_test, verbose=0)

print(‘Test score:‘, score[0])
print(‘Test accuracy:‘, score[1])

Code: a Convolutional Neural Network

import numpy as np
np.random.seed(1337) # for reproducibility

import os
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils

batch_size = 128
nb_classes = 10
nb_epoch = 12

# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

#Add the depth in the input. Only grayscale so depth is only one
#see http://cs231n.github.io/convolutional-networks/#overview
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test  = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

#Make the value floats in [0;1] instead of int in [0;255]
X_train = X_train.astype(‘float32‘)
X_test = X_test.astype(‘float32‘)
X_train /= 255
X_test /= 255

#Display the shapes to check if everything‘s ok
print(‘X_train shape:‘, X_train.shape)
print(X_train.shape[0], ‘train samples‘)
print(X_test.shape[0], ‘test samples‘)

# convert class vectors to binary class matrices (ie one-hot vectors)
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
 ##############################################################################################
model = Sequential()
#For an explanation on conv layers see http://cs231n.github.io/convolutional-networks/#conv
#By default the stride/subsample is 1
#border_mode "valid" means no zero-padding.
#If you want zero-padding add a ZeroPadding layer or, if stride is 1 use border_mode="same"
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
      border_mode=‘valid‘,
      input_shape=(1, img_rows, img_cols)))model.add(Activation(‘relu‘))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation(‘relu‘))
#For an explanation on pooling layers see http://cs231n.github.io/convolutional-networks/#pool
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
#Flatten the 3D output to 1D tensor for a fully connected layer to accept the input
model.add(Flatten())
model.add(Dense(128))
model.add(Activation(‘relu‘))
model.add(Dropout(0.5))
model.add(Dense(nb_classes)) #Last layer with one output per class
model.add(Activation(‘softmax‘)) #We want a score simlar to a probability for each class
###############################################################################################
#The function to optimize is the cross entropy between the true label and the output (softmax) of the model
#We will use adadelta to do the gradient descent see http://cs231n.github.io/neural-networks-3/#ada
model.compile(loss=‘categorical_crossentropy‘, optimizer=‘adadelta‘, metrics=["accuracy"])

#Make the model learn
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))

#Evaluate how the model does on the test set
score = model.evaluate(X_test, Y_test, verbose=0)

print(‘Test score:‘, score[0])
print(‘Test accuracy:‘, score[1])

另一个卷积示例:

#coding:utf-8

‘‘‘
    GPU run command:
        THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cnn.py
    CPU run command:
        python cnn.py
‘‘‘
#导入各种用到的模块组件
from __future__ import absolute_import
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.advanced_activations import PReLU
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from six.moves import range
from data import load_data
import random
import numpy as np

np.random.seed(1024)  # for reproducibility

#加载数据
data, label = load_data()
#打乱数据
index = [i for i in range(len(data))]
random.shuffle(index)
data = data[index]
label = label[index]
print(data.shape[0], ‘ samples‘)

#label为0~9共10个类别,keras要求格式为binary class matrices,转化一下,直接调用keras提供的这个函数
label = np_utils.to_categorical(label, 10)

###############
#开始建立CNN模型
###############

#生成一个model
model = Sequential()

#【第一个卷积层】,4个卷积核,每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。
#border_mode可以是valid或者full,参见这里:http://blog.csdn.net/niuwei22007/article/details/49366745
#激活函数用tanh
#你还可以在model.add(Activation(‘tanh‘))后加上dropout的技巧: model.add(Dropout(0.5))
model.add(Convolution2D(4, 5, 5, border_mode=‘valid‘,input_shape=(1,28,28)))
model.add(Activation(‘tanh‘))

#【第二个卷积层】,8个卷积核,每个卷积核大小3*3。4表示输入的特征图个数,等于上一层的卷积核个数
#激活函数用tanh
#采用maxpooling,poolsize为(2,2)
model.add(Convolution2D(8, 3, 3, border_mode=‘valid‘))
model.add(Activation(‘tanh‘))
model.add(MaxPooling2D(pool_size=(2, 2)))

#【第三个卷积层】,16个卷积核,每个卷积核大小3*3
#激活函数用tanh
#采用maxpooling,poolsize为(2,2)
model.add(Convolution2D(16, 3, 3, border_mode=‘valid‘))
model.add(Activation(‘relu‘))
model.add(MaxPooling2D(pool_size=(2, 2)))
#【全连接层】,先将前一层输出的二维特征图flatten为一维的。
#Dense就是隐藏层。16就是上一层输出的特征图个数。4是根据每个卷积层计算出来的:(28-5+1)得到24,(24-3+1)/2得到11,(11-3+1)/2得到4
#全连接有128个神经元节点,初始化方式为normal
model.add(Flatten())
model.add(Dense(128, init=‘normal‘))
model.add(Activation(‘tanh‘))

#【Softmax分类】,输出是10类别
model.add(Dense(10, init=‘normal‘))
model.add(Activation(‘softmax‘))

#############
#开始训练模型
##############
#使用SGD + momentum
#model.compile里的参数loss就是损失函数(目标函数)
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss=‘categorical_crossentropy‘, optimizer=sgd,metrics=["accuracy"])

#调用fit方法,就是一个训练过程. 训练的epoch数设为10,batch_size为100.
#数据经过随机打乱shuffle=True。verbose=1,训练过程中输出的信息,0、1、2三种方式都可以,无关紧要。show_accuracy=True,训练时每一个epoch都输出accuracy。
#validation_split=0.2,将20%的数据作为验证集。
model.fit(data, label, batch_size=100, nb_epoch=10,shuffle=True,verbose=1,validation_split=0.2)
时间: 2024-08-01 10:46:03

[Keras] mnist with cnn的相关文章

【转载】史上最全:TensorFlow 好玩的技术、应用和你不知道的黑科技

[导读]TensorFlow 在 2015 年年底一出现就受到了极大的关注,经过一年多的发展,已经成为了在机器学习.深度学习项目中最受欢迎的框架之一.自发布以来,TensorFlow 不断在完善并增加新功能,直到在这次大会上发布了稳定版本的 TensorFlow V1.0.这次是谷歌第一次举办的TensorFlow开发者和爱好者大会,我们从主题演讲.有趣应用.技术生态.移动端和嵌入式应用多方面总结这次大会上的Submit,希望能对TensorFlow开发者有所帮助. TensorFlow:面向大

11.1-11.10

图像识别.一.搭建环境基于python的TensorFlow实现1.anaconda的安装参考:https://jingyan.baidu.com/article/f0062228503d2afbd3f0c8fe.html2.可视化工具:jupyter notebook文学编程 有利于数据分析 参考:https://www.jianshu.com/p/97fa4ed3edbc 打开方式:安装完anacoda之后在cmd中输入 jupyter notebook 在浏览器弹出的界面中点击new即可开

初学者的机器学习入门实战教程!

文章来源:   https://www.jianshu.com/p/091b7dc8f12a 这是一篇手把手教你使用 Python 实现机器学习算法,并在数值型数据和图像数据集上运行模型的入门教程,当你看完本文后,你应当可以开始你的机器学习之旅了! 本教程会采用下述两个库来实现机器学习算法: scikit-learn Keras 此外,你还将学习到: 评估你的问题 准备数据(原始数据.特征提取.特征工程等等) 检查各种机器学习算法 检验实验结果 深入了解性能最好的算法 在本文会用到的机器学习算法

利用keras搭建CNN进行mnist数据集分类

当接触深度学习算法的时候,大家都很想自己亲自实践一下这个算法,但是一想到那些复杂的程序,又感觉心里面很累啊,又要学诸如tensorflow.theano这些框架.那么,有没有什么好东西能够帮助我们快速搭建这个算法呢?当然是有咯!,现如今真不缺少造轮子的大神,so,我强烈向大家推荐keras,Keras是一个高层神经网络API,Keras由纯Python编写而成并基Tensorflow或Theano.Keras为支持快速实验而生,能够把你的idea迅速转换为结果. 具体keras的安装与使用,请参

NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)

用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征训练SVMhttp://www.bubuko.com/infodetail-792731.html ./dive_into _keras 自己动手写demo实现

MNIST手写数字图片识别(线性回归、CNN方法的手工及框架实现)(未完待续)

0-Background 作为Deep Learning中的Hello World 项目无论如何都要做一遍的. 代码地址:Github 练习过程中将持续更新blog及代码. 第一次写博客,很多地方可能语言组织不清,请多多提出意见..谢谢~ 0.1 背景知识: Linear regression CNN LeNet-5 AlexNet ResNet VGG 各种regularization方式 0.2 Catalog 1-Prepare 2-MNIST 3-LinearRegression 1-P

对比学习用 Keras 搭建 CNN RNN 等常用神经网络

Keras 是一个兼容 Theano 和 Tensorflow 的神经网络高级包, 用他来组件一个神经网络更加快速, 几条语句就搞定了. 而且广泛的兼容性能使 Keras 在 Windows 和 MacOS 或者 Linux 上运行无阻碍. 今天来对比学习一下用 Keras 搭建下面几个常用神经网络: 回归 RNN回归 分类 CNN分类 RNN分类 自编码分类 它们的步骤差不多是一样的: [导入模块并创建数据] [建立模型] [定义优化器] [激活模型] [训练模型] [检验模型] [可视化结果

keras实现mnist数据集手写数字识别

一. Tensorflow环境的安装 这里我们只讲CPU版本,使用 Anaconda 进行安装 a.首先我们要安装 Anaconda 链接:https://pan.baidu.com/s/1AxdGi93oN9kXCLdyxOMnRA 密码:79ig 过程如下: 第一步:点击next 第二步:I Agree 第三步:Just ME 第四步:自己选择一个恰当位置放它就好 第五步:建议只选择第二个 第六步:就直接install啦啦啦啦,然后你就可以上手万能库了 b.找到Anaconda prompt

Tensorflow中使用CNN实现Mnist手写体识别

本文参考Yann LeCun的LeNet5经典架构,稍加ps得到下面适用于本手写识别的cnn结构,构造一个两层卷积神经网络,神经网络的结构如下图所示: 输入-卷积-pooling-卷积-pooling-全连接层-Dropout-Softmax输出 第一层卷积利用5*5的patch,32个卷积核,可以计算出32个特征.然后进行maxpooling.第二层卷积利用5*5的patch,64个卷积核,可以计算出64个特征.然后进行max pooling.卷积核的个数是我们自己设定,可以增加卷积核数目提高