目錄
二、使用tensorflow Estimator構建CNN MNIST分類器
一、卷積神經網絡CNN的結構
卷積神經網絡通常包含卷積層、池化層和全連接層,下面是利用CNN對圖片分類的結構圖示例:
1、卷積層CONV
過濾器:
獲取輸入網絡層局部區域(包含輸入網絡層所有深度區域)的某方面特徵,過濾器移動覆蓋上層網絡的所有區域,輸出網絡層相同深度的數據共享同一個過濾器。如下圖所示,紅色局部區域,通過一個過濾器,對應一個(下一網絡層)藍色神經元,五個神經元代表應用了五個過濾器,分別獲取了五個方面的特徵。輸入層32*32*3,過濾器的大小爲5*5,每一個神經元參數個數爲5*5*3+1。
輸出層:
輸出層長、寬計算 = (W-F+2P)/S+1
W爲輸入層大小,F爲過濾器大小,P爲0填充維度,S爲滑動步長。以下圖爲例,只展示一維數據,輸入層爲5*5(W),過濾層爲3*3(F),0填充維度爲1(P)。若步長爲1(S),輸出層爲5*5;若步長爲2,輸出層爲3*3。其中[1,0,-1]是過濾器權重。
輸出層深度 = 過濾器個數
卷積層的計算過程:
下圖是一個卷積層計算過程,輸入層爲5*5*3,0填充維度爲1,過濾器大小爲3*3,移動步長爲2,過濾器個數爲2個,輸出層維度爲3*3*2 ( (5+2*1-3)/2+1=3 )。
卷積神經網絡與傳統神經網絡的區別在於相鄰兩層的連接方式,卷積神經網絡爲局部連接,傳統神經網絡爲全連接。卷積層涉及的參數個數爲(3*3*3+1)*2=56;如果爲傳統神經網絡,輸入層元素個數爲147,輸出層元素個數爲18,採用全連接,參數個數爲(147+1)*18=2664。
2、池化層POOL
對卷積層提取的圖像數據進行向下採樣,以降低特徵圖的維度,從而減少參數個數以及縮短處理時間。
過濾器:
常用的過濾器大小爲2*2,步長爲2,對輸入的每個深度層的數據分別進行池化。常用的池化算法是最大池化,它會提取特徵圖的子區域,保留子區域的最大值,並捨棄其他所有值。以下圖所示採用最大池化,輸入層爲4*4,過濾器爲2*2,步長爲2,輸出層爲2*2。
輸出層:
輸出層長計算=(W1-F)/S+1
輸出層寬計算=(H1-F)/S+1
輸出層深度計算=D1
W1爲輸入層的長,H1爲輸入層的寬,D1爲輸入層的深度,F爲過濾器大小,S爲滑動步長,通常沒有0填充。由於池化層對每層數據分別池化,所以輸出層的深度與輸入層深度相同。
如下圖所示,輸入層224*224*64,池化層過濾器爲2*2,步長爲2,輸出層爲112*112*64。( (224-2)/2+1=112 )
3、全連接層FC
對由卷積層提取並由池化層下采樣的特徵進行分類。全連接層中的每個節點都連接到前一層中的所有節點。
在CNN的最終全連接層中,模型中的每個目標類別都對應一個節點,並應用softmax激活函數,對每個節點生成一個介於0到1之間的值,表示爲該圖像屬於每個目標類別的概率。
二、使用tensorflow Estimator構建CNN MNIST分類器
1、CNN模型函數
CNN卷積神經網絡架構:
卷積層1:應用32個5*5過濾器(提取5*5像素的子區域),並應用ReLU激活函數
池化層1:使用2*2過濾器和步長爲2,執行最大池化運算
卷積層2:應用64個5*5過濾器,並應用ReLU激活函數
池化層2:使用2*2過濾器和步長爲2,執行最大池化運算
全連接層1:包含1024個神經元,其中丟棄正則化率爲0.4(任何指定元素在訓練期間被丟棄的概率爲0.4)
全連接層2:包含10個神經元,每個數字目標類別(0-9)對應一個神經元
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Imports
import numpy as np
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
def cnn_model_fn(features, labels, mode):
"""Model function for CNN."""
# Input Layer--[batch_size, image_height, image_width, channels], batch_size=-1根據輸入值的數量動態計算此維度
#MNIST 數據集由28*28像素的單色圖像組成
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
# Convolutional Layer #1, padding=same:向輸入張量的邊緣添加0,使輸出張量與輸入張量具有相同的高度和寬度值
#將32個5*5過濾器應用到輸入層,並應用RELU激活函數,輸出層爲28*28*32
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
# Pooling Layer #1
#使用2*2過濾器和步長2執行最大池化運算,輸出層爲14*14*32
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
# Convolutional Layer #2 and Pooling Layer #2
#將64個5*5過濾器應用到輸入層,並應用RELU激活函數,輸出層爲14*14*64
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
# Pooling Layer #2
#使用2*2過濾器和步長2執行最大池化運算,輸出層爲7*7*64
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
# Dense Layer #1
#輸入張量爲扁平化後的特徵向量, 輸出層的神經元數量爲1024
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)#丟棄正則化,防止過擬合
# Logits Layer #2
#一個具有10個神經元的全連接層
logits = tf.layers.dense(inputs=dropout, units=10)
#生成預測--每個樣本的預測類別:介於0到9之間的數字;每個樣本屬於每個可能的目標類別的概率。
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) #多類別分類問題,採用softmax交叉熵用作損失函數
# Configure the Training Op (for TRAIN mode)
#採用隨機梯度下降法優化損失函數,學習速率爲0.001
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode), 準確率指標
eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])}
return tf.estimator.EstimatorSpec( mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
2、訓練和評估CNN分類器
加載訓練和測試數據:
# Load training and eval data
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
創建Estimator,一種用於執行高級模型訓練、評估和推理的 TensorFlow 類
# Create the Estimator
mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")
模型訓練
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_data},
y=train_labels,
batch_size=100, #模型會在每一步訓練100個小批次樣本
num_epochs=None, #模型一直訓練,直到達到指定的訓練步數
shuffle=True) #隨機化處理訓練數據
mnist_classifier.train(
input_fn=train_input_fn,
steps=20000) #模型總共要訓練20000步
模型評估
# Evaluate the model and print results
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": eval_data},
y=eval_labels,
num_epochs=1,
shuffle=False)
eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)
模型結果:
{'accuracy': 0.9726, 'global_step': 23052, 'loss': 0.08958568}
在測試數據集上的準確率達到97.3%
參考資料:
https://cs231n.github.io/convolutional-networks/#conv
https://www.tensorflow.org/tutorials/estimators/cnn#run_the_model