1. 程式人生 > >深度學習之影象分類模型AlexNet結構分析和tensorflow實現

深度學習之影象分類模型AlexNet結構分析和tensorflow實現

在ImageNet上的影象分類challenge上,Hinton和他的學生Alex Krizhevsky提出的AlexNet網路結構模型贏得了2012屆的冠軍,重新整理了Image Classification的機率。因此,要研究CNN型別深度學習模型在影象分類上的應用,AlexNet就不得不談,這是CNN在影象分類上的經典模型。

下面先看看AlexNet的結構圖:

下面對其結構進行詳細的分析,具體分析過程請看下面的流程圖:

  1. conv1階段DFD(data flow diagram):

In [13]:

Image(filename="/Users/youwei.tan/Documents/1.png")

Out[13]:

  1. conv2階段DFD(data flow diagram):

In [6]:

Image(filename="/Users/youwei.tan/Documents/2.png")

Out[6]:

  1. conv3階段DFD(data flow diagram):

In [7]:

Image(filename="/Users/youwei.tan/Documents/3.png")

Out[7]:

  1. conv4階段DFD(data flow diagram):

In [8]:

Image(filename="/Users/youwei.tan/Documents/4.png")

Out[8]:

  1. conv5階段DFD(data flow diagram):

In [9]:

Image(filename="/Users/youwei.tan/Documents/5.png")

Out[9]:

  1. fc6階段DFD(data flow diagram):

In [10]:

Image(filename="/Users/youwei.tan/Documents/6.png")

Out[10]:

  1. fc7階段DFD(data flow diagram):

In [11]:

Image(filename="/Users/youwei.tan/Documents/7.png")

Out[11]:

  1. fc8階段DFD(data flow diagram):

In [12]:

Image(filename="/Users/youwei.tan/Documents/8.png")

Out[12]:

理解了AlexNet模型的結構,實現AlexNet的程式碼應該不難了,在網上看到了已經有大神用tensorflow實現了AlexNet(程式碼出處:http://blog.csdn.net/chenriwei2/article/details/50615753 ) 下面直接搬過來,具體程式碼如下:

In [6]:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# load資料
# import input_data
# mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# 定義網路超引數
learning_rate = 0.001
training_iters = 200000
batch_size = 64
display_step = 20

# 定義網路引數
n_input = 784 # 輸入的維度
n_classes = 10 # 標籤的維度
dropout = 0.8 # Dropout 的概率

# 佔位符輸入
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)

# 卷積操作
def conv2d(name, l_input, w, b):
    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name)

# 最大下采樣操作
def max_pool(name, l_input, k):
    return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)

# 歸一化操作
def norm(name, l_input, lsize=4):
    return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)

# 定義整個網路 
def alex_net(_X, _weights, _biases, _dropout):
    # 向量轉為矩陣
    _X = tf.reshape(_X, shape=[-1, 28, 28, 1])

    # 卷積層
    conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])
    # 下采樣層
    pool1 = max_pool('pool1', conv1, k=2)
    # 歸一化層
    norm1 = norm('norm1', pool1, lsize=4)
    # Dropout
    norm1 = tf.nn.dropout(norm1, _dropout)

    # 卷積
    conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])
    # 下采樣
    pool2 = max_pool('pool2', conv2, k=2)
    # 歸一化
    norm2 = norm('norm2', pool2, lsize=4)
    # Dropout
    norm2 = tf.nn.dropout(norm2, _dropout)

    # 卷積
    conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])
    # 下采樣
    pool3 = max_pool('pool3', conv3, k=2)
    # 歸一化
    norm3 = norm('norm3', pool3, lsize=4)
    # Dropout
    norm3 = tf.nn.dropout(norm3, _dropout)

    # 全連線層,先把特徵圖轉為向量
    dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) 
    dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') 
    # 全連線層
    dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation

    # 網路輸出層
    out = tf.matmul(dense2, _weights['out']) + _biases['out']
    return out

# 儲存所有的網路引數
weights = {
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64])),
    'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),
    'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),
    'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])),
    'wd2': tf.Variable(tf.random_normal([1024, 1024])),
    'out': tf.Variable(tf.random_normal([1024, 10]))
}
biases = {
    'bc1': tf.Variable(tf.random_normal([64])),
    'bc2': tf.Variable(tf.random_normal([128])),
    'bc3': tf.Variable(tf.random_normal([256])),
    'bd1': tf.Variable(tf.random_normal([1024])),
    'bd2': tf.Variable(tf.random_normal([1024])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# 構建模型
pred = alex_net(x, weights, biases, keep_prob)

# 定義損失函式和學習步驟
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# 測試網路
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# 初始化所有的共享變數
init = tf.initialize_all_variables()

# 開啟一個訓練
with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        # 獲取批資料
        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
        if step % display_step == 0:
            # 計算精度
            acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
            # 計算損失值
            loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
            print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))
        step += 1
    print ("Optimization Finished!")
    # 計算測試精度
    print ("Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}))