"""
mnist识别例子,使用卷积神经网络
"""
import os
import sys
import time
import numpy as np
from keras.datasets import mnist
from keras.optimizers import SGD,RMSprop
from keras.utils import np_utils
from keras.models import Sequential
import keras.backend as K
from keras.models import load_model
from numpy import newaxis
import matplotlib.pyplot as plt
from keras.layers import Dense,Activation,Dropout,Conv2D,Flatten,MaxPool2D,Input,Conv1D
np.random.seed(1337)
reload(sys)
sys.setdefaultencoding('utf-8')
def loadMnistData():
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train=x_train.astype('float32')
x_test = x_test.astype('float32')
y_train=np_utils.to_categorical(y_train,10)
y_test = np_utils.to_categorical(y_test, 10)
print np.shape(x_train), np.shape(x_test)
return x_train,y_train,x_test,y_test
def buildModel(x_train, y_train, x_test, y_test):
print K.image_data_format()
if K.image_data_format()=='channels_first':
x_train = x_train.reshape(60000, 1,28, 28)
x_test = x_test.reshape(10000, 1, 28, 28)
input_shape=(1,28,28)
else:
x_train = x_train.reshape(60000,28, 28,1)
x_test = x_test.reshape(10000,28, 28,1)
input_shape=(28,28,1)
print K.backend(),input_shape
model = Sequential()
model.add(Conv2D(input_shape=input_shape,filters=32,kernel_size=(2,2)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=(3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
print model.output_shape
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(optimizer=RMSprop(),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=15,batch_size=128,verbose=1,validation_data=(x_test,y_test))
score=model.evaluate(x_test,y_test,verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
if __name__=="__main__":
x_train, y_train, x_test, y_test=loadMnistData()
buildModel(x_train, y_train, x_test, y_test)