TensorFlow 從簡單的手勢識別開始

 

前言

學習筆記來自於Andrew Ng,文末附資料。

1 - 導入TensorFlow庫

import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
import tf_utils
import time
%matplotlib inline
np.random.seed(1)

Exercise----1

=============================

y_hat = tf.constant(36, name='y_hat') #定義y_hat爲固定值36
y = tf.constant(39, name='y')         #定義y爲固定值39

loss = tf.Variable((y-y_hat)**2, name='loss')   #爲損失函數創建一個變量
init = tf.global_variables_initializer()  #運行之後的初始化(ession.run(init)
sess = tf.Session()  #損失變量將被初始化並準備計算
sess.run(init)          #初始化變量
print(sess.run(loss))  #創建一個session並打印輸出 
9

佔位符是一個對象,它的值只能在稍後指定,要指定佔位符的值,可以使用一個feed字典(feed_dict變量)來傳入,接下來,我們爲x創建一個佔位符,這將允許我們在稍後運行會話時傳入一個數字。

x = tf.placeholder(tf.int64, name='x')
print(sess.run(2*x, feed_dict={x:3}))
sess.close()
6

1.1 - 線性函數

讓我們通過計算以下等式來開始編程:Y=WX+b ,W和X是隨機矩陣,b是隨機向量。
我們計算WX+b,其中W,X和b是從隨機正態分佈中抽取的。 W的維度是(4,3),X是(3,1),b是(4,1)。 我們開始定義一個shape=(3,1)的常量X:

def linear_function():
    """
    實現一個線性功能:
        初始化W,類型爲tensor的隨機變量,維度爲(4,3)
        初始化X,類型爲tensor的隨機變量,維度爲(3,1)
        初始化b,類型爲tensor的隨機變量,維度爲(4,1)
    返回:
        result - 運行了session後的結果,運行的是Y = WX + b 

    """
    
    np.random.seed(1)
    
    X = np.random.randn(3,1)
    W = np.random.randn(4,3)
    b = np.random.randn(4,1)
    
    # Y = tf.add(tf.matmul(W,X)+b)
    Y = tf.matmul(W,X) + b
    sess = tf.Session()
    result = sess.run(Y)
    sess.close()  #session使用完畢,關閉它
    return result
print('result = ' + str(linear_function()))
result = [[-2.15657382]
 [ 2.95891446]
 [-1.08926781]
 [-0.84538042]]

1.2 - 計算sigmoid

def sigmoid(z):
    x = tf.placeholder(tf.float32, name='x')
    sigmoid = tf.sigmoid(x)
    with tf.Session() as sess:
        result = sess.run(sigmoid, feed_dict={x:z})
    return result
print ("sigmoid(12) = " + str(sigmoid(12)))
print ("sigmoid(0) = " + str(sigmoid(0)))
sigmoid(12) = 0.999994
sigmoid(0) = 0.5

1.3 - 計算成本

1.4 - 使用獨熱編碼(0、1編碼)

獨熱編碼 ------> one_hot_coding

很多時候在深度學習中y向量的維度是從0到C−1的,C是指分類的類別數量,如果C=4,那麼對y而言你可能需要有以下的轉換方式:

def one_hot_matrix(lables, C):
    """
    創建一個矩陣,其中第i行對應第i個類號,第j列對應第j個訓練樣本
    所以如果第j個樣本對應着第i個標籤,那麼entry (i,j)將會是1

    參數:
        lables - 標籤向量
        C - 分類數

    返回:
        one_hot - 獨熱矩陣
    """
    C = tf.constant(C, name='C')
    one_hot_matrix = tf.one_hot(indices=lables, depth=C, axis=0) 
    # axis the direction of depth (0->row, 1->column)
    sess = tf.Session()
    one_hot = sess.run(one_hot_matrix)
    sess.close()
    return one_hot
lables = np.array([1, 2, 3, 0, 2, 1])
one_hot = one_hot_matrix(lables, 4)
print(str(one_hot))
print("------------------------------------")
lable2 = np.array([1,2,3,4,5,6,7,8,9])
two_hot = one_hot_matrix(lable2, 10)
print(str(two_hot))
[[ 0.  0.  0.  1.  0.  0.]
 [ 1.  0.  0.  0.  0.  1.]
 [ 0.  1.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  0.  0.]]
------------------------------------
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  1.]]

1.5 - 初始化爲0和1

現在我們將學習如何用0或者1初始化一個向量,我們要用到tf.ones()和tf.zeros(),給定這些函數一個維度值那麼它們將會返回全是1或0的滿足條件的向量/矩陣

def ones(shape):
    ones = tf.ones(shape)
    sess = tf.Session()
    ones = sess.run(ones)
    sess.close()
    return ones

def zeros(shape):
    ones = tf.zeros(shape)
    sess = tf.Session()
    ones = sess.run(ones)
    sess.close()
    return ones
print('ones = ' + str(ones([3,1])))
print('zeros= ' + str(zeros([4,1])))
ones = [[ 1.]
 [ 1.]
 [ 1.]]
zeros= [[ 0.]
 [ 0.]
 [ 0.]
 [ 0.]]

2 - 使用TensorFlow構建你的第一個神經網絡

X_train_orig , Y_train_orig , X_test_orig , Y_test_orig , classes = tf_utils.load_dataset()
index = 111
plt.imshow(X_train_orig[index])
print('Y = ' + str(np.squeeze(Y_train_orig[:, index])))
Y = 2

數字二

# X_train_orig.reshape(X_train_orig.shape[0], -1) # ? why is -1
#  anwerser : (number, -1) this mean number is the cow, shape/number is the column 

test_one = np.random.randn(4,5)
print(test_one.shape)
print(test_one.reshape(10, -1))
(4, 5)
[[ 0.58281521 -1.10061918]
 [ 1.14472371  0.90159072]
 [ 0.50249434  0.90085595]
 [-0.68372786 -0.12289023]
 [-0.93576943 -0.26788808]
 [ 0.53035547 -0.69166075]
 [-0.39675353 -0.6871727 ]
 [-0.84520564 -0.67124613]
 [-0.0126646  -1.11731035]
 [ 0.2344157   1.65980218]]

和往常一樣,我們要對數據集進行扁平化,然後再除以255以歸一化數據,除此之外,我們要需要把每個標籤轉化爲獨熱向量,像上面的圖一樣。

X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T #每一列就是一個樣本
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0],-1).T
print(X_train_flatten.shape)
print(X_train_orig.shape)

#歸一化數據
X_train = X_train_flatten /255
X_test = X_test_flatten/255

#轉換爲獨熱矩陣
Y_train = tf_utils.convert_to_one_hot(Y_train_orig, 6)
Y_test = tf_utils.convert_to_one_hot(Y_test_orig, 6)

print("訓練集樣本數 = " + str(X_train.shape[1]))
print("測試集樣本數 = " + str(X_test.shape[1]))
print("X_train.shape: " + str(X_train.shape))
print("Y_train.shape: " + str(Y_train.shape))
print("X_test.shape: " + str(X_test.shape))
print("Y_test.shape: " + str(Y_test.shape))
(12288, 1080)
(1080, 64, 64, 3)
訓練集樣本數 = 1080
測試集樣本數 = 120
X_train.shape: (12288, 1080)
Y_train.shape: (6, 1080)
X_test.shape: (12288, 120)
Y_test.shape: (6, 120)

我們的目標是構建能夠高準確度識別符號的算法。 要做到這一點,你要建立一個TensorFlow模型,這個模型幾乎和你之前在貓識別中使用的numpy一樣(但現在使用softmax輸出)。要將您的numpy實現與tensorflow實現進行比較的話這是一個很好的機會。

目前的模型是:LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX,SIGMOID輸出層已經轉換爲SOFTMAX。當有兩個以上的類時,一個SOFTMAX層將SIGMOID一般化。

2.1 - 創建placeholders

def create_placeholders(n_x, n_y):
    """
    爲TensorFlow會話創建佔位符
    參數:
        n_x - 一個實數,圖片向量的大小(64*64*3 = 12288)
        n_y - 一個實數,分類數(從0到5,所以n_y = 6)

    返回:
        X - 一個數據輸入的佔位符,維度爲[n_x, None],dtype = "float"
        Y - 一個對應輸入的標籤的佔位符,維度爲[n_Y,None],dtype = "float"

    提示:
        使用None,因爲它讓我們可以靈活處理佔位符提供的樣本數量。事實上,測試/訓練期間的樣本數量是不同的。

    """
    X = tf.placeholder(tf.float32, [n_x, None], name='X')
    Y = tf.placeholder(tf.float32, [n_y, None], name='Y')
    return X, Y
X, Y = create_placeholders(12288,6)
print('X = ' + str(X))
print('Y = ' + str(Y)) 
X = Tensor("X_2:0", shape=(12288, ?), dtype=float32)
Y = Tensor("Y_2:0", shape=(6, ?), dtype=float32)

2.2 - 初始化參數

初始化tensorflow中的參數,我們將使用Xavier初始化權重和用零來初始化偏差

def initialize_parameters():
    tf.set_random_seed(1)
    W1 = tf.get_variable('W1', [25, 12288], initializer=tf.contrib.layers.xavier_initializer(seed=1))
    b1 = tf.get_variable("b1",[25,1],initializer=tf.zeros_initializer())
    W2 = tf.get_variable("W2", [12, 25], initializer = tf.contrib.layers.xavier_initializer(seed=1))
    b2 = tf.get_variable("b2", [12, 1], initializer = tf.zeros_initializer())
    W3 = tf.get_variable("W3", [6, 12], initializer = tf.contrib.layers.xavier_initializer(seed=1))
    b3 = tf.get_variable("b3", [6, 1], initializer = tf.zeros_initializer())
    
    parameters = {
        'W1': W1,
        'b1': b1,
        'W2': W2,
        'b2': b2,
        'W3': W3,
        'b3': b3
    }
    return parameters
tf.reset_default_graph() #用於清除默認圖形堆棧並重置全局默認圖形。
with tf.Session() as sess:
    parameters = initialize_parameters()
    print("W1 = " + str(parameters["W1"]))
    print("b1 = " + str(parameters["b1"]))
    print("W2 = " + str(parameters["W2"]))
    print("b2 = " + str(parameters["b2"]))
W1 = <tf.Variable 'W1:0' shape=(25, 12288) dtype=float32_ref>
b1 = <tf.Variable 'b1:0' shape=(25, 1) dtype=float32_ref>
W2 = <tf.Variable 'W2:0' shape=(12, 25) dtype=float32_ref>
b2 = <tf.Variable 'b2:0' shape=(12, 1) dtype=float32_ref>

2.3 - 前向傳播

我們將要在TensorFlow中實現前向傳播,該函數將接受一個字典參數並完成前向傳播,它會用到以下代碼:

1. tf.add(…) :加法
2. tf.matmul(… , …) :矩陣乘法
3. tf.nn.relu(…) :Relu激活函數
def forward_propagation(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']
    W3 = parameters['W3']
    b3 = parameters['b3']
    
    Z1 = tf.add(tf.matmul(W1, X), b1)
    A1 = tf.nn.relu(Z1)
    Z2 = tf.add(tf.matmul(W2, A1), b2)
    A2 = tf.nn.relu(Z2)
    Z3 = tf.add(tf.matmul(W3, A2), b3)
    
    return Z3
tf.reset_default_graph()
with tf.Session() as sess:
    X,Y = create_placeholders(12288, 6)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    print('Z3 = ' + str(Z3))
Z3 = Tensor("Add_2:0", shape=(6, ?), dtype=float32)

2.4 - 計算成本

def compute_cost(Z3,Y):
    logits = tf.transpose(Z3) #轉置
    labels = tf.transpose(Y)
    
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=labels))
    return cost
tf.reset_default_graph()
with tf.Session() as sess:
    X,Y = create_placeholders(12288,6)
    parameters = initialize_parameters()
    Z3 = forward_propagation(X, parameters)
    cost = compute_cost(Z3,Y)
    print('cost =' +str(cost))
cost =Tensor("Mean:0", shape=(), dtype=float32)

2.5 - 反向傳播&更新參數

得益於編程框架,所有反向傳播和參數更新都在1行代碼中處理。計算成本函數後,將創建一個“optimizer”對象。 運行tf.session時,必須將此對象與成本函數一起調用,當被調用時,它將使用所選擇的方法和學習速率對給定成本進行優化。

optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost)

(n_x, m) = X_train.shape
print(n_x)
print(m)
12288
1080

2.6 - 構建模型

def model(X_train, Y_train, X_test, Y_test, learning_rate=0.0001,
          num_epochs=1500,minibatch_size=32,print_cost=True, is_plot=True):
    """
    實現一個三層的TensorFlow神經網絡:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX

    參數:
        X_train - 訓練集,維度爲(輸入大小(輸入節點數量) = 12288, 樣本數量 = 1080)
        Y_train - 訓練集分類數量,維度爲(輸出大小(輸出節點數量) = 6, 樣本數量 = 1080)
        X_test - 測試集,維度爲(輸入大小(輸入節點數量) = 12288, 樣本數量 = 120)
        Y_test - 測試集分類數量,維度爲(輸出大小(輸出節點數量) = 6, 樣本數量 = 120)
        learning_rate - 學習速率
        num_epochs - 整個訓練集的遍歷次數
        mini_batch_size - 每個小批量數據集的大小
        print_cost - 是否打印成本,每100代打印一次
        is_plot - 是否繪製曲線圖

    返回:
        parameters - 學習後的參數

    """
    ops.reset_default_graph() #能夠重新運行模型而不覆蓋tf變量
    tf.set_random_seed(1)
    seed = 3
    (n_x, m) = X_train.shape  #獲取輸入節點數量和樣本數
    n_y = Y_train.shape[0]
    costs = []                #成本集
    
    
    #給X和Y創建placeholder
    X,Y = create_placeholders(n_x, n_y)
    
    #初始化參數
    parameters = initialize_parameters()
    
    #前向傳播
    Z3 = forward_propagation(X, parameters)
    
    #計算成本
    cost = compute_cost(Z3,Y)
    
    #反向傳播,使用Adam優化
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
    
    #初始化所有的變量
    init = tf.global_variables_initializer()
    
    #開始會話並計算
    with tf.Session() as sess:
        #初始化
        sess.run(init)
        
        #正常訓練的循環
        for epoch in range(num_epochs):
            epoch_cost = 0        #每代的成本
            num_minibatches = int(m / minibatch_size)  #minibatch的總數量
            seed = seed +1
            minibatches = tf_utils.random_mini_batches(X_train, Y_train)
            
            for minibatch in minibatches:
                
                #選擇一個minibatch
                (minibatch_X, minibatch_Y) = minibatch
                
                #數據已經準備好了,開始運行session
                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X:minibatch_X,Y:minibatch_Y})
                
                #計算這個minibatch在這一代中所佔的誤差
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches
            
            #記錄並打印成本
            ## 記錄成本
            if epoch % 5 == 0:
                costs.append(epoch_cost)
                if print_cost and epoch % 100 ==0:
                    print("epoch = " + str(epoch) + "    epoch_cost = " + str(epoch_cost))
         
        #是否繪製圖譜
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()
            
        parameters = sess.run(parameters)
        print('參數已經保存到session。')
        #計算當前的預測結果
        correct_prediction = tf.equal(tf.argmax(Z3),tf.argmax(Y))
         #計算準確率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
        
        print("訓練集的準確率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("測試集的準確率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters

2.7 - 運行的全過程

X_train_orig , Y_train_orig , X_test_orig , Y_test_orig , classes = tf_utils.load_dataset()
X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T #每一列就是一個樣本
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0],-1).T
print(X_train_flatten.shape)
print(X_train_orig.shape)

#歸一化數據
X_train = X_train_flatten /255
X_test = X_test_flatten/255

#轉換爲獨熱矩陣
Y_train = tf_utils.convert_to_one_hot(Y_train_orig, 6)
Y_test = tf_utils.convert_to_one_hot(Y_test_orig, 6)

