將訓練好的神經(jīng)網(wǎng)絡(luò)保存下來在另一個文件中載入后進行預測

注意這里是將訓練好的神經(jīng)網(wǎng)絡(luò)拿出來進行預測(使用)卸耘,而不是測試鳄橘,測試可與訓練在一個文件中進行折柠,當然也可像這樣載入網(wǎng)絡(luò)和訓練數(shù)據(jù)來測試讳苦。

tensorflow_tutorial.py文件---訓練神經(jīng)網(wǎng)絡(luò)

#!/usr/bin/env python
# coding=utf-8
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
import scipy
from PIL import Image
from scipy import ndimage
# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Flatten the training and test images
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
# Normalize image vectors
X_train = X_train_flatten/255.
X_test = X_test_flatten/255.
# Convert training and test labels to one hot matrices,每一列代表一張圖片label的ont hot vector
Y_train = convert_to_one_hot(Y_train_orig, 6)
Y_test = convert_to_one_hot(Y_test_orig, 6)


def create_placeholders(n_x, n_y):
    """
    Creates the placeholders for the tensorflow session.

    Arguments:
    n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)
    n_y -- scalar, number of classes (from 0 to 5, so -> 6)

    Returns:
    X -- placeholder for the data input, of shape [n_x, None] and dtype "float"
    Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"

    Tips:
    - You will use None because it let's us be flexible on the number of examples you will for the placeholders.
      In fact, the number of examples during test/train is different.
    """

    ### START CODE HERE ### (approx. 2 lines)
    X = tf.placeholder(tf.float32,shape=(n_x,None))
    Y = tf.placeholder(tf.float32,shape=(n_y,None))
    ### END CODE HERE ###

    return X, Y


def get_weight(shape):
    w=tf.Variable(tf.truncated_normal(shape,stddev=0.1))
    return w

def get_bias(shape):
    b=tf.Variable(tf.zeros(shape))
    return b
def initialize_parameters():
    """
    Initializes parameters to build a neural network with tensorflow. The shapes are:
                        W1 : [25, 12288]
                        b1 : [25, 1]
                        W2 : [12, 25]
                        b2 : [12, 1]
                        W3 : [6, 12]
                        b3 : [6, 1]

    Returns:
    parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3
    """

    tf.set_random_seed(1)  # so that your "random" numbers match ours

    ### START CODE HERE ### (approx. 6 lines of code)
    W1 = get_weight([25,12288])
    b1 = get_bias([25,1])
    W2 = get_weight([12,25])
    b2 = get_bias([12,1])
    W3 = get_weight([6,12])
    b3 = get_bias([6,1])
    ### END CODE HERE ###

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2,
                  "W3": W3,
                  "b3": b3}

    return parameters


def forward_propagation(X, parameters):
    """
    Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX

    Arguments:
    X -- input dataset placeholder, of shape (input size, number of examples)
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
                  the shapes are given in initialize_parameters

    Returns:
    Z3 -- the output of the last LINEAR unit
    """

    # Retrieve the parameters from the dictionary "parameters"
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']
    W3 = parameters['W3']
    b3 = parameters['b3']

    ### START CODE HERE ### (approx. 5 lines)              # Numpy Equivalents:
    Z1 = tf.matmul(W1,X)+b1  # Z1 = np.dot(W1, X) + b1
    A1 = tf.nn.relu(Z1)  # A1 = relu(Z1)
    Z2 = tf.matmul(W2,A1)+b2   # Z2 = np.dot(W2, a1) + b2
    A2 = tf.nn.relu(Z2)  # A2 = relu(Z2)
    Z3 = tf.matmul(W3,A2)+b3  # Z3 = np.dot(W3,Z2) + b3
    ### END CODE HERE ###

    return Z3


# GRADED FUNCTION: compute_cost

