Tesorflow2.0卷積神經網絡實例之Fashion MINST數據集處理

本實例主要參考博客https://blog.csdn.net/cos_lee/article/details/93882520,裏面有數據集的下載鏈接,講解了加載數據集的方法,請大家閱讀,這裏不再贅述。唯一不同的是,此博客採用普通的三層全連接神經網絡處理數據,而本例採用卷積神經網絡。

關於安裝tensorflow,推薦大家使用anaconda navigator下載,用字符界面容易出錯。

本例中使用的Fashion MINST數據是四個.gz文件,如下圖。文件路徑根據實際情況設定。

代碼如下。卷積神經網絡訓練時間很長,請耐心等待。

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import gzip
import math

def get_data():
    # 文件獲取
    train_image = r"E:/Data/fashion_minist/train-images-idx3-ubyte.gz"
    test_image = r"E:/Data/fashion_minist/t10k-images-idx3-ubyte.gz"
    train_label = r"E:/Data/fashion_minist/train-labels-idx1-ubyte.gz"
    test_label = r"E:/Data/fashion_minist/t10k-labels-idx1-ubyte.gz" #文件路徑
    paths = [train_label, train_image, test_label,test_image]

    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)

(train_images, train_labels), (test_images, test_labels) = get_data()

if __name__ == '__main__':
    
    class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
                   'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
    #數據歸一化
    train_images = train_images / 255.0
    test_images = test_images / 255.0
    
    num_train_examples=len(train_images)
    num_test_examples=len(test_images)
    
    '''
    #打印其中一個樣本
    image=test_images[1]
    plt.figure()
    plt.imshow(image,cmap=plt.cm.binary)
    plt.colorbar()
    plt.grid(False)
    plt.show()
    
    #打印前25個樣本
    plt.figure(figsize=(10,10))
    for i in range(25):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        #print(train_images[i])
        plt.imshow(train_images[i], cmap=plt.cm.binary)
        plt.xlabel(class_names[train_labels[i]])
    plt.show()
     '''
    
    #卷積神經網絡 
    model = tf.keras.Sequential([
        #卷積層,輸出結點32,卷積核3*3,邊界補零,激活函數採用relu,
        #輸入格式28*28*1,圖像是28*28像素,1個卷積核,下同
        tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu,
        input_shape=(28, 28, 1)),
        #池化層,池化維度2*2,步長爲2,下同
        tf.keras.layers.MaxPooling2D((2, 2), strides=2),
        tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu),
        tf.keras.layers.MaxPooling2D((2, 2), strides=2),
        #將數據壓爲一維
        tf.keras.layers.Flatten(),
        #最後兩層是全連接層
        tf.keras.layers.Dense(128, activation=tf.nn.relu),
        tf.keras.layers.Dense(10,  activation=tf.nn.softmax)
    ])
    
        
    
    #轉換數據格式,與卷積神經網絡的輸入相適應
    train_reshaped=np.reshape(train_images,newshape=(len(train_images),28,28,1))
    test_reshaped=np.reshape(test_images,newshape=(len(test_images),28,28,1))
    
    #模型編譯
    model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
    
    
    #模型訓練
    model.fit(train_reshaped, train_labels, epochs=50)
    #模型測試
    test_loss, test_acc = model.evaluate(test_reshaped, test_labels)
    print('Test accuracy:', test_acc)
   
    
  

 

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