(12288, 1080)
(1080, 64, 64, 3)
def create_placeholders(n_x, n_y):
    """
    爲TensorFlow會話創建佔位符
    參數:
        n_x - 一個實數,圖片向量的大小(64*64*3 = 12288)
        n_y - 一個實數,分類數(從0到5,所以n_y = 6)

    返回:
        X - 一個數據輸入的佔位符,維度爲[n_x, None],dtype = "float"
        Y - 一個對應輸入的標籤的佔位符,維度爲[n_Y,None],dtype = "float"

    提示:
        使用None,因爲它讓我們可以靈活處理佔位符提供的樣本數量。事實上,測試/訓練期間的樣本數量是不同的。

    """
    X = tf.placeholder(tf.float32, [n_x, None], name='X')
    Y = tf.placeholder(tf.float32, [n_y, None], name='Y')
    return X, Y
def initialize_parameters():
    tf.set_random_seed(1)
    W1 = tf.get_variable('W1', [25, 12288], initializer=tf.contrib.layers.xavier_initializer(seed=1))
    b1 = tf.get_variable("b1",[25,1],initializer=tf.zeros_initializer())
    W2 = tf.get_variable("W2", [12, 25], initializer = tf.contrib.layers.xavier_initializer(seed=1))
    b2 = tf.get_variable("b2", [12, 1], initializer = tf.zeros_initializer())
    W3 = tf.get_variable("W3", [6, 12], initializer = tf.contrib.layers.xavier_initializer(seed=1))
    b3 = tf.get_variable("b3", [6, 1], initializer = tf.zeros_initializer())
    
    parameters = {
        'W1': W1,
        'b1': b1,
        'W2': W2,
        'b2': b2,
        'W3': W3,
        'b3': b3
    }
    return parameters
def forward_propagation(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']
    W3 = parameters['W3']
    b3 = parameters['b3']
    
    Z1 = tf.add(tf.matmul(W1, X), b1)
    A1 = tf.nn.relu(Z1)
    Z2 = tf.add(tf.matmul(W2, A1), b2)
    A2 = tf.nn.relu(Z2)
    Z3 = tf.add(tf.matmul(W3, A2), b3)
    
    return Z3
def compute_cost(Z3,Y):
    logits = tf.transpose(Z3) #轉置
    labels = tf.transpose(Y)
    
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=labels))
    return cost
