????????這一節(jié)主要講怎樣判斷意圖,我們的思路是這樣的:首先以意圖命名文件夾(這里我們還是以最初定下的attack、combat和march),而后在各文件夾下對(duì)應(yīng)放置各簡(jiǎn)圖,然后用分類器分類并計(jì)算這些樣本與測(cè)試圖片間的相似度。
????????剛開始我們想著直接各圖之間作比較,在網(wǎng)上查了一下,有灰度的、cos的、均值hash和感知hash算法的(可以忽視圖像的旋轉(zhuǎn)角度)比較,按比較的方向不同,計(jì)算出來的相似度也不同,但數(shù)值不具有普遍性,同一張圖片多次比較得到的結(jié)果不同。下面我選用了均值hash和感知hash的平均和來作為相似度,得到的結(jié)果分別是91.40625%和87.5%:


????????為了得到更局普遍意義上的結(jié)果,這里還是選用深度學(xué)習(xí)框架來對(duì)大量訓(xùn)練數(shù)據(jù)進(jìn)行分析,用整個(gè)訓(xùn)練集來與實(shí)驗(yàn)圖片比較相似度,得到的結(jié)果才能讓同學(xué)老師滿意。下面是使用inception v3模型,將原先的softmax輸出拋棄,自定義分類的種類,訓(xùn)練參數(shù),得到自己的分類模型,順便打印出相似度。
????????按照這個(gè)思路,首先還是制作特定的文件夾存放數(shù)據(jù):
bottleneck(空文件夾)
data(存放要訓(xùn)練的圖片數(shù)據(jù))
images(用來測(cè)試的圖片數(shù)據(jù))
????????將我們之前準(zhǔn)備好的attack、combat和march文件夾放在data下面:

????????image文件夾下放一張準(zhǔn)備測(cè)試的圖片:

????????而后下載retrain.py 程序,它是用于下載inception v3模型以及訓(xùn)練后面的分類器,我們會(huì)在inception模型的基礎(chǔ)上進(jìn)行自己圖片分類的代碼。附上retrain.py的鏈接:https://github.com/tensorflow/hub/blob/master/examples/image_retraining/retrain.py
????????下載完成后,運(yùn)行retrain.py,下載inception v3模型并訓(xùn)練分類器,它的命令格式具體如下:
# 1.模型和樣本路徑的設(shè)置
bottleneck_dir?????????????????? ???#訓(xùn)練數(shù)據(jù)存放
how_many_training_steps????????? ??#訓(xùn)練次數(shù)
MODEL_DIR = 'inception_model'???? ?# inception模型位置
output_graph????????????????????? #輸出標(biāo)簽的pb文件
output_labels???????????????????? ?#輸出檢測(cè)標(biāo)簽,這里為attackcombatmarch
image_dir???????????????????????? #測(cè)試用圖片位置
# 2. 神經(jīng)網(wǎng)絡(luò)參數(shù)的設(shè)置(默認(rèn))
LEARNING_RATE = 0.01
STEPS = 4000
BATCH = 100
????????這里我的運(yùn)行命令如下,運(yùn)行后會(huì)生成output_graph.pb和output_labels.txt兩個(gè)文件,我們的分類效果就是靠這兩個(gè)文件內(nèi)的訓(xùn)練數(shù)據(jù),bottleneck_dir文件夾中會(huì)對(duì)應(yīng)每張圖片生成一個(gè)txt文檔,計(jì)算的是訓(xùn)練參數(shù):


????????好的,我們的訓(xùn)練很快就完成了,下面就可以進(jìn)行坦克軍團(tuán)的意圖識(shí)別了,首先寫一個(gè)數(shù)據(jù)接收并調(diào)用模型的腳本:
import tensorflow as tf
import os?
import numpy as np?
import re?
from PIL import Image?
import matplotlib.pyplot as plt?
lines = tf.gfile.GFile('retrained_labels.txt').readlines()?
uid_to_human ={}?
#讀取參數(shù)中的數(shù)據(jù)?
for uid,line in enumerate(lines):?
? ? line=line.strip('\n')?
? ? uid_to_human[uid]=line?
def id_to_string(node_id):?
? ? if node_id not in uid_to_human:?
? ? ? ? return ''?
? ? return uid_to_human[node_id]?
#創(chuàng)建圖來存放訓(xùn)練好的模型參數(shù)?
with tf.gfile.FastGFile('retrained_graph.pb','rb') as f:?
? ? graph_def = tf.GraphDef()?
? ? graph_def.ParseFromString(f.read())?
? ? tf.import_graph_def(graph_def,name='')?
#測(cè)試圖片分類?
with tf.Session() as sess:?
? ? softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')?
? ? #遍歷目錄?
? ? for root,dirs,files in os.walk('images/'):?
? ? ? ? for file in files:?
? ? ? ? ? ? #載入圖片?
? ? ? ? ? ? image_data = tf.gfile.FastGFile(os.path.join(root,file),'rb').read()?
? ? ? ? ? ? #jpeg格式的圖片?
? ? ? ? ? ? predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})?
? ? ? ? ? ? #結(jié)果轉(zhuǎn)為1維度?
? ? ? ? ? ? predictions = np.squeeze(predictions)?
? ? ? ? ? ? #打印圖片信息?
? ? ? ? ? ? image_path = os.path.join(root,file)?
? ? ? ? ? ? print (image_path)?
? ? ? ? ? ? #顯示圖片?
? ? ? ? ? ? img=Image.open(image_path)?
? ? ? ? ? ? plt.imshow(img)?
? ? ? ? ? ? plt.axis("off")?
? ? ? ? ? ? plt.show()?
? ? ? ? ? ? #排序?
? ? ? ? ? ? top_k = predictions.argsort()[::-1]?
? ? ? ? ? ? print(top_k)?
? ? ? ? ? ? for node_id in top_k:?
? ? ? ? ? ? ? ? human_string =id_to_string(node_id)?
? ? ? ? ? ? ? ? #置信度?
? ? ? ? ? ? ? ? score = predictions[node_id]?
? ? ? ? ? ? ? ? print ('%s (score = %.5f)' % (human_string, score))?
? ? ? ? ? ? print()?
????????我們傳入一張?zhí)箍塑妶F(tuán)圖片(attack意圖),然后將它轉(zhuǎn)成連線簡(jiǎn)圖放在images文件夾中,然后運(yùn)行命令:

分類器輸出結(jié)果如下:


????????正如輸出打印所示,分類器將三種意圖的可能性打印了出來,其中attack占0.81665,combat占0.12556,march占0.05779,效果極其明顯。
????????以上便是對(duì)圖片內(nèi)目標(biāo)進(jìn)行識(shí)別和意圖分析的主要過程,同學(xué)的匯報(bào)也受到了老師的表?yè)P(yáng),看來是時(shí)候讓他準(zhǔn)備請(qǐng)客啦。最后一節(jié),將會(huì)繼續(xù)改良腳本,將整個(gè)項(xiàng)目的過程關(guān)聯(lián)在一起。
代碼已上傳至GitHub及Gitee,歡迎star,歡迎討論:
GitHub:https://github.com/wangwei39120157028/Machine_Learning_research_on_simple_target_recognition_and_intention_analysis
Gitee:https://gitee.com/wwy2018/Machine_Learning_research_on_simple_target_recognition_and_intention_analysis/settings