1. 程式人生 > >Tensorflow object detection API 搭建屬於自己的物體識別模型——環境搭建與測試

Tensorflow object detection API 搭建屬於自己的物體識別模型——環境搭建與測試

1、開發環境搭建

①、安裝Anaconda

  建議選擇 Anaconda3-5.0.1 版本,已經整合大多數庫,並將其作為預設python版本(3.6.3),配置好環境變數(Anaconda安裝則已經配好)。也可以直接安裝python,安裝各種包比較麻煩,因此直接裝了Anaconda整合環境。

  安裝完Anaconda後,開啟Anaconda Prompt,逐個輸入conda --version和python --version,出現下圖所示內容則安裝成功。

 

②、安裝TensorFlow

  如果是初學者,我們安裝cpu版本的tensorflow足夠使用。安裝TensorFlow-cpu很簡單,開啟Anaconda Prompt,輸入pip install tensorflow。稍等一會就安裝成功。通過輸入以下程式碼,檢測是否安裝成功。

python

import tensorflow as tf

 hello = tf.constant('Hello, TensorFlow!')

 sess = tf.Session()

 print(sess.run(hello))

  如果輸出如下圖所示,則安裝成功。

 

如果輸入import tensorflow as tf出現如下警告:

  FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_converters。則需要開啟Anaconda Prompt,輸入pip install h5py==2.8.0rc1解決。

如果輸入 sess = tf.Session()出現Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2,則表明TensorFlow版本太低了,需要開啟Anaconda Prompt,輸入pip install --upgrade tensorflow解決。

③、下載Tensorflow object detection API模型,從https://github.com/tensorflow/models下載。解壓檔案到磁碟指定目錄,例如C:\Users\CFF\Desktop,重新命名為models(此包內包括各種內容,我們所用到的object_detection資料夾在C:\Users\CFF\Desktop\models\research資料夾下)

④、Protobuf 編譯。從https://github.com/google/protobuf/releases下載windows版的工具,如protoc-3.6.1-win32.zip,解壓到C:\Users\CFF\Desktop,生成:bin, include兩個資料夾。開啟Anaconda Prompt,cd C:\Users\CFF\Desktop\models\research ,輸入C:\Users\CFF\Desktop\bin\protoc ,編譯結果如下說明可以開始編譯。

 

Protobuf 編譯,用protoc可執行檔案編譯目錄object_detection/protos下的proto檔案,生成Python檔案。如:C:\Users\CFF\Desktop\bin\protoc object_detection/protos/*.proto --python_out=.。如何出現object_detection/protos/*.proto目錄沒有發現,可以將*號改成對應的檔案逐個編譯,生成對應的Python檔案。 

⑤、測試。開啟Anaconda Prompt,cd C:\Users\CFF\Desktop\models\research目錄,輸入jupyter notebook,跳轉到網頁介面。

在cell中選擇runAll,正常的話稍等一會兒就會有結果:

到此,已經成功完成了環境搭建與測試。

⑥、用來測試自己的圖片,改變PATH_TO_TEST_IMAGES_DIR 的路徑,我的如下所示。測試圖片命名為image1.jpg   image2.jpg... 相應的數量for i in range(1, 3)也要根據自己的圖片數量進行改變。

執行後,輸出結果如下: