本文連接:https://blog.csdn.net/u011961856/article/details/77984667
函數解析
github 代碼:https://github.com/adonistio/inception-face-shape-classifierpython
CLASSIFY_FACE.py
1
用於運行訓練好的Inception model,對輸入圖像進行分類.git
CLASSIFY_FACE_CONFUSION.py
1
與CLASSIFY_FACE.PY相似,可是講述如結果和一個困惑度矩陣保存在文本文件中.github
EXTRACT_FEATURES.py
1
這個腳本用於檢測圖像中的人臉,即bounding box,檢測特徵點,並提取人臉特徵用於訓練.網絡
PROCESS_IMAGE.py
1
包含幾個圖像預處理和加強函數,例如圖像平方,濾波,模糊,旋轉,翻轉等.函數
RETRAIN_CMDGEN.py
1
獲得CMD窗口命令,以從新訓練Inception V3 model.測試
RETRAIN_v2.py
1
將測試圖片設置爲包含全部的圖像,解決了驗證時的double counting 等問題..net
TRAIN_CLASSIFIERS.py
1
用於訓練LDA, SVM-LIN, SVM-RBF, MLP, KNN分類模型.scala
bottlenecks.rar
1
包含全部500張圖像的bottleneck files, bottleneck files爲圖像的向量表示,向量爲Inception model的最後一層的輸出.orm
features.txt
1
包含LDA,SVM,KNN,MLP分類中使用的特徵向量.blog
原理
inceptionV2網絡結構:
採用inceptionV2,對圖像,提取一個2048維的特徵向量.因爲咱們須要將輸入圖像分爲5個類別,所以須要添加網絡層,網絡層的輸入爲2048維的向量,輸出爲5維的特徵向量.
具體爲將特徵向量輸入一個全鏈接層,獲得5維的特徵向量,以後加一個softmax激活函數,獲得輸出機率:
# Add the new layer that we'll be training.
(train_step, cross_entropy, bottleneck_input, ground_truth_input,
final_tensor) = add_final_training_ops(len(image_lists.keys()),
FLAGS.final_tensor_name,
bottleneck_tensor)
1
2
3
4
5
def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor):
with tf.name_scope('input'):
bottleneck_input = tf.placeholder_with_default(
bottleneck_tensor, shape=[None, BOTTLENECK_TENSOR_SIZE],
name='BottleneckInputPlaceholder')#[batch_size,2048]
ground_truth_input = tf.placeholder(tf.float32,
[None, class_count],
name='GroundTruthInput')
# Organizing the following ops as `final_training_ops` so they're easier
# to see in TensorBoard
layer_name = 'final_training_ops'
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
layer_weights = tf.Variable(tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights')
variable_summaries(layer_weights)
with tf.name_scope('biases'):
layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
variable_summaries(layer_biases)
with tf.name_scope('Wx_plus_b'):
logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases
tf.summary.histogram('pre_activations', logits)
final_tensor = tf.nn.softmax(logits, name=final_tensor_name)
tf.summary.histogram('activations', final_tensor)
with tf.name_scope('cross_entropy'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
labels=ground_truth_input, logits=logits)
with tf.name_scope('total'):
cross_entropy_mean = tf.reduce_mean(cross_entropy)
tf.summary.scalar('cross_entropy', cross_entropy_mean)
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
cross_entropy_mean)
return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input,
final_tensor)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
模型訓練
inception模型訓練函數爲retrain_v2.py,訓練命令爲:
python retrain_v2.py –image_dir /home/qinghua/data/face_classify/celebs3_squared/
訓練輸入數據爲,人臉圖像(長度爲2048的特徵向量),根據inceptionv2網絡計算全部的訓練,驗證,測試數據的特徵向量(bottleneck),並將其保存在bootlneck文件假下,每一個圖像的特徵向量對應一個文本文件,文件名爲filename.txt.
label爲長度爲5的向量,須要訓練的爲添加的全鏈接層的權重矩陣w([2048,5]),b([5,]).
迭代4000次的結果: