官網示例:https://www.tensorflow.org/tutorials/keras/basic_classification
主要步驟:python
若是機器學習模型在新數據上的表現不如在訓練數據上的表現,就表示出現過擬合git
GitHub:https://github.com/anliven/Hello-AI/blob/master/Google-Learn-and-use-ML/1_basic_classification.pygithub
1 # coding=utf-8 2 import tensorflow as tf 3 from tensorflow import keras 4 import numpy as np 5 import matplotlib.pyplot as plt 6 import os 7 8 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 9 print("TensorFlow version: {} - tf.keras version: {}".format(tf.VERSION, tf.keras.__version__)) # 查看版本 10 11 # ### 加載數據集 12 # 網絡暢通的狀況下,能夠從 TensorFlow 直接訪問 Fashion MNIST,只需導入和加載數據便可 13 # 或者手工下載文件,並存放在「~/.keras/datasets」下的fashion-mnist目錄 14 fashion_mnist = keras.datasets.fashion_mnist 15 (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() 16 # 訓練集:train_images 和 train_labels 數組,用於學習的數據 17 # 測試集:test_images 和 test_labels 數組,用於測試模型 18 # 圖像images爲28x28的NumPy數組,像素值介於0到255之間 19 # 標籤labels是整數數組,介於0到9之間,對應於圖像表明的服飾所屬的類別,每張圖像都映射到一個標籤 20 21 class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 22 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] # 類別名稱 23 24 # ### 探索數據:瞭解數據格式 25 print("train_images.shape: {}".format(train_images.shape)) # 訓練集中有60000張圖像,每張圖像都爲28x28像素 26 print("train_labels len: {}".format(len(train_labels))) # 訓練集中有60000個標籤 27 print("train_labels: {}".format(train_labels)) # 每一個標籤都是一個介於 0 到 9 之間的整數 28 print("test_images.shape: {}".format(test_images.shape)) # 測試集中有10000張圖像,每張圖像都爲28x28像素 29 print("test_labels len: {}".format(len(test_labels))) # 測試集中有10000個標籤 30 print("test_labels: {}".format(test_labels)) 31 32 # ### 預處理數據 33 # 必須先對數據進行預處理,而後再訓練網絡 34 plt.figure(num=1) # 建立圖形窗口,參數num是圖像編號 35 plt.imshow(train_images[0]) # 繪製圖片 36 plt.colorbar() # 漸變色度條 37 plt.grid(False) # 顯示網格 38 plt.savefig("./outputs/sample-1-figure-1.png", dpi=200, format='png') # 保存文件,必須在plt.show()前使用,不然將是空白內容 39 plt.show() # 顯示 40 plt.close() # 關閉figure實例,若是要建立多個figure實例,必須顯示調用close方法來釋放再也不使用的figure實例 41 42 # 值縮小爲0到1之間的浮點數 43 train_images = train_images / 255.0 44 test_images = test_images / 255.0 45 46 # 顯示訓練集中的前25張圖像,並在每張圖像下顯示類別名稱 47 plt.figure(num=2, figsize=(10, 10)) # 參數figsize指定寬和高,單位爲英寸 48 for i in range(25): # 前25張圖像 49 plt.subplot(5, 5, i + 1) 50 plt.xticks([]) # x座標軸刻度 51 plt.yticks([]) # y座標軸刻度 52 plt.grid(False) 53 plt.imshow(train_images[i], cmap=plt.cm.binary) 54 plt.xlabel(class_names[train_labels[i]]) # x座標軸名稱 55 plt.savefig("./outputs/sample-1-figure-2.png", dpi=200, format='png') 56 plt.show() 57 plt.close() 58 59 # ### 構建模型 60 # 構建神經網絡須要先配置模型的層,而後再編譯模型 61 # 設置層 62 model = keras.Sequential([ 63 keras.layers.Flatten(input_shape=(28, 28)), # 將圖像格式從二維數組(28x28像素)轉換成一維數組(784 像素) 64 keras.layers.Dense(128, activation=tf.nn.relu), # 全鏈接神經層,具備128個節點(或神經元) 65 keras.layers.Dense(10, activation=tf.nn.softmax)]) # 全鏈接神經層,具備10個節點的softmax層 66 # 編譯模型 67 model.compile(optimizer=tf.train.AdamOptimizer(), # 優化器:根據模型看到的數據及其損失函數更新模型的方式 68 loss='sparse_categorical_crossentropy', # 損失函數:衡量模型在訓練期間的準確率。 69 metrics=['accuracy']) # 指標:用於監控訓練和測試步驟;這裏使用準確率(圖像被正確分類的比例) 70 71 # ### 訓練模型 72 # 將訓練數據饋送到模型中,模型學習將圖像與標籤相關聯 73 model.fit(train_images, # 訓練數據 74 train_labels, # 訓練數據 75 epochs=5, # 訓練週期(訓練模型迭代輪次) 76 verbose=2 # 日誌顯示模式:0爲安靜模式, 1爲進度條(默認), 2爲每輪一行 77 ) # 調用model.fit 方法開始訓練,使模型與訓練數據「擬合 78 79 # ### 評估準確率 80 # 比較模型在測試數據集上的表現 81 test_loss, test_acc = model.evaluate(test_images, test_labels) 82 print('Test loss: {} - Test accuracy: {}'.format(test_loss, test_acc)) 83 84 # ### 作出預測 85 predictions = model.predict(test_images) # 使用predict()方法進行預測 86 print("The first prediction: {}".format(predictions[0])) # 查看第一個預測結果(包含10個數字的數組,分別對應10種服飾的「置信度」 87 label_number = np.argmax(predictions[0]) # 置信度值最大的標籤 88 print("label: {} - class name: {}".format(label_number, class_names[label_number])) 89 print("Result true or false: {}".format(test_labels[0] == label_number)) # 對比測試標籤,查看該預測是否正確 90 91 92 # 可視化:將該預測繪製成圖來查看所有10個通道 93 def plot_image(m, predictions_array, true_label, img): 94 predictions_array, true_label, img = predictions_array[m], true_label[m], img[m] 95 plt.grid(False) 96 plt.xticks([]) 97 plt.yticks([]) 98 plt.imshow(img, cmap=plt.cm.binary) 99 predicted_label = np.argmax(predictions_array) 100 if predicted_label == true_label: 101 color = 'blue' # 正確的預測標籤爲藍色 102 else: 103 color = 'red' # 錯誤的預測標籤爲紅色 104 plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 105 100 * np.max(predictions_array), 106 class_names[true_label]), 107 color=color) 108 109 110 def plot_value_array(n, predictions_array, true_label): 111 predictions_array, true_label = predictions_array[n], true_label[n] 112 plt.grid(False) 113 plt.xticks([]) 114 plt.yticks([]) 115 thisplot = plt.bar(range(10), predictions_array, color="#777777") 116 plt.ylim([0, 1]) 117 predicted_label = np.argmax(predictions_array) 118 thisplot[predicted_label].set_color('red') 119 thisplot[true_label].set_color('blue') 120 121 122 # 查看第0張圖像、預測和預測數組 123 i = 0 124 plt.figure(num=3, figsize=(8, 5)) 125 plt.subplot(1, 2, 1) 126 plot_image(i, predictions, test_labels, test_images) 127 plt.subplot(1, 2, 2) 128 plot_value_array(i, predictions, test_labels) 129 plt.xticks(range(10), class_names, rotation=45) # x座標軸刻度,參數rotation表示label旋轉顯示角度 130 plt.savefig("./outputs/sample-1-figure-3.png", dpi=200, format='png') 131 plt.show() 132 plt.close() 133 134 # 查看第12張圖像、預測和預測數組 135 i = 12 136 plt.figure(num=4, figsize=(8, 5)) 137 plt.subplot(1, 2, 1) 138 plot_image(i, predictions, test_labels, test_images) 139 plt.subplot(1, 2, 2) 140 plot_value_array(i, predictions, test_labels) 141 plt.xticks(range(10), class_names, rotation=45) # range(10)做爲x軸的刻度,class_names做爲對應的標籤 142 plt.savefig("./outputs/sample-1-figure-4.png", dpi=200, format='png') 143 plt.show() 144 plt.close() 145 146 # 繪製圖像:正確的預測標籤爲藍色,錯誤的預測標籤爲紅色,數字表示預測標籤的百分比(總計爲 100) 147 num_rows = 5 148 num_cols = 3 149 num_images = num_rows * num_cols 150 plt.figure(num=5, figsize=(2 * 2 * num_cols, 2 * num_rows)) 151 for i in range(num_images): 152 plt.subplot(num_rows, 2 * num_cols, 2 * i + 1) 153 plot_image(i, predictions, test_labels, test_images) 154 plt.subplot(num_rows, 2 * num_cols, 2 * i + 2) 155 plot_value_array(i, predictions, test_labels) 156 plt.xticks(range(10), class_names, rotation=45) 157 plt.savefig("./outputs/sample-1-figure-5.png", dpi=200, format='png') 158 plt.show() 159 plt.close() 160 161 # 使用通過訓練的模型對單個圖像進行預測 162 image = test_images[0] # 從測試數據集得到一個圖像 163 print("img shape: {}".format(image.shape)) # 圖像的shape信息 164 image = (np.expand_dims(image, 0)) # 添加到列表中 165 print("img shape: {}".format(image.shape)) 166 predictions_single = model.predict(image) # model.predict返回一組列表,每一個列表對應批次數據中的每張圖像 167 print("prediction_single: {}".format(predictions_single)) # 查看預測,預測結果是一個具備10個數字的數組,分別對應10種不一樣服飾的「置信度」 168 169 plt.figure(num=6) 170 plot_value_array(0, predictions_single, test_labels) 171 plt.xticks(range(10), class_names, rotation=45) 172 plt.savefig("./outputs/sample-1-figure-6.png", dpi=200, format='png') 173 plt.show() 174 plt.close() 175 176 prediction_result = np.argmax(predictions_single[0]) # 獲取批次數據中相應圖像的預測結果(置信度值最大的標籤) 177 print("prediction_result: {}".format(prediction_result))
C:\Users\anliven\AppData\Local\conda\conda\envs\mlcc\python.exe D:/Anliven/Anliven-Code/PycharmProjects/TempTest/TempTest.py TensorFlow version: 1.12.0 train_images.shape: (60000, 28, 28) train_labels len: 60000 train_labels: [9 0 0 ... 3 0 5] test_images.shape: (10000, 28, 28) test_labels len: 10000 test_labels: [9 2 1 ... 8 1 5] Epoch 1/5 - 3s - loss: 0.5077 - acc: 0.8211 Epoch 2/5 - 3s - loss: 0.3790 - acc: 0.8632 Epoch 3/5 - 3s - loss: 0.3377 - acc: 0.8755 Epoch 4/5 - 3s - loss: 0.3120 - acc: 0.8855 Epoch 5/5 - 3s - loss: 0.2953 - acc: 0.8914 32/10000 [..............................] - ETA: 15s 2208/10000 [=====>........................] - ETA: 0s 4576/10000 [============>.................] - ETA: 0s 7008/10000 [====================>.........] - ETA: 0s 9344/10000 [===========================>..] - ETA: 0s 10000/10000 [==============================] - 0s 30us/step Test loss: 0.3584352566242218 - Test accuracy: 0.8711 The first prediction: [4.9706377e-06 2.2675355e-09 1.3649772e-07 3.6149192e-08 4.7982059e-08 8.5262489e-03 1.5245891e-05 3.2628113e-03 1.6874857e-05 9.8817366e-01] label: 9 - class name: Ankle boot Result true or false: True img shape: (28, 28) img shape: (1, 28, 28) prediction_single: [[4.9706327e-06 2.2675313e-09 1.3649785e-07 3.6149192e-08 4.7982059e-08 8.5262526e-03 1.5245891e-05 3.2628146e-03 1.6874827e-05 9.8817366e-01]] prediction_result: 9 Process finished with exit code 0
錯誤提示
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
......
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz: None -- [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respondapi
處理方法1數組
選擇一個連接,網絡
手工下載下面四個文件,並存放在「~/.keras/datasets」下的fashion-mnist目錄。app
guowli@5CG450158J MINGW64 ~/.keras/datasets $ pwd /c/Users/guowli/.keras/datasets guowli@5CG450158J MINGW64 ~/.keras/datasets $ ls -l total 0 drwxr-xr-x 1 guowli 1049089 0 Mar 27 14:44 fashion-mnist/ guowli@5CG450158J MINGW64 ~/.keras/datasets $ ls -l fashion-mnist/ total 30164 -rw-r--r-- 1 guowli 1049089 4422102 Mar 27 15:47 t10k-images-idx3-ubyte.gz -rw-r--r-- 1 guowli 1049089 5148 Mar 27 15:47 t10k-labels-idx1-ubyte.gz -rw-r--r-- 1 guowli 1049089 26421880 Mar 27 15:47 train-images-idx3-ubyte.gz -rw-r--r-- 1 guowli 1049089 29515 Mar 27 15:47 train-labels-idx1-ubyte.gz
處理方法2機器學習
手工下載文件,存放在指定目錄。
改寫「tensorflow\python\keras\datasets\fashion_mnist.py」定義的load_data()函數。函數
from tensorflow.python.keras.utils import get_file import numpy as np import pathlib import gzip def load_data(): # 改寫「tensorflow\python\keras\datasets\fashion_mnist.py」定義的load_data()函數 base = "file:///" + str(pathlib.Path.cwd()) + "\\" # 當前目錄 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(get_file(fname, origin=base + 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) (train_images, train_labels), (test_images, test_labels) = load_data()
錯誤提示學習
「OSError: Not a gzipped file (b'\n\n')」
處理方法
對於損壞的、不完整的.gz文件,zip.open()將沒法打開。檢查.gz文件是否完整無損。
參考信息