1. 程式人生 > >基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(2)

基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(2)

2. 卷積神經網路模型的構造——model.py
  關於神經網路模型不想說太多,視訊中使用的模型是仿照TensorFlow的官方例程cifar-10的網路結構來寫的。就是兩個卷積層(每個卷積層後加一個池化層),兩個全連線層,最後一個softmax輸出分類結果。

import tensorflow as tf

def inference(images, batch_size, n_classes):
    # conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
    with tf.variable_scope("conv1"
) as scope: weights = tf.get_variable("weights", shape=[3, 3, 3, 16], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) biases = tf.get_variable("biases"
, shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) 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="conv1"
) # pool1 && norm1 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') # conv2 with tf.variable_scope("conv2") as scope: weights = tf.get_variable("weights", shape=[3, 3, 16, 16], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) biases = tf.get_variable("biases", shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) 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") # pool2 && norm2 with tf.variable_scope("pooling2_lrn") as scope: pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="SAME", name="pooling2") norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm2') # full-connect1 with tf.variable_scope("fc1") as scope: reshape = tf.reshape(norm2, shape=[batch_size, -1]) dim = reshape.get_shape()[1].value weights = tf.get_variable("weights", shape=[dim, 128], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32)) biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1") # full_connect2 with tf.variable_scope("fc2") as scope: weights = tf.get_variable("weights", shape=[128, 128], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32)) biases = tf.get_variable("biases", shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) fc2 = tf.nn.relu(tf.matmul(fc1, weights) + biases, name="fc2") # softmax with tf.variable_scope("softmax_linear") as scope: weights = tf.get_variable("weights", shape=[128, n_classes], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32)) biases = tf.get_variable("biases", shape=[n_classes], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) softmax_linear = tf.add(tf.matmul(fc2, weights), biases, name="softmax_linear") softmax_linear = tf.nn.softmax(softmax_linear) return softmax_linear

  發現程式裡面有很多with tf.variable_scope("name")的語句,這其實是TensorFlow中的變數作用域機制,目的是有效便捷地管理需要的變數。
  變數作用域機制在TensorFlow中主要由兩部分組成:

  • tf.get_variable(<name>, <shape>, <initializer>): 建立一個變數
  • tf.variable_scope(<scope_name>): 指定名稱空間

如果需要共享變數,需要通過reuse_variables()方法來指定,詳細的例子去官方文件中看就好了。(連結在部落格參考部分)

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


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


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

   函式losses(logits, labels)用於計算訓練過程中的loss,這裡輸入引數logtis是函式inference()的輸出,代表圖片對貓和狗的預測概率,labels則是圖片對應的標籤。
  通過在程式中設定斷點,檢視logtis的值,結果如下圖所示,根據這個就很好理解了,一個數值代表屬於貓的概率,一個數值代表屬於狗的概率,兩者的和為1。

logtis變數

  而函式tf.nn.sparse_sotfmax_cross_entropy_with_logtis從名字就很好理解,是將稀疏表示的label與輸出層計算出來結果做對比。然後因為訓練的時候是16張圖片一個batch,所以再用tf.reduce_mean求一下平均值,就得到了這個batch的平均loss。
  training(loss, learning_rate)就沒什麼好說的了,loss是訓練的loss,learning_rate是學習率,使用AdamOptimizer優化器來使loss朝著變小的方向優化。
  evaluation(logits, labels)功能是在訓練過程中實時監測驗證資料的準確率,達到反映訓練效果的作用。

參考

補充

  本來是自己之前犯懶,最後一篇關於訓練的部落格沒寫=0=,鑑於不少人想要訓練程式碼,這裡我就從簡貼一下程式碼好了,大夥將就著看看,最近自己的事比較多,不想再把最開始的程式碼拿來翻了(剛開始寫的太醜了)。

import os
import numpy as np
import tensorflow as tf
import input_data
import model

N_CLASSES = 2
IMG_H = 208
IMG_W = 208
BATCH_SIZE = 32
CAPACITY = 2000
MAX_STEP = 15000
learning_rate = 0.0001


def run_training():
    train_dir = "data\\train\\"
    logs_train_dir = "logs\\"

    train, train_label = input_data.get_files(train_dir)
    train_batch, train_label_batch = input_data.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE,
                                                          CAPACITY)
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
    train_loss = model.losses(train_logits, train_label_batch)
    train_op = model.trainning(train_loss, learning_rate)
    train_acc = model.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

            if step % 100 == 0:
                print("Step %d, train loss = %.2f, train accuracy = %.2f%%" % (step, tra_loss, tra_acc))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            if step % 2000 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
                saver.save(sess, checkpoint_path, global_step=step)
    except tf.errors.OutOfRangeError:
        print("Done training -- epoch limit reached.")
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()

# 評估模型
from PIL import Image
import matplotlib.pyplot as plt


def get_one_image(train):
    n = len(train)
    ind = np.random.randint(0, n)
    img_dir = train[ind]

    image = Image.open(img_dir)
    plt.imshow(image)
    plt.show()
    image = image.resize([208, 208])
    image = np.array(image)
    return image


def evaluate_one_image():
    train_dir = "C:\\Users\\panch\\Documents\\PycharmProjects\\Cats_vs_Dogs\\data\\train\\"
    train, train_label = input_data.get_files(train_dir)
    image_array = get_one_image(train)

    with tf.Graph().as_default():
        BATCH_SIZE = 1
        N_CLASSES = 2

        image = tf.cast(image_array, tf.float32)
        image = tf.reshape(image, [1, 208, 208, 3])
        logit = model.inference(image, BATCH_SIZE, N_CLASSES)
        logit = tf.nn.softmax(logit)

        x = tf.placeholder(tf.float32, shape=[208, 208, 3])

        logs_train_dir = "C:\\Users\\panch\\Documents\\PycharmProjects\\Cats_vs_Dogs\\logs\\"
        saver = tf.train.Saver()

        with tf.Session() as sess:
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split("/")[-1].split("-")[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print("Loading success, global_step is %s" % global_step)
            else:
                print("No checkpoint file found")

            prediction = sess.run(logit, feed_dict={x: image_array})
            max_index = np.argmax(prediction)
            if max_index == 0:
                print("This is a cat with possibility %.6f" % prediction[:, 0])
            else:
                print("This is a dog with possibility %.6f" % prediction[:, 1])


run_training()
# evaluate_one_image()