【連載】深度學(xué)習(xí)筆記12:卷積神經(jīng)網(wǎng)絡(luò)的Tensorflow實(shí)現(xiàn)

????? 在上一講中商叹,我們學(xué)習(xí)了如何利用 numpy 手動(dòng)搭建卷積神經(jīng)網(wǎng)絡(luò)估蹄。但在實(shí)際的圖像識(shí)別中次绘,使用 numpy 去手寫 CNN 未免有些吃力不討好始锚。在 DNN 的學(xué)習(xí)中,我們也是在手動(dòng)搭建之后利用 Tensorflow 去重新實(shí)現(xiàn)一遍喳逛,一來(lái)為了能夠?qū)ι窠?jīng)網(wǎng)絡(luò)的傳播機(jī)制能夠理解更加透徹瞧捌,二來(lái)也是為了更加高效使用開源框架快速搭建起深度學(xué)習(xí)項(xiàng)目。本節(jié)就繼續(xù)和大家一起學(xué)習(xí)如何利用 Tensorflow 搭建一個(gè)卷積神經(jīng)網(wǎng)絡(luò)润文。

????? 我們繼續(xù)以 NG 課題組提供的 sign 手勢(shì)數(shù)據(jù)集為例姐呐,學(xué)習(xí)如何通過(guò) Tensorflow 快速搭建起一個(gè)深度學(xué)習(xí)項(xiàng)目。數(shù)據(jù)集標(biāo)簽共有零到五總共 6 類標(biāo)簽典蝌,示例如下:

????? 先對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單的預(yù)處理并查看訓(xùn)練集和測(cè)試集維度:

X_train = X_train_orig/255.

X_test = X_test_orig/255.

Y_train = convert_to_one_hot(Y_train_orig, 6).T

Y_test = convert_to_one_hot(Y_test_orig, 6).T

print ("number of training examples = " + str(X_train.shape[0]))

print ("number of test examples = " + str(X_test.shape[0]))

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))

可見(jiàn)我們總共有 1080 張 64643 訓(xùn)練集圖像曙砂,120 張 64643 的測(cè)試集圖像,共有 6 類標(biāo)簽骏掀。下面我們開始搭建過(guò)程鸠澈。

創(chuàng)建 placeholder

????? 首先需要為訓(xùn)練集預(yù)測(cè)變量和目標(biāo)變量創(chuàng)建占位符變量 placeholder ,定義創(chuàng)建占位符變量函數(shù):

def create_placeholders(n_H0, n_W0, n_C0, n_y): ? ?

? ?"""

? ?Creates the placeholders for the tensorflow session.

? ?Arguments:

? ?n_H0 -- scalar, height of an input image

? ?n_W0 -- scalar, width of an input image

? ?n_C0 -- scalar, number of channels of the input

? ?n_y -- scalar, number of classes

? ?Returns:

? ?X -- placeholder for the data input, of shape [None, n_H0, n_W0, n_C0] and dtype "float"

? ?Y -- placeholder for the input labels, of shape [None, n_y] and dtype "float"

? ?""" ? ?X = tf.placeholder(tf.float32, shape=(None, n_H0, n_W0, n_C0), name='X')

? ?Y = tf.placeholder(tf.float32, shape=(None, n_y), name='Y') ? ?

? ?return X, Y

參數(shù)初始化

????? 然后需要對(duì)濾波器權(quán)值參數(shù)進(jìn)行初始化:

def initialize_parameters(): ? ?

