1. 程式人生 > >Tensorflow例項:實現AlexNet

Tensorflow例項:實現AlexNet

AlexNet以顯著的優勢贏得了ILSVRC 2012比賽的冠軍,可以說是神經網路在低谷期後的第一次發聲,確立了深度學習在計算機視覺的統治地位,同時也推動了深度學習在語音識別、自動語言處理、強化學習等領域的擴充套件。
AlexNet將LeNet的思想發揚光大,把CNN的基礎原理應用到了很深很寬的網路中。AlexNet主要使用到的新技術點如下:

  1. 成功使用ReLU作為CNN的啟用函式,並驗證其效果在較深的網路超過了Sigmoid,成功解決了Sigmoid在網路較深時的梯度彌散問題。
  2. 訓練時使用Dropout隨機忽略一部分神經元,以避免過擬合
  3. 在CNN中使用重疊的最大池化。此前CNN中普遍使用平均池化,AlexNet全部使用最大池化,避免平均池化的模糊化效果。並且AlexNet中提出讓步長比池化核的尺寸小,這樣池化層的輸出層之間會有重疊和覆蓋,提升了特徵的豐富性。
  4. 提出了LRN層,對區域性神經元的活動建立競爭機制,使得其中響應比較大的值變得相對更大,並抑制其他反饋較小的神經元,增強了模型的泛化能力。
  5. 使用cuda加速深度卷積網路的訓練,利用GPU強大的平行計算能力,處理神經網路訓練時大量的矩陣運算。
  6. 資料增強,隨機地從256*256的原始影象中擷取224*224大小的區域。如果沒有資料增強,僅靠原始的資料量,引數眾多的CNN會陷入過擬閤中,使用了資料增強後可以大大減輕過擬合,提升泛化能力。

整個AlexNet包含8個需要訓練引數的層(不包括池化層和LRN層),前5層為卷積層,後3層為全連線層。使用ImageNet資料集訓練一個完整的AlexNet耗時非常長,因此本文AlexNet的實現將不涉及實際資料的訓練。我們會建立一個完整的AlexNet卷積神經網路,然後對它每個batch的前饋計算(forward)和反饋計算(backward)的速度進行測試。這裡使用隨機圖片資料來計算每輪前饋、反饋的平均耗時。

from datetime import datetime
import math
import time
import tensorflow as tf

batch_size = 32
num_batches = 100

# 定義一個用來顯示網路每一層結構的函式print_actications,展示每一個卷積層或池化層輸出tensor的尺寸。
# 這個函式接受一個tensor作為輸入,並顯示其名稱(t.op.name)和tensor尺寸(t.get_shape.as_list())

def print_activation(t):
    print(t.op.name, ' ', t.get_shape().as_list())

# 接下來設計AlexNet的網路結構。並將可訓練的引數kernel、biases新增到parameters中
def inference(iamges): parameters = [] # 定義第一個卷積層,並在其後新增LRN層和最大池化層 with tf.name_scope('conv1') as scope: kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64], dtype=tf.float32, stddev=1e-1), name='weights') conv = tf.nn.conv2d(iamges, kernel, strides=[1, 4, 4, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), trainable=True, name='biases') bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] lrn1 = tf.nn.lrn(conv1, 4, bias=1.0, alpha=0.001/9, beta=0.75, name='lrn1') pool1 = tf.nn.max_pool(lrn1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1') print_activation(pool1) # 接下來設計第2個卷積層,大部分步驟和第1個卷積層相同,只有幾個引數不同。 with tf.name_scope('conv2') as scope: kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192], dtype=tf.float32, stddev=1e-1), name='weights') conv = tf.nn.conv2d(pool1, kernel, strides=[1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape=[192], dtype=tf.float32), trainable=True, name='biases') bias = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9, beta=0.75, name='lrn2') pool2 = tf.nn.max_pool(lrn2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool2') print_activation(pool2) # 定義第3個卷積層 with tf.name_scope('conv3') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384], dtype=tf.float32, stddev=1e-1), name='weights') conv = tf.nn.conv2d(pool2, kernel, strides=[1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[384]), trainable=True, name='biases') bias = tf.nn.bias_add(conv, biases) conv3 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activation(conv3) # 定義第4個卷積層 with tf.name_scope('conv4') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256], dtype=tf.float32, stddev=1e-1), name='weights') conv = tf.nn.conv2d(conv3, kernel, strides=[1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[256]), trainable=True, name='biases') bias = tf.nn.bias_add(conv, biases) conv4 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activation(conv4) # 定義第5個卷積層 with tf.name_scope('conv5') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, stddev=1e-1), name='weights') conv = tf.nn.conv2d(conv4, kernel, strides=[1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[256]), trainable=True, name='biases') bias = tf.nn.bias_add(conv, biases) conv5 = tf.nn.relu(bias, name=scope) parameters += [kernel, biases] print_activation(conv5) pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool5') print_activation(pool5) return pool5, parameters # 接下來實現一個評估AlexNet每輪計算時間的函式time_tensorflow_run.第一個輸入是Tensorflow的Session, # 第二個變數是需要測評的運算運算元,第三個變數是測試的名稱。先定義預熱輪數num_steps_burn_in=10,它的作用是 # 給程式預熱 def time_tensorflow_run(session, target, info_string): num_steps_burn_in = 10 total_duration = 0.0 total_duration_squared = 0.0 for i in range(num_batches + num_steps_burn_in): start_time = time.time() _ = session.run(target) duration = time.time() if i >= num_steps_burn_in: if not i % 10: print('%s: step %d, duration = %.3f' % (datetime.now(), i - num_steps_burn_in, duration)) total_duration += duration total_duration_squared += duration * duration mn = total_duration / num_batches vr = total_duration_squared / num_batches - mn * mn sd = math.sqrt(vr) print('%s: %s across %d steps, %.3f + +/- %.3f sec / batch' % (datetime.now(), info_string, num_batches, mn, sd)) # 定義主函式 def run_benchmark(): with tf.Graph().as_default(): image_size = 224 images = tf.Variable(tf.random_normal([batch_size, image_size, image_size, 3], dtype=tf.float32, stddev=1e-1)) pool5, parameters = inference(images) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) time_tensorflow_run(sess, pool5, "Forward") objective = tf.nn.l2_loss(pool5) grad = tf.gradients(objective, parameters) time_tensorflow_run(sess, grad, "Forward-backward") run_benchmark()

執行結果:
這裡寫圖片描述