MNIST手寫數據,從訓練到數據預測(keras)

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"




 

 2 對單個MNIST數據進行預測

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 14:22:09 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 # 導入優化函數
from keras.models import load_model

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/')
# 模型的路徑
path = "D:/Data/Model/model_file_path.h5"
加載模型
model = load_model(path)

# 取一個測試集數據
test_data=x_test[0] 
test_data=test_data[np.newaxis,:] # 增加一個維度
test_data=test_data.reshape(1,784) # 改變形狀
test_data=np.tile(test_data,[64,1]) # 在0維度上覆制64份,擬合網絡的輸入

# 預測結果
result=model.predict(test_data)
print(np.argmax(result[0]))

參考博客:

01 本地加載MNIST數據:

https://www.cnblogs.com/ypzhai/p/9997856.html

02 模型的保存和加載:

https://www.cnblogs.com/Mrzhang3389/p/10746300.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章