def compute_cost(Z3, Y):
    """
    Computes the cost

    Arguments:
    Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (6, number of examples)
    Y -- "true" labels vector placeholder, same shape as Z3

    Returns:
    cost - Tensor of the cost function
    """

    # to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits(...,...)
    logits = tf.transpose(Z3)
    labels = tf.transpose(Y)

    ### START CODE HERE ### (1 line of code)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = labels))
    ### END CODE HERE ###

    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):
    """
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    ops.reset_default_graph()  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)  # to keep consistent results
    seed = 3  # to keep consistent results
    (n_x, m) = X_train.shape  # (n_x: input size, m : number of examples in the train set)
    n_y = Y_train.shape[0]  # n_y : output size
    costs = []  # To keep track of the cost

    # Create Placeholders of shape (n_x, n_y)
    ### START CODE HERE ### (1 line)
    X, Y = create_placeholders(n_x,n_y)
    ### END CODE HERE ###

    # Initialize parameters
    ### START CODE HERE ### (1 line)
    parameters = initialize_parameters()
    ### END CODE HERE ###

    # Forward propagation: Build the forward propagation in the tensorflow graph
    ### START CODE HERE ### (1 line)
    Z3 = forward_propagation(X,parameters)
    ### END CODE HERE ###

    # Cost function: Add cost function to tensorflow graph
    ### START CODE HERE ### (1 line)
    cost = compute_cost(Z3,Y)
    ### END CODE HERE ###

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    ### START CODE HERE ### (1 line)
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
    ### END CODE HERE ###

    # Initialize all the variables
    init = tf.global_variables_initializer()
    # 實例化Saver對象带膜,max_to_keep=1表示只想保存最終模型
    # 默認max_to_keep=5,保存最近的5個模型
    saver=tf.train.Saver(max_to_keep=1)

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            epoch_cost = 0.  # Defines a cost related to an epoch
            num_minibatches = int(m / minibatch_size)  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            # 將總的訓練數(shù)據(jù)隨機分成m/minibatch_size個minibatch
            # 由于每個epoch的隨機種子在遞增(都不同了)鸳谜,所以每個epoch的訓練數(shù)據(jù)的劃分是不同的
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)

            for minibatch in minibatches:
                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).
                ### START CODE HERE ### (1 line)
                _, minibatch_cost = sess.run((optimizer,cost),feed_dict={X:minibatch_X,Y:minibatch_Y})
                ### END CODE HERE ###

                epoch_cost += minibatch_cost / num_minibatches
            saver.save(sess,'./model/my_model',global_step=num_epochs)

            # Print the cost every epoch
            if print_cost == True and epoch % 100 == 0:
                print("Cost after epoch %i: %f" % (epoch, epoch_cost))
            if print_cost == True and epoch % 5 == 0:
                costs.append(epoch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # lets save the parameters in a variable

        print("Parameters have been trained!")

        # Calculate the correct predictions
        correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
        print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
if __name__ == '__main__':
    model(X_train, Y_train, X_test, Y_test)

tf_utils.py----輔助訓練神經(jīng)網(wǎng)絡(luò)的文件

import h5py
import numpy as np
import tensorflow as tf
import math

def load_dataset():
    train_dataset = h5py.File('datasets/train_signs.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('datasets/test_signs.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):
    m = X.shape[1]                  # number of training examples
    mini_batches = []
    np.random.seed(seed)
    
    # Step 1: Shuffle (X, Y)
    permutation = list(np.random.permutation(m))
    shuffled_X = X[:, permutation]
    shuffled_Y = Y[:, permutation].reshape((Y.shape[0],m))

    # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
    num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning
    for k in range(0, num_complete_minibatches):
        mini_batch_X = shuffled_X[:, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
        mini_batch_Y = shuffled_Y[:, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    
    # Handling the end case (last mini-batch < mini_batch_size)
    if m % mini_batch_size != 0:
        mini_batch_X = shuffled_X[:, num_complete_minibatches * mini_batch_size : m]
        mini_batch_Y = shuffled_Y[:, num_complete_minibatches * mini_batch_size : m]
        mini_batch = (mini_batch_X, mini_batch_Y)
        mini_batches.append(mini_batch)
    
    return mini_batches

def convert_to_one_hot(Y, C):
    Y = np.eye(C)[Y.reshape(-1)].T
    return Y


def predict(X, parameters):
    
    W1 = tf.convert_to_tensor(parameters["W1"])
    b1 = tf.convert_to_tensor(parameters["b1"])
    W2 = tf.convert_to_tensor(parameters["W2"])
    b2 = tf.convert_to_tensor(parameters["b2"])
    W3 = tf.convert_to_tensor(parameters["W3"])
    b3 = tf.convert_to_tensor(parameters["b3"])
    
    params = {"W1": W1,
              "b1": b1,
              "W2": W2,
              "b2": b2,
              "W3": W3,
              "b3": b3}
    
    x = tf.placeholder("float", [12288, 1]) #這里是預測膝藕,即只輸入一張圖片進去預測其類別,所以樣本數(shù)為‘1’
    
    z3 = forward_propagation_for_predict(x, params)
    p = tf.argmax(z3)
    
    sess = tf.Session()
    prediction = sess.run(p, feed_dict = {x: X})
        
    return prediction

def forward_propagation_for_predict(X, parameters):
    
    # Retrieve the parameters from the dictionary "parameters" 
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']
    W3 = parameters['W3']
    b3 = parameters['b3'] 
                                                           # Numpy Equivalents:
    Z1 = tf.add(tf.matmul(W1, X), b1)                      # Z1 = np.dot(W1, X) + b1
    A1 = tf.nn.relu(Z1)                                    # A1 = relu(Z1)
    Z2 = tf.add(tf.matmul(W2, A1), b2)                     # Z2 = np.dot(W2, a1) + b2
    A2 = tf.nn.relu(Z2)                                    # A2 = relu(Z2)
    Z3 = tf.add(tf.matmul(W3, A2), b3)                     # Z3 = np.dot(W3,Z2) + b3
    
    return Z3
    
    

predict.py---載入模型進行預測

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
import scipy
from PIL import Image
from scipy import ndimage
import tensorflow_tutorial

# 載入待預測的圖片
my_image = "thumbs_up.jpg"
fname = "images/" + my_image
# 將圖片轉(zhuǎn)換成我們訓練的神經(jīng)網(wǎng)絡(luò)輸入的格式
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(64,64)).reshape((1, 64*64*3)).T

with tf.Graph().as_default() as g:
    # 給送入訓練好的神經(jīng)網(wǎng)絡(luò)進行預測的圖片數(shù)據(jù)占位
    x = tf.placeholder(tf.float32, shape=(12288,None))
    #初始化神經(jīng)網(wǎng)絡(luò)變量
    parameters=tensorflow_tutorial.initialize_parameters()                
    #建立計算圖(前向傳播)
    Z3=tensorflow_tutorial.forward_propagation(x,parameters)
    # 預測圖片類別
    p=tf.argmax(Z3)

    # 實例化 saver 對象
    saver = tf.train.Saver()

    with tf.Session() as sess:
        # 將加載指定路徑下的ckpt咐扭,若模型存在芭挽,則加載模型到當前對話
        ckpt = tf.train.get_checkpoint_state('./model')
        saver.restore(sess, ckpt.model_checkpoint_path)
        # 進行預測
        prediction=sess.run(p,feed_dict={x:my_image})
        print(prediction)# 打印出預測結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蝗肪,隨后出現(xiàn)的幾起案子袜爪,更是在濱河造成了極大的恐慌,老刑警劉巖穗慕,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饿敲,死亡現(xiàn)場離奇詭異,居然都是意外死亡逛绵,警方通過查閱死者的電腦和手機怀各,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來术浪,“玉大人瓢对,你說我怎么就攤上這事∫人眨” “怎么了硕蛹?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長棋傍,這世上最難降的妖魔是什么祠墅? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮乙濒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘卵蛉。我一直安慰自己颁股,他們只是感情好,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布傻丝。 她就那樣靜靜地躺著甘有,像睡著了一般。 火紅的嫁衣襯著肌膚如雪葡缰。 梳的紋絲不亂的頭發(fā)上亏掀,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天,我揣著相機與錄音运准,去河邊找鬼幌氮。 笑死,一個胖子當著我的面吹牛胁澳,可吹牛的內(nèi)容都是我干的该互。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼韭畸,長吁一口氣:“原來是場噩夢啊……” “哼宇智!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起胰丁,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤随橘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锦庸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體机蔗,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年甘萧,在試婚紗的時候發(fā)現(xiàn)自己被綠了萝嘁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡扬卷,死狀恐怖牙言,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怪得,我是刑警寧澤咱枉,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布卑硫,位于F島的核電站,受9級特大地震影響蚕断,放射性物質(zhì)發(fā)生泄漏欢伏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一亿乳、第九天 我趴在偏房一處隱蔽的房頂上張望颜懊。 院中可真熱鬧,春花似錦风皿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至夷恍,卻和暖如春魔眨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背酿雪。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工遏暴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人指黎。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓朋凉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親醋安。 傳聞我的和親對象是個殘疾皇子杂彭,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 黨,我愛你 作者:李繼育 我把愛你的口號放在稱砣上 心跳的血液染紅旗幟的一角 一呼一吸燃燒著愛你的激情 2...
    A風尚子玉閱讀 135評論 0 0
  • 2018年7月01日星期天 LP158高鵬叭首,我是一個自信习勤、認真、負責任的男人放棒!我承諾創(chuàng)造一個開心姻报、友愛、奉獻的星期...
    高鵬_6bf0閱讀 207評論 0 0
  • 《瞬時花開》目錄上一章:瞬時花開(48) 掌聲间螟、歡呼聲響徹全場吴旋,許多同學激動得站了起來损肛。姚葉令人叫絕的收尾動作,毫...
    星月花木閱讀 276評論 0 0
  • 聊與東坡 忙乎了好一陣子荣瑟,也沒時間上來打理園子治拿,讓秋天的葉子在風里忽起忽落,在雨里濕淋淋的粘著泥笆焰,顯得荒涼劫谅,象這不...
    fjbsoho簡閱讀 552評論 0 1