TensorFlow 一步一步實現卷積神經網路

fendouai發表於2018-04-01

TensorFlow 從入門到精通系列教程:

http://www.tensorflownews.com/series/tensorflow-tutorial/

卷積層簡單封裝
# 池化操作
def conv2d(x, W, b, strides=1):
    # Conv2D wrapper, with bias and relu activation
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)
複製程式碼
TensorFlow max_pool 函式介紹:

tf.nn.max_pool(x, ksize, strides ,padding)

引數 x: 和 conv2d 的引數 x 相同,是一個 4 維張量,每一個維度分別代表 batch,in_height,in_height,in_channels。

引數 ksize: 池化核的大小,是一個 1 維長度為 4 的張量,對應引數 x 的 4 個維度上的池化大小。

引數 strides: 1 維長度為 4 的張量,對應引數 x 的 4 個維度上的步長。

引數 padding: 邊緣填充方式,主要是 "SAME", "VALID",一般使用 “SAME”。

接下來將會使用 TensorFlow 實現以下結構的卷積神經網路:

卷積層簡單封裝
def maxpool2d(x, k=2):
    # MaxPool2D wrapper
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],padding='SAME')

複製程式碼
卷積神經網路函式

超引數定義:

# 訓練引數
learning_rate = 0.001
num_steps = 200
batch_size = 128
display_step = 10

# 網路引數
#MNIST 資料維度
num_input = 784
#MNIST 列標數量
num_classes = 10
#神經元保留率
dropout = 0.75
複製程式碼

卷積神經網路定義:


# 卷積神經網路
def conv_net(x, weights, biases, dropout):
    x = tf.reshape(x, shape=[-1, 28, 28, 1])
    # 第一層卷積
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    # 第二層池化
    conv1 = maxpool2d(conv1, k=2)

    # 第三層卷積
    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    # 第四層池化
    conv2 = maxpool2d(conv2, k=2)

    #全連線層
    fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
    fc1 = tf.nn.relu(fc1)
    #丟棄
    fc1 = tf.nn.dropout(fc1, dropout)

    #輸出層,輸出最後的結果
    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    return out

複製程式碼

效果評估

#softmax 層
logits = conv_net(X, weights, biases, keep_prob)
prediction = tf.nn.softmax(logits)

#定義損失函式
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=logits, labels=Y))
#定義優化函式
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
#確定優化目標
train_op = optimizer.minimize(loss_op)


#獲得預測正確的結果
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
#求準確率
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

複製程式碼

訓練過程輸出

Step 1, Minibatch Loss= 92463.1406, Training Accuracy= 0.117
Step 10, Minibatch Loss= 28023.7285, Training Accuracy= 0.203
Step 20, Minibatch Loss= 13119.1172, Training Accuracy= 0.508
Step 30, Minibatch Loss= 5153.5215, Training Accuracy= 0.719
Step 40, Minibatch Loss= 4394.2578, Training Accuracy= 0.750
Step 50, Minibatch Loss= 4201.6006, Training Accuracy= 0.734
Step 60, Minibatch Loss= 2271.7676, Training Accuracy= 0.820
Step 70, Minibatch Loss= 2406.0142, Training Accuracy= 0.836
Step 80, Minibatch Loss= 3353.5925, Training Accuracy= 0.836
Step 90, Minibatch Loss= 1519.4861, Training Accuracy= 0.914
Step 100, Minibatch Loss= 1908.3972, Training Accuracy= 0.883
Step 110, Minibatch Loss= 2853.9766, Training Accuracy= 0.852
Step 120, Minibatch Loss= 2722.6582, Training Accuracy= 0.844
Step 130, Minibatch Loss= 1433.3765, Training Accuracy= 0.891
Step 140, Minibatch Loss= 3010.4907, Training Accuracy= 0.859
Step 150, Minibatch Loss= 1436.4202, Training Accuracy= 0.922
Step 160, Minibatch Loss= 791.8259, Training Accuracy= 0.938
Step 170, Minibatch Loss= 596.7582, Training Accuracy= 0.930
Step 180, Minibatch Loss= 2496.4136, Training Accuracy= 0.906
Step 190, Minibatch Loss= 1081.5593, Training Accuracy= 0.914
Step 200, Minibatch Loss= 783.2731, Training Accuracy= 0.930
Optimization Finished!
Testing Accuracy: 0.925781
複製程式碼

模型優化

經典卷積神經網路

影象分類實戰專案

The CIFAR-10 dataset

https://www.cs.toronto.edu/~kriz/cifar.html

目標檢測實戰專案

Tensorflow Object Detection API

https://github.com/tensorflow/models/tree/master/research/object_detection

主要參考物件:

1.TensorFlow 官方介紹

Image Recognition https://tensorflow.google.cn/tutorials/image_recognition

https://www.tensorflow.org/tutorials/deep_cnn

2.最經典論文

ImageNet Classification with Deep Convolutional Neural Networks http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks

3.最經典課程

Convolutional Neural Networks http://cs231n.github.io/convolutional-networks/

Deep learning http://neuralnetworksanddeeplearning.com/chap6.html

3.Wikipedia

Convolutional neural network https://en.wikipedia.org/wiki/Convolutional_neural_network

4.Good tutorial

Comparison of Normal Neural network

https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/convolutional_neural_networks.html

Convolutional Neural Networks (LeNet)

http://deeplearning.net/tutorial/lenet.html#sparse-connectivity

Convolutional neural networks from scratch

http://gluon.mxnet.io/chapter04_convolutional-neural-networks/cnn-scratch.html

卷積神經網路

http://prors.readthedocs.io/zh_CN/latest/2ndPart/Chapter8.SceneClassification/ConvNet.html

ImageNet Classification with Deep Convolutional Neural Networks

https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf

相關文章