? ?"""

? ?Initializes weight parameters to build a neural network with tensorflow.

? ?Returns:

? ?parameters -- a dictionary of tensors containing W1, W2

? ?""" ? ?tf.set_random_seed(1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?W1 = tf.get_variable("W1", [4,4,3,8], initializer = tf.contrib.layers.xavier_initializer(seed = 0))

? ?W2 = tf.get_variable("W2", [2,2,8,16], initializer = tf.contrib.layers.xavier_initializer(seed = 0))

? ?parameters = {"W1": W1, ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?"W2": W2} ? ?

? ?return parameters

執(zhí)行卷積網(wǎng)絡(luò)的前向傳播過(guò)程

前向傳播過(guò)程如下所示:

CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED

可見(jiàn)我們要搭建的是一個(gè)典型的 CNN 過(guò)程截驮,經(jīng)過(guò)兩次的卷積-relu激活-最大池化笑陈,然后展開接上一個(gè)全連接層。利用

Tensorflow ?搭建上述傳播過(guò)程如下:

def forward_propagation(X, parameters): ? ?

? ?"""

? ?Implements the forward propagation for the model

? ?Arguments:

? ?X -- input dataset placeholder, of shape (input size, number of examples)

? ?parameters -- python dictionary containing your parameters "W1", "W2"

? ? ? ? ? ? ? ? ?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']

? ?W2 = parameters['W2'] ? ?

? ?# CONV2D: stride of 1, padding 'SAME' ? ?Z1 = tf.nn.conv2d(X,W1, strides = [1,1,1,1], padding = 'SAME') ? ?

? ?# RELU ? ?A1 = tf.nn.relu(Z1) ? ?

? ?# MAXPOOL: window 8x8, sride 8, padding 'SAME' ? ?P1 = tf.nn.max_pool(A1, ksize = [1,8,8,1], strides = [1,8,8,1], padding = 'SAME') ? ?

? ?# CONV2D: filters W2, stride 1, padding 'SAME' ? ?Z2 = tf.nn.conv2d(P1,W2, strides = [1,1,1,1], padding = 'SAME') ? ?

? ?# RELU ? ?A2 = tf.nn.relu(Z2) ?

? ?# MAXPOOL: window 4x4, stride 4, padding 'SAME' ? ?P2 = tf.nn.max_pool(A2, ksize = [1,4,4,1], strides = [1,4,4,1], padding = 'SAME') ? ?

? ?# FLATTEN ? ?P2 = tf.contrib.layers.flatten(P2)

? ?Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn = None) ? ?

? ?return Z3

計(jì)算當(dāng)前損失

????? 在 Tensorflow ?中計(jì)算損失函數(shù)非常簡(jiǎn)單葵袭,一行代碼即可:

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

? ?""" ? ?cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y)) ? ?

? ?return cost

????? 定義好上述過(guò)程之后涵妥,就可以封裝整體的訓(xùn)練過(guò)程模型∑挛可能你會(huì)問(wèn)為什么沒(méi)有反向傳播蓬网,這里需要注意的是 Tensorflow 幫助我們自動(dòng)封裝好了反向傳播過(guò)程,無(wú)需我們?cè)俅味x鹉勒,在實(shí)際搭建過(guò)程中我們只需將前向傳播的網(wǎng)絡(luò)結(jié)構(gòu)定義清楚即可帆锋。

封裝模型

def model(X_train, Y_train, X_test, Y_test, learning_rate =0.009, ? ? ? ? ?num_epochs =100, minibatch_size =64, print_cost = True): ? ?

? ?"""

? ?Implements a three-layer ConvNet in Tensorflow:

? ?CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED

? ?Arguments:

? ?X_train -- training set, of shape (None, 64, 64, 3)

? ?Y_train -- test set, of shape (None, n_y = 6)

? ?X_test -- training set, of shape (None, 64, 64, 3)

? ?Y_test -- test set, of shape (None, n_y = 6)

? ?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:

? ?train_accuracy -- real number, accuracy on the train set (X_train)

? ?test_accuracy -- real number, testing accuracy on the test set (X_test)

? ?parameters -- parameters learnt by the model. They can then be used to predict.

? ?""" ? ?ops.reset_default_graph() ? ? ? ? ? ? ? ? ? ? ? ?

? ?tf.set_random_seed(1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?seed = 3 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?(m, n_H0, n_W0, n_C0) = X_train.shape ? ? ? ? ? ?

? ?n_y = Y_train.shape[1] ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?costs = [] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ?# Create Placeholders of the correct shape ? ?X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y) ?

? ?# Initialize parameters ? ?parameters = initialize_parameters() ? ?

? ?# Forward propagation ? ?Z3 = forward_propagation(X, parameters) ? ?

? ?# Cost function ? ?cost = compute_cost(Z3, Y) ? ?

? ?# Backpropagation ? ?optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost) ? ?# Initialize all the variables globally ? ?init = tf.global_variables_initializer() ? ?

? ?# 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):

? ? ? ? ? ?minibatch_cost = 0. ? ? ? ? ? ?num_minibatches = int(m / minibatch_size)

? ? ? ? ? ?seed = seed + 1 ? ? ? ? ? ?minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed) ? ? ? ? ? ?