def model(X_train,Y_train,X_test,Y_test,
        learning_rate=0.0001,num_epochs=1500,minibatch_size=32,
        print_cost=True,is_plot=True):
    """
    實現一個三層的TensorFlow神經網絡:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX

    參數:
        X_train - 訓練集,維度爲(輸入大小(輸入節點數量) = 12288, 樣本數量 = 1080)
        Y_train - 訓練集分類數量,維度爲(輸出大小(輸出節點數量) = 6, 樣本數量 = 1080)
        X_test - 測試集,維度爲(輸入大小(輸入節點數量) = 12288, 樣本數量 = 120)
        Y_test - 測試集分類數量,維度爲(輸出大小(輸出節點數量) = 6, 樣本數量 = 120)
        learning_rate - 學習速率
        num_epochs - 整個訓練集的遍歷次數
        mini_batch_size - 每個小批量數據集的大小
        print_cost - 是否打印成本,每100代打印一次
        is_plot - 是否繪製曲線圖

    返回:
        parameters - 學習後的參數

    """
    ops.reset_default_graph()                #能夠重新運行模型而不覆蓋tf變量
    tf.set_random_seed(1)
    seed = 3
    (n_x , m)  = X_train.shape               #獲取輸入節點數量和樣本數
    n_y = Y_train.shape[0]                   #獲取輸出節點數量
    costs = []                               #成本集

    #給X和Y創建placeholder
    X,Y = create_placeholders(n_x,n_y)

    #初始化參數
    parameters = initialize_parameters()

    #前向傳播
    Z3 = forward_propagation(X,parameters)

    #計算成本
    cost = compute_cost(Z3,Y)

    #反向傳播,使用Adam優化
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    #初始化所有的變量
    init = tf.global_variables_initializer()

    #開始會話並計算
    with tf.Session() as sess:
        #初始化
        sess.run(init)

        #正常訓練的循環
        for epoch in range(num_epochs):

            epoch_cost = 0  #每代的成本
            num_minibatches = int(m / minibatch_size)    #minibatch的總數量
            seed = seed + 1
            minibatches = tf_utils.random_mini_batches(X_train,Y_train,minibatch_size,seed)

            for minibatch in minibatches:

                #選擇一個minibatch
                (minibatch_X,minibatch_Y) = minibatch

                #數據已經準備好了,開始運行session
                _ , minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})

                #計算這個minibatch在這一代中所佔的誤差
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches

            #記錄並打印成本
            ## 記錄成本
            if epoch % 5 == 0:
                costs.append(epoch_cost)
                #是否打印:
                if print_cost and epoch % 100 == 0:
                        print("epoch = " + str(epoch) + "    epoch_cost = " + str(epoch_cost))

        #是否繪製圖譜
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()

        #保存學習後的參數
        parameters = sess.run(parameters)
        print("參數已經保存到session。")

        #計算當前的預測結果
        correct_prediction = tf.equal(tf.argmax(Z3),tf.argmax(Y))

        #計算準確率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))

        print("訓練集的準確率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("測試集的準確率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
start_time = time.clock()
parameters = model(X_train, Y_train, X_test, Y_test)
end_time = time.clock()
print("CPU的執行時間 = " + str(end_time - start_time) + " 秒" )
epoch = 0    epoch_cost = 1.85570190892
epoch = 100    epoch_cost = 1.01645778345
epoch = 200    epoch_cost = 0.733102415547
epoch = 300    epoch_cost = 0.572939646967
epoch = 400    epoch_cost = 0.468774231997
epoch = 500    epoch_cost = 0.381020727031
epoch = 600    epoch_cost = 0.313821615143
epoch = 700    epoch_cost = 0.254157840302
epoch = 800    epoch_cost = 0.203829386921
epoch = 900    epoch_cost = 0.166421434644
epoch = 1000    epoch_cost = 0.141485600083
epoch = 1100    epoch_cost = 0.107580181776
epoch = 1200    epoch_cost = 0.0862698159886
epoch = 1300    epoch_cost = 0.0593705453317
epoch = 1400    epoch_cost = 0.0522282078975

仿真圖

參數已經保存到session。
訓練集的準確率: 0.999074
測試集的準確率: 0.716667
CPU的執行時間 = 1185.987672 秒

3 - 預測

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

my_image1 = '5.png'
fileName1 = 'datasets/fingers/' + my_image1

image1 = mpimg.imread(fileName1)
plt.imshow(image1)
my_image1 = image1.reshape(1, 64*64*3).T
my_image_prediction = tf_utils.predict(my_image1, parameters)
print('預測結果: y = ' + str(np.squeeze(my_image_prediction)))
預測結果: y = 5

數字五

my_image1 = '4.png'
fileName1 = 'datasets/fingers/' + my_image1

image1 = mpimg.imread(fileName1)
plt.imshow(image1)
my_image1 = image1.reshape(1, 64*64*3).T
my_image_prediction = tf_utils.predict(my_image1, parameters)
print('預測結果: y = ' + str(np.squeeze(my_image_prediction)))
預測結果: y = 2

數字四

看樣子還要在繼續改進!

my_image1 = '3.png'
fileName1 = 'datasets/fingers/' + my_image1

image1 = mpimg.imread(fileName1)
plt.imshow(image1)
my_image1 = image1.reshape(1, 64*64*3).T
my_image_prediction = tf_utils.predict(my_image1, parameters)
print('預測結果: y = ' + str(np.squeeze(my_image_prediction)))
預測結果: y = 2

數字三

my_image1 = '2.png'
fileName1 = 'datasets/fingers/' + my_image1

image1 = mpimg.imread(fileName1)
plt.imshow(image1)
my_image1 = image1.reshape(1, 64*64*3).T
my_image_prediction = tf_utils.predict(my_image1, parameters)
print('預測結果: y = ' + str(np.squeeze(my_image_prediction)))
預測結果: y = 1

數字二

my_image1 = '1.png'
fileName1 = 'datasets/fingers/' + my_image1

image1 = mpimg.imread(fileName1)
plt.imshow(image1)
my_image1 = image1.reshape(1, 64*64*3).T
my_image_prediction = tf_utils.predict(my_image1, parameters)
print('預測結果: y = ' + str(np.squeeze(my_image_prediction)))
預測結果: y = 1

附件: 訓練模型train_signs.h5

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