Keras BP神经网络运用于手写数字识别

Keras 手写数字识别

导入相关的包

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.optimizers import SGD,Adam
from keras.regularizers import l2

载入数据

直接从keras下载可能会比较慢,这里事先准备好了数据集

链接:https://pan.baidu.com/s/1kbDiH-nnbgmTRdaZM6c80g
提取码:sg2k

#载入数据
# (x_train,y_train),(x_test,y_test) = mnist.load_data() 
#自己准备好数据集,并存放在当前目录的文件夹下
path = 'mnist.npz'
def downloadmnist(path):
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train,y_train),(x_test,y_test)
(x_train,y_train),(x_test,y_test) = downloadmnist(path)
#(60000,28,28)
print('x_shape',x_train.shape)
#(60000,)
print('y_shape',y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0],784)/255.0  #归一化处理
x_test = x_test.reshape(x_test.shape[0],784)/255.0
#换one hot 格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)

模型构建

这里构建784-100-10的神经网络,采用adam优化器效果会好一些,这里的数据比较简单,可以不采用正则化

784-100-10
model = Sequential([Dense(units=200,input_dim=784,activation='tanh',kernel_regularizer=l2(0.0003))])  #softmax放在最后一层
# model.add(Dropout(0.4))
model.add(Dense(units=100,activation='tanh',kernel_regularizer=l2(0.0003)))
# model.add(Dropout(0.4))
model.add(Dense(units=10,activation='softmax',kernel_regularizer=l2(0.0003)))
adam = Adam(lr=0.001)
sgd = SGD(lr=0.3)
model.compile(optimizer=adam , loss='categorical_crossentropy' , metrics=['accuracy'])
#model.compile(optimizer=sgd , loss='mse' , metrics=['accuracy'])

训练模型

model.fit(x_train,y_train,batch_size=32,epochs=10)

评估模型

#评估模型
test_loss,test_accuracy = model.evaluate(x_test,y_test)
print('loss:',test_loss)
print('accuracy:',test_accuracy)

train_loss,train_accuracy = model.evaluate(x_train,y_train)
print('loss:',train_loss)
print('accuracy:',train_accuracy)

预测结果

test_loss: 0.18031258141994477
test_accuracy: 0.9728999733924866

train_loss: 0.15471795084079107
train_accuracy: 0.9812833070755005

​测试结果

#测试
from PIL import Image
import matplotlib.pyplot as plt
import os


img = Image.open('image/77.jpg').convert('L')
print(img)
plt.imshow(img)
plt.axis('off')
plt.show()

# resize的过程
if img.size[0] != 28 or img.size[1] != 28:
    img = img.resize((28, 28))
    
arr = []
for i in range(28):
    for j in range(28):
        # mnist 里的颜色是0代表白色(背景),1.0代表黑色
        pixel = 1.0 - float(img.getpixel((j, i)))/255.0
        # pixel = 255.0 - float(img.getpixel((j, i))) # 如果是0-255的颜色值
        arr.append(pixel)
arr1 = np.array(arr).reshape((1, 784))

prediction=model.predict(arr1)
print("prediction:",np.argmax(prediction[0]))

Keras BP神经网络运用于手写数字识别_第1张图片

你可能感兴趣的:(深度学习)