kears編寫CNN網絡,實現對mnist的識別

訓練代碼:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 13 09:18:19 2020

@author: tianx
"""
import numpy as np
import os
import gzip
import keras
from keras.models import Sequential # 導入序貫模型,可以通過順序的方式,疊加神經網絡層
from keras.layers import Dense,Flatten,MaxPool2D

from keras import optimizers
from keras.optimizers import SGD # 導入優化函數

from keras.models import Sequential,Model
from keras import layers,Input
from keras.utils import plot_model


# 定義加載數據的函數,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[:,:,:,np.newaxis]

x_test=x_test[:,:,:,np.newaxis]
#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)  # 轉換成one-hot格式
y_test = keras.utils.to_categorical(y_test,10)

# 定義網絡模型
input_tensor=Input(shape=(28,28,1))
x=layers.Conv2D(32,(3,3),activation='relu')(input_tensor)
x=MaxPool2D((2,2),name='pool1')(x)
x=layers.Conv2D(64,3,activation='relu')(x)
x=MaxPool2D((2,2),name='pool2')(x)
x=Flatten()(x)
x=layers.Dense(1000,activation='relu')(x)
out_tensor=layers.Dense(10,activation='softmax')(x)


model=Model(input_tensor,out_tensor)
model.summary()
#plot_model(model,to_file='convolutional_neural_network.png')
path = "D:/Data/Model/model_mnist_cnn.h5"
model.save(path)


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])



預測代碼:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 13 10:45:28 2020

@author: tianx
"""
# -*- 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_mnist_cnn.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=test_data[np.newaxis,:,:,:]

test_data=np.tile(test_data,[64,1,1,1]) # 在0維度上覆制64份,擬合網絡的輸入
 
# 預測結果
result=model.predict(test_data)
print(np.argmax(result[0]))

結果:

參考博客:

函數式API變成(超詳細)

https://blog.csdn.net/ting0922/article/details/94437540

 

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