mnist識別優化——使用新的fashion mnist進行模型訓練

今天經過論壇偶然知道,在mnist以後,還出現了一個旨在代替經典mnist數據集的Fashion MNIST,同mnist同樣,它也是被用做深度學習程序的「hello world」,並且也是由70k張28*28的圖片組成的,它們也被分爲10類,有60k被用做訓練,10k被用做測試。惟一的區別就是,fashion mnist的十種類別由手寫數字換成了服裝。這十種類別以下:網絡

'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'函數

設計流程以下:學習

  · 首先獲取數據集,tensorflow獲取fashion mnist的方法和mnist相似,使用keras.datasets.fashion_mnist.load_data()便可測試

  · 將數據集劃分爲訓練集和測試集優化

  · 因爲圖片像素值範圍是0-255,將數據集進行預處理,把像素值縮放到0到1的範圍(即除以255)this

  · 搭建網絡模型 (784→128(relu)→10(softmax)),全鏈接lua

  · 編譯模型,設計損失函數(對數損失)、優化器(adam)以及訓練指標(accuracy)spa

  · 訓練模型.net

  · 評估準確性(測試數據使用matplotlib進行可視化)設計

 

關於Adam優化器的來源和特色請參考:https://www.jianshu.com/p/aebcaf8af76e

關於matplotlib數據可視化請參考:http://www.javashuo.com/article/p-dkxsxmvg-db.html

 

訓練集部分數據可視化以下:

 

一共作了50輪訓練,訓練開始時的損失和精度以下:

 

 訓練完成時的損失和精度以下:

 

 模型在測試集上的表現以下:

 

 選擇測試集某張圖片的預測可視化結果以下:

 

 程序代碼以下:

 1 import tensorflow as tf  2 from tensorflow import keras  3 import numpy as np  4 import matplotlib.pyplot as plt  5 
 6 # 導入fashion mnist數據集
 7 fashion_mnist = keras.datasets.fashion_mnist  8 (train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()  9 
 10 # 衣服類別
 11 class_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal',  12                'Shirt','Sneaker','Bag','Ankle boot']  13 print(train_images.shape,len(train_labels))  14 print(test_images.shape,len(test_labels))  15 
 16 # 查看圖片
 17 plt.figure()  18 plt.imshow(train_images[0])  19 plt.colorbar()  20 plt.grid(False)  21 plt.show()  22 
 23 # 預處理數據,將像素值除以255,使其縮放到0到1的範圍
 24 train_images = train_images / 255.0
 25 test_images = test_images / 255.0
 26 
 27 # 驗證數據格式的正確性,顯示訓練集前25張圖像並註明類別
 28 plt.figure(figsize=(10,10))  29 for i in range(25):  30     plt.subplot(5,5,i+1)  31  plt.xticks([])  32  plt.yticks([])  33  plt.grid(False)  34     plt.imshow(train_images[i],cmap=plt.cm.binary)  35  plt.xlabel(class_names[train_labels[i]])  36 plt.show()  37 
 38 # 搭建網絡結構
 39 model = keras.Sequential([  40     keras.layers.Flatten(input_shape=(28,28)),  41     keras.layers.Dense(128,activation='relu'),  42     keras.layers.Dense(10,activation='softmax')  43 ])  44 
 45 # 設置損失函數、優化器及訓練指標
 46 model.compile(  47     optimizer='adam',  48     loss='sparse_categorical_crossentropy',  49     metrics=['accuracy']  50 )  51 
 52 # 訓練模型
 53 model.fit(train_images,train_labels,epochs=50)  54 
 55 # 模型評估
 56 test_loss,test_acc=model.evaluate(test_images,test_labels,verbose=2)  57 print('/nTest accuracy:',test_acc)  58 
 59 # 選擇測試集中的圖像進行預測
 60 predictions=model.predict(test_images)  61 
 62 # 查看第一個預測
 63 print("預測結果:",np.argmax(predictions[0]))  64 # 將正確標籤打印出來和預測結果對比
 65 print("真實結果:",test_labels[0])  66 
 67 # 以圖形方式查看完整的十個類的預測
 68 def plot_image(i,predictions_array,true_label,img):  69     predictions_array,true_label,img=predictions_array,true_label[i],img[i]  70  plt.grid(False)  71  plt.xticks([])  72  plt.yticks([])  73 
 74     plt.imshow(img,cmap=plt.cm.binary)  75 
 76     predicted_label=np.argmax(predictions_array)  77     if predicted_label==true_label:  78         color='blue'
 79     else:  80         color='red'
 81 
 82     plt.xlabel("{}{:2.0f}%({})".format(class_names[predicted_label],  83                                        100*np.max(predictions_array),  84  class_names[true_label]),  85                                        color=color)  86 
 87 def plot_value_array(i,predictions_array,true_label):  88     predictions_array,true_label=predictions_array,true_label[i]  89  plt.grid(False)  90     plt.xticks(range(10))  91  plt.yticks([])  92     thisplot=plt.bar(range(10),predictions_array,color="#777777")  93     plt.ylim([0,1])  94     predicted_label=np.argmax(predictions_array)  95 
 96     thisplot[predicted_label].set_color('red')  97     thisplot[true_label].set_color('blue')  98 
 99 i=10
100 plt.figure(figsize=(6,3)) 101 plt.subplot(1,2,1) 102 plot_image(i,predictions[i],test_labels,test_images) 103 plt.subplot(1,2,2) 104 plot_value_array(i,predictions[i],test_labels) 105 plt.show()
相關文章
相關標籤/搜索