? ? ? ? ? ?for minibatch in minibatches: ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?# Select a minibatch ? ? ? ? ? ? ? ?(minibatch_X, minibatch_Y) = minibatch

? ? ? ? ? ? ? ?_ , temp_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})

? ? ? ? ? ? ? ?minibatch_cost += temp_cost / num_minibatches ? ? ? ? ? ?

? ? ? ? ? ? ? ?# Print the cost every epoch ? ? ? ? ? ?if print_cost == True and epoch % 5 == 0: ? ? ? ? ? ? ?

? ? ? ?? ? ? ? print ("Cost after epoch %i: %f" % (epoch, minibatch_cost)) ? ? ? ? ? ?

? ? ? ?? ? if print_cost == True and epoch % 1 == 0:

? ? ? ? ? ? ? ?costs.append(minibatch_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() ? ? ? ?# Calculate the correct predictions ? ? ? ?predict_op = tf.argmax(Z3, 1)

? ? ? ?correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1)) ? ? ? ?

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

? ? ? ?print(accuracy)

? ? ? ?train_accuracy = accuracy.eval({X: X_train, Y: Y_train})

? ? ? ?test_accuracy = accuracy.eval({X: X_test, Y: Y_test})

? ? ? ?print("Train Accuracy:", train_accuracy)

? ? ? ?print("Test Accuracy:", test_accuracy) ? ? ?


? ? ? ?return train_accuracy, test_accuracy, parameters

???? 對(duì)訓(xùn)練集執(zhí)行模型訓(xùn)練:

_, _, parameters = model(X_train, Y_train, X_test, Y_test)

?????訓(xùn)練迭代過(guò)程如下:

????我們?cè)谟?xùn)練集上取得了 0.67 的準(zhǔn)確率,在測(cè)試集上的預(yù)測(cè)準(zhǔn)確率為 0.58 贸弥,雖然效果并不顯著窟坐,模型也有待深度調(diào)優(yōu),但我們已經(jīng)學(xué)會(huì)了如何用 Tensorflow ?快速搭建起一個(gè)深度學(xué)習(xí)系統(tǒng)了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市哲鸳,隨后出現(xiàn)的幾起案子臣疑,更是在濱河造成了極大的恐慌,老刑警劉巖徙菠,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件讯沈,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡婿奔,警方通過(guò)查閱死者的電腦和手機(jī)缺狠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)萍摊,“玉大人挤茄,你說(shuō)我怎么就攤上這事”荆” “怎么了穷劈?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)踊沸。 經(jīng)常有香客問(wèn)我歇终,道長(zhǎng),這世上最難降的妖魔是什么逼龟? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任评凝,我火速辦了婚禮,結(jié)果婚禮上腺律,老公的妹妹穿的比我還像新娘奕短。我一直安慰自己,他們只是感情好疾渣,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布篡诽。 她就那樣靜靜地躺著,像睡著了一般榴捡。 火紅的嫁衣襯著肌膚如雪杈女。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天吊圾,我揣著相機(jī)與錄音达椰,去河邊找鬼。 笑死项乒,一個(gè)胖子當(dāng)著我的面吹牛啰劲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播檀何,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蝇裤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼廷支!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起栓辜,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤恋拍,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后藕甩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體施敢,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年狭莱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了僵娃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腋妙,死狀恐怖默怨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骤素,我是刑警寧澤先壕,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站谆甜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏集绰。R本人自食惡果不足惜规辱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望栽燕。 院中可真熱鬧罕袋,春花似錦、人聲如沸碍岔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蔼啦。三九已至榆纽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間捏肢,已是汗流浹背奈籽。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鸵赫,地道東北人衣屏。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像辩棒,于是被迫代替她去往敵國(guó)和親狼忱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子膨疏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

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