1 读取本地MNIST数据,训练,保存模型
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 9 21:10:34 2020
@author: tianx
"""
import numpy as np
import os
import gzip
import keras
from keras.models import Sequential # 导入序贯模型,可以通过顺序的方式,叠加神经网络层
from keras.layers import Dense
from keras import optimizers
from keras.optimizers import SGD # 导入优化函数
# 定义加载数据的函数,data_folder为保存gz数据的文件夹,该文件夹下有4个文件
# 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
# 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
def load_data(data_folder):
files = [
'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]
paths = []
for fname in files:
paths.append(os.path.join(data_folder,fname))
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
(x_train,y_train), (x_test, y_test) = load_data('D:/Data/Mnist/MNIST/raw/')
print(x_train.shape,y_train.shape) # 60000张28*28的单通道灰度图
print(x_test.shape,y_test.shape)
import matplotlib.pyplot as plt # 导入可视化的包
im = plt.imshow(x_train[0],cmap='gray')
plt.show()
y_train[0]
x_train = x_train.reshape(60000,784) # 将图片摊平,变成向量
x_test = x_test.reshape(10000,784) # 对测试集进行同样的处理
x_train = x_train / 255
x_test = x_test / 255
y_train = keras.utils.to_categorical(y_train,10)
y_test = keras.utils.to_categorical(y_test,10)
model = Sequential() # 构建一个空的序贯模型
# 添加神经网络层
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(Dense(256,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.summary()
model.compile(optimizer=SGD(),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=64,epochs=1,validation_data=(x_test,y_test)) # 此处直接将测试集用作了验证集
score = model.evaluate(x_test,y_test)
print("loss:",score[0])
print("accu:",score[1])
# 保存模型
path = "D:/Data/Model/model_file_path.h5"
model.save(path)
# 取一个测试集数据
test_data=x_test[0]
test_data=test_data[np.newaxis,:]
test_data=np.tile(test_data,[64,1])
#test_data=test_data.reshape(1,784)
#test_28=test_data.reshape(28,28)
#plt.imshow(test_28,cmap='gray')
#plt.show()
# 预测结果
result=model.predict(test_data)
result=result.tolist()
#print(result[0].index(max(result[0])
print(result[0].index(max(result[0])))
# model_save_path = "model_file_path.h5"
# -*- coding: utf-8 -*-