給你的應用添加圖像識別--TensorFlow
TensorFlow介紹
TensorFlow™ 是一個開放源代碼軟件庫,是谷歌大腦的第二代機器學習系統,用於進行高性能數值計算。於2015年11月9日在Apache 2.0開源許可證下發布。目前最新的版本是1.12。總的來說,TensorFlow計算框架可以很好地支持深度學習的各種算法,很好地兼容了學術研究和工業生產的不同需求,是一個比較厲害的一個機器學習框架。
實際運用
1.配置環境
- MacBook Pro macOS Mojava 版本 10.14(本機環境)
方式一
由於Mac系統自帶Python環境,所以,我們在安裝Python的時候需要注意一下,TensorFlow的官方文檔上介紹的說支持Python2.X,但是我在Python2.7環境的安裝下出現了很多意外的問題,比如編碼的問題和其它依賴庫不匹配問題,所以後來不得不重新安裝其他的版本。Python官網最新的Python版本是Python3.7.1,但是當前的TensorFlow不能很好的兼容,所以建議安裝Python3.5左右的版本。
這裏給出部分相關安裝命令:
//查詢系統默認的Python版本
ls /System/Library/Frameworks/Python.framework/Versions
//自己安裝的Python版本檢測
ls /Library/Frameworks/Python.framework/Versions
//安裝pip(Python3.0以下並沒有pip管理器,需要手動安裝)
sudo easy_install pip
Python各個版本下載 (注意:勾選“Add Python to PATH”)
安裝完之後,打開終端輸入python 就可以獲取到你當前安裝的Python版本號,說明安裝成功。
//安裝TensorFlow(CPU)
pip install tensorflow
安裝完成後,打開終端輸入python,接着輸入下列代碼:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
打印b’Hello, TensorFlow!’,則安裝成功!
接着,我們下載並安裝相關API運行支持組件:
//圖像處理標準庫
pip install pillow
//解析庫
pip install lxml
//開源的Web應用程序(可視化)
pip install jupyter
//Python的繪圖庫
pip install matplotlib
基本的環境配置基本結束,我們需要驗證一下安裝的相關組件是否正確,我們在PC上運行一下試試!
下載源碼並進行編譯:
//下載源碼
git clone https://github.com/tensorflow/models.git
//安裝編譯器
brew install protobuf
注:如沒brew,安裝命令如下:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
protobuf安裝完成後,輸入protoc --version,得到對應安裝的版本,則安裝成功。
models的源碼下載完成後,進入models目錄下進行編譯:
protoc object_detection/protos/*.proto --python_out=.
這個命令在Mac上編譯時沒有有問題,但是在windows7上的話,可能會提示找不到路徑的問題,那麼需要換成另一個命令:
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.
到此基本環境已經配置好了,我們再次打開終端,cd 到models目錄下,輸入:
jupyter-notebook
則會在瀏覽器中打開,我們找到research/object_detection/object_detection_tutorial.ipynb文件,打開,點擊Cell按鈕,選中 Run all,稍等一會(第一次比較慢,得下載運行的模型),就完成了圖像的識別。結果圖如下:
在research/object_detection/test_images文件夾替換我們自己的圖片,再試試看!
識別視頻的代碼如下:
//忙,下次補上
好了,到此我們的環境基本配置完整了,安裝的組件稍多,也比較亂,這裏我建議使用一個統一的軟件進行管理。
方式二
anaconda各個版本下載 ,這種方式比較直觀簡易,通過Anaconda來安裝和管理相關Python的組件,可謂是一鍵操作!具體的安裝細節,大家可自行查詢,不再贅述!
2.模型訓練
看到了識別的效果,那我們怎麼識別我們自己的圖片呢?我們可以進行遷移訓練,即直接使用已經寫好的Python。(也可用其他方式訓練)
下載hub項目:
git clone https://github.com/tensorflow/hub.git
找到:
hub/examples/image_retraining/retrain.py
使用retrain.py文件,我們進行重新訓練,獲得我們需要模型。
接下來準備要訓練的圖片:
這裏我從網上抓取了100張蘋果和100張手錶的圖片進行訓練:
命令如下:
python3 /Users/tianchuangxin1/hub/examples/image_retraining/retrain.py \
--image_dir /Users/tianchuangxin1/tensorflow_image \
--how_many_training_steps=2000 \
----model_dir=/Users/tianchuangxin1/tensorflow_image \
--output_graph /Users/tianchuangxin1/tensorflow_image/output_graph.pb \
--output_labels /Users/tianchuangxin1/tensorflow_image/output_labels.txt \
--architecture=mobilenet_1.0_224_quant \
--summaries_dir /Users/tianchuangxin1/tensorflow_image \
--intermediate_output_graphs_dir /Users/tianchuangxin1/tensorflow_image \
--saved_model_dir /Users/tianchuangxin1/tensorflow_image
相關參數說明在retrain.py代碼中。
訓練過程中我們可以使用TensorBoard進行直觀的觀察。
//tensorboard的打開命令
tensorboard --logdir /tmp/retrain_logs
效果展示:
訓練完成後tensorflow_image文件夾下生成output_labels.txt和output_graph.pb兩個文件,爲了確保生成模型正確,我們驗證一下:
命令如下:
python3 /Users/tianchuangxin1/tensorflow/tensorflow/examples/label_image/label_image.py \
--graph=/Users/tianchuangxin1/tensorflow_image/output_graph.pb --labels=/Users/tianchuangxin1/tensorflow_image/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--image=/Users/tianchuangxin1/tensorflow_image/apple/apple5.jpg
得到如下結果:
apple 0.99963665
watch 0.00036338356
可以看到正確率很高,訓練的十分完美!接下來,我們將pb文件轉化爲tflite類型供Android端使用。
3.模型轉化
安裝tflite:
pip install tensorflow-hub
命令如下:
tflite_convert \
--graph_def_file=/Users/tianchuangxin1/tensorflow_image/output_graph.pb \
--output_file=/Users/tianchuangxin1/tensorflow_image/output_graph.tflite \
--output_format=TFLITE \
--input_shape=1,299,299,3 \
--input_array=Placeholder \
--output_array=final_result \
--inference_type=FLOAT \
--input_data_type=FLOAT
注意的是input_array和output_array這兩個參數值得設置。獲取這兩個參數值得方法如下:
import tensorflow as tf
model_file = "/Users/tianchuangxin1/tensorflow_image/output_graph.pb"
def load_graph(pbmodelFile):
with tf.gfile.GFile(pbmodelFile, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)
input_name = graph.get_operations()[0].name+':0'
output_name = graph.get_operations()[-1].name+':0'
return graph, input_name, output_name
graph, inputName, outputName = load_graph(model_file)
input_x = graph.get_tensor_by_name(inputName)
output_y = graph.get_tensor_by_name(outputName)
print(input_x)
print(output_y)
轉化完成之後,會在你的文件夾下生成一個.tflite的文件。
4.Android端移植
下載TensorFlow的源碼後
tensorflow/tensorflow/examples/android
導入Android studio,等待編譯結束,運行,會在手機上安裝了三個APP,TFL classify,TFL Detect,TFL Speech,我們是圖像識別,所以找到對應的Classify相關即可。output_labels.txt和output_graph.tflite移入assert,轉化的時候用的FLOAT類型,input_shape是299,相關參數更改後,再運行。
得到如下效果: