天天看點

TensorFlow詳解貓狗識别(二)--定義神經網絡

這裡附上親測的兩個神經網絡模型Lenet5&AlexNet7以及損失函數loss,優化器反向傳播,評估函數evaluation

介紹

LeNet5:LeNet5誕生于1994年,是最早的卷積神經網絡之一, 并且推動了深度學習領域的發展。自從1988年開始,在許多次成功的疊代後,這項由Yann LeCun完成的開拓性成果被命名為LeNet5。

AlexNet:AlexNet是2012年ImageNet競賽冠軍獲得者Hinton和他的學生Alex Krizhevsky設計的。也是在那年之後,更多的更深的神經網路被提出,比如優秀的vgg,GoogleLeNet。其官方提供的資料模型,準确率達到57.1%,top 1-5 達到80.2%. 這項對于傳統的機器學習分類算法而言,已經相當的出色。

下面貼出神經網絡代碼

model01(LeNet5)

import tensorflow as tf

def inference(images, batch_size, n_classes):
    # 一個簡單的卷積神經網絡,卷積+池化層x2,全連接配接層x2,最後一個softmax層做分類。
    # 卷積層1
    # 16個3x3的卷積核(3通道),padding=’SAME’,表示padding後卷積的圖與原圖尺寸一緻,激活函數relu()
    with tf.variable_scope('conv1') as scope:
        #tf.tuncated_normal從截斷的正态分布中輸出随機值,
        # 生成的值服從具有指定平均值和标準偏差的狀态分布,如果生成的值大于平均值兩個标準偏差的值,則丢棄
        #stddev正太分布的标準差
        weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 16], stddev=0.1, dtype=tf.float32),
                              name='weights', dtype=tf.float32)
        #tf.constant初始化常量
        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[16]),
                             name='biases', dtype=tf.float32)
        #nn.conv2d,第一個參數為input,指需要做卷積的輸入圖像,第二個參數,卷積核,第三個參數步長,
        # 第四個設定為SAME表示可以停留在圖像邊上
        conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding='SAME')
        pre_activation = tf.nn.bias_add(conv, biases)
        conv1 = tf.nn.relu(pre_activation, name=scope.name)
    # 池化層1
    # 3x3最大池化,步長strides為2,池化後執行lrn()操作,局部響應歸一化,對訓練有利。
    with tf.variable_scope('pooling1_lrn') as scope:
        #第一個參數,需要池化的輸入
        #第二個參數池化視窗的大小
        #第三個參數步長
        #第四個參數同上
        pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pooling1')
        #
        norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
    # 卷積層2
    # 16個3x3的卷積核(16通道),padding=’SAME’,表示padding後卷積的圖與原圖尺寸一緻,激活函數relu()
    with tf.variable_scope('conv2') as scope:
        weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 16, 16], stddev=0.1, dtype=tf.float32),
                              name='weights', dtype=tf.float32)
        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[16]),
                             name='biases', dtype=tf.float32)

        conv = tf.nn.conv2d(norm1, weights, strides=[1, 1, 1, 1], padding='SAME')
        pre_activation = tf.nn.bias_add(conv, biases)
        conv2 = tf.nn.relu(pre_activation, name='conv2')
    # 池化層2
    # 3x3最大池化,步長strides為2,池化後執行lrn()操作,
    # pool2 and norm2

    with tf.variable_scope('pooling2_lrn') as scope:
        norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
        pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME', name='pooling2')
    # 全連接配接層3
    # 128個神經元,将之前pool層的輸出reshape成一行,激活函數relu()

    with tf.variable_scope('local3') as scope:
        reshape = tf.reshape(pool2, shape=[batch_size, -1])
        dim = reshape.get_shape()[1].value
        weights = tf.Variable(tf.truncated_normal(shape=[dim, 128], stddev=0.005, dtype=tf.float32),
                              name='weights', dtype=tf.float32)
        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
                             name='biases', dtype=tf.float32)
        local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
    # 全連接配接層4

    # 128個神經元,激活函數relu()

    with tf.variable_scope('local4') as scope:
        weights = tf.Variable(tf.truncated_normal(shape=[128, 128], stddev=0.005, dtype=tf.float32),
                              name='weights', dtype=tf.float32)
        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
                             name='biases', dtype=tf.float32)
        local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4')

    # dropout層
    #    with tf.variable_scope('dropout') as scope:
    #        drop_out = tf.nn.dropout(local4, 0.8)
    # Softmax回歸層

    # 将前面的FC層輸出,做一個線性回歸,計算出每一類的得分,在這裡是2類,是以這個層輸出的是兩個得分。
    with tf.variable_scope('softmax_linear') as scope:
        weights = tf.Variable(tf.truncated_normal(shape=[128, n_classes], stddev=0.005, dtype=tf.float32),
                              name='softmax_linear', dtype=tf.float32)
        biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[n_classes]),
                             name='biases', dtype=tf.float32)
        softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear')
    return softmax_linear

# -----------------------------------------------------------------------------
# loss計算
# 傳入參數:logits,網絡計算輸出值。labels,真實值,在這裡是0或者1
# 傳回參數:loss,損失值



def losses(logits, labels):
    with tf.variable_scope('loss') as scope:
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,
                                                                       name='xentropy_per_example')
        loss = tf.reduce_mean(cross_entropy, name='loss')
        tf.summary.scalar(scope.name + '/loss', loss)
    return loss

# --------------------------------------------------------------------------
# loss損失值優化
# 輸入參數:loss。learning_rate,學習速率。
# 傳回參數:train_op,訓練op,這個參數要輸入sess.run中讓模型去訓練。
def trainning(loss, learning_rate):
    with tf.name_scope('optimizer'):
        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        global_step = tf.Variable(0, name='global_step', trainable=False)
        train_op = optimizer.minimize(loss, global_step=global_step)
    return train_op

# -----------------------------------------------------------------------

# 評價/準确率計算

# 輸入參數:logits,網絡計算值。labels,标簽,也就是真實值,在這裡是0或者1。

# 傳回參數:accuracy,目前step的平均準确率,也就是在這些batch中多少張圖檔被正确分類了。

def evaluation(logits, labels):
    with tf.variable_scope('accuracy') as scope:
        correct = tf.nn.in_top_k(logits, labels, 1)
        correct = tf.cast(correct, tf.float16)
        accuracy = tf.reduce_mean(correct)
        tf.summary.scalar(scope.name + '/accuracy', accuracy)
    return accuracy
           

model02(AlexNet)

import tensorflow as tf
import numpy as np


def AlexNet(X, KEEP_PROB, NUM_CLASSES):
    """Create the network graph."""
    # 1st Layer: Conv (w ReLu) -> Lrn -> Pool
    conv1 = conv(X, [5, 5, 3, 64], [64], 1, 1, name='conv1')
    norm1 = lrn(conv1, 2, 1e-05, 0.75, name='norm1')
    pool1 = max_pool(norm1, 2, 2, 2, 2, name='pool1')  ##64*64*64
    # 2nd Layer: Conv (w ReLu)  -> Lrn -> Pool with 2 groups
    conv2 = conv(pool1, [5, 5, 64, 128], [128], 1, 1, name='conv2')
    norm2 = lrn(conv2, 2, 1e-05, 0.75, name='norm2')
    pool2 = max_pool(norm2, 2, 2, 2, 2, name='pool2')  ##32*32*128
    # 3rd Layer: Conv (w ReLu)
    conv3 = conv(pool2, [3, 3, 128, 256], [256], 1, 1, name='conv3')
    # 4th Layer: Conv (w ReLu) splitted into two groups
    conv4 = conv(conv3, [3, 3, 256, 512], [512], 1, 1, name='conv4')
    # 5th Layer: Conv (w ReLu) -> Pool splitted into two groups
    conv5 = conv(conv4, [3, 3, 512, 512], [512], 1, 1, name='conv5')
    pool5 = max_pool(conv5, 2, 2, 2, 2, name='pool5')
    # 6th Layer: Flatten -> FC (w ReLu) -> Dropout
    flattened = tf.reshape(pool5, [-1, 16 * 16 * 512])
    fc6 = fc(flattened, [16 * 16 * 512, 1024], [1024], name='fc6')
    fc6 = tf.nn.relu(fc6)
    dropout6 = dropout(fc6, KEEP_PROB)
    # 7th Layer: FC (w ReLu) -> Dropout
    fc7 = fc(dropout6, [1024, 2048], [2048], name='fc7')
    fc7 = tf.nn.relu(fc7)
    dropout7 = dropout(fc7, KEEP_PROB)
    # 8th Layer: FC and return unscaled activations
    fc8 = fc(dropout7, [2048, NUM_CLASSES], [NUM_CLASSES], name='fc8')
    return fc8


def conv(x, kernel_size, bias_size, stride_y, stride_x, name):
    with tf.variable_scope(name) as scope:
        weights = tf.get_variable('weights',
                                  shape=kernel_size,
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable('biases',
                                 shape=bias_size,
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        conv = tf.nn.conv2d(x, weights, strides=[1, stride_y, stride_x, 1], padding='SAME')
        pre_activation = tf.nn.bias_add(conv, biases, name=scope.name)
    return pre_activation


def fc(x, kernel_size, bias_size, name):
    """Create a fully connected layer."""
    with tf.variable_scope(name) as scope:
        weights = tf.get_variable('weights',
                                  shape=kernel_size,
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable('biases',
                                 shape=bias_size,
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        softmax_linear = tf.add(tf.matmul(x, weights), biases, name=scope.name)
    return softmax_linear


def max_pool(x, filter_height, filter_width, stride_y, stride_x, name, padding='SAME'):
    """Create a max pooling layer."""
    return tf.nn.max_pool(x, ksize=[1, filter_height, filter_width, 1],
                          strides=[1, stride_y, stride_x, 1],
                          padding=padding, name=name)

def lrn(x, radius, alpha, beta, name, bias=1.0):
    """Create a local response normalization layer."""
    return tf.nn.local_response_normalization(x, depth_radius=radius,
                                              alpha=alpha, beta=beta,
                                              bias=bias, name=name)

def dropout(x, keep_prob):
    """Create a dropout layer."""

    return tf.nn.dropout(x, keep_prob)

def losses(logits, labels):
    with tf.variable_scope('loss') as scope:
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,
                                                                       name='xentropy_per_example')
        loss = tf.reduce_mean(cross_entropy, name='loss')
        tf.summary.scalar(scope.name + '/loss', loss)
    return loss

# --------------------------------------------------------------------------
# loss損失值優化
# 輸入參數:loss。learning_rate,學習速率。
# 傳回參數:train_op,訓練op,這個參數要輸入sess.run中讓模型去訓練。
def trainning(loss, learning_rate):
    with tf.name_scope('optimizer'):
        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        global_step = tf.Variable(0, name='global_step', trainable=False)
        train_op = optimizer.minimize(loss, global_step=global_step)
    return train_op

# -----------------------------------------------------------------------

# 評價/準确率計算

# 輸入參數:logits,網絡計算值。labels,标簽,也就是真實值,在這裡是0或者1。

# 傳回參數:accuracy,目前step的平均準确率,也就是在這些batch中多少張圖檔被正确分類了。

def evaluation(logits, labels):
    with tf.variable_scope('accuracy') as scope:
        correct = tf.nn.in_top_k(logits, labels, 1)
        correct = tf.cast(correct, tf.float16)
        accuracy = tf.reduce_mean(correct)
        tf.summary.scalar(scope.name + '/accuracy', accuracy)
    return accuracy
           

下一篇:開始訓練模型!

繼續閱讀