Tensorflow Object Detection API 安裝


轉載:http://www.codingsoho.com/zh/blog/object-detection-api-installatin/

這兩天剛開始研究OpenCV,剛好碰到同事大牛在搞機器學習的識別的算法庫,今天跟他一起研究了一下Object Detection API的安裝,並基本測試通過!

網上資料很多,但或者版本老,或者環境不一樣,總之整個操作下來坑很多。 接下來我的操作是針對Window平台 (Win7), Python 3 (3.6/3.5)

安裝 Tensorflow Object Detection API

建立工程

Object Detection API 對應的源碼位置為https://github.com/tensorflow/models,可參考安裝文檔 https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

新建Tensorflow文件夾,將https://github.com/tensorflow/models 下載放在Tensorflow目錄下。我習慣git同步,可以用下面命令把代碼git到本地

git clone git@github.com:tensorflow/models.git

安裝Tensorflow及依賴庫

首先安裝Tensorflow, 命令如下

# For CPU
pip install tensorflow
# For GPU
pip install tensorflow-gpu

安裝依賴庫

pip install Cython
pip install contextlib2
pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib

編譯 Protobuf 依賴包

Tensorflow Object Detection API使用Protobufs來配置模型和訓練參數。在使用框架之前,必須編譯 Protobuf 依賴包。在 tensorflow/model目錄運行命令:

cd models/research
protoc object_detection/protos/*.proto --python_out=.

這條手冊上的命令很有可能會讓你碰到兩個問題:

  • protoc 命令無法執行,因為沒安裝
  • object_detection/protos/*.proto: No such file or directory

安裝 protoc

下載地址: http://code.google.com/p/protobuf/downloads/list,估計你也上不去,可以直接從protocbuf的github網站下載https://github.com/protocolbuffers/protobuf/releases

首先下載protobuf-all-3.6.1.tar 和 protoc-3.6.1-win32.zip 兩個包。分別解壓到各自目錄。
接着將protoc-3.6.1-win32.zip中的protoc.exe拷貝到c:\windows\system32中。
這樣protoc命令找不到的問題就能解決了。

object_detection/protos/*.proto: No such file or directory

這個問題是Object Detection Module的一個bug,說明如下

This is a typical bug of the object detection module working in Windows. Here are detailed discussion #1591.
A solution is to compile those .proto files one by one. You can write a batch to run.

所以我就簡單做了個批處理腳本運行

protoc object_detection\protos\anchor_generator.proto --python_out=.
protoc object_detection\protos\argmax_matcher.proto --python_out=.
protoc object_detection\protos\bipartite_matcher.proto --python_out=.
protoc object_detection\protos\box_coder.proto --python_out=.
protoc object_detection\protos\box_predictor.proto --python_out=.
protoc object_detection\protos\eval.proto --python_out=.
protoc object_detection\protos\faster_rcnn.proto --python_out=.
protoc object_detection\protos\faster_rcnn_box_coder.proto --python_out=.
protoc object_detection\protos\graph_rewriter.proto --python_out=.
protoc object_detection\protos\grid_anchor_generator.proto --python_out=.
protoc object_detection\protos\hyperparams.proto --python_out=.
protoc object_detection\protos\image_resizer.proto --python_out=.
protoc object_detection\protos\input_reader.proto --python_out=.
protoc object_detection\protos\keypoint_box_coder.proto --python_out=.
protoc object_detection\protos\losses.proto --python_out=.
protoc object_detection\protos\matcher.proto --python_out=.
protoc object_detection\protos\mean_stddev_box_coder.proto --python_out=.
protoc object_detection\protos\model.proto --python_out=.
protoc object_detection\protos\multiscale_anchor_generator.proto --python_out=.
protoc object_detection\protos\optimizer.proto --python_out=.
protoc object_detection\protos\pipeline.proto --python_out=.
protoc object_detection\protos\post_processing.proto --python_out=.
protoc object_detection\protos\preprocessor.proto --python_out=.
protoc object_detection\protos\region_similarity_calculator.proto --python_out=.
protoc object_detection\protos\square_box_coder.proto --python_out=.
protoc object_detection\protos\ssd.proto --python_out=.
protoc object_detection\protos\ssd_anchor_generator.proto --python_out=.
protoc object_detection\protos\string_int_label_map.proto --python_out=.
protoc object_detection\protos\train.proto --python_out=.

運行完成之后,對應的python文件就生成了。

注意: 運行上面命令時,不要在protos目錄編譯,安裝手冊步驟,在research目錄下,運行protoc object_detection\protos?.proto,否則會有一些文件依賴相關的報錯

在上面的過程中,我們並沒有去編譯protobuf-all-3.6.1.tar這個包。我也剛開始研究這個庫,所以還不確定它跟tensorflow下面的proto內部是否完全一致,但是它不影響接下來的基本測試,不過,我還是在這兒介紹一下它的編譯方法,有兩種(網上學習到的,待驗證)

1.跟前面一樣,用protoc編譯

2.如它tutorial里說的一樣,完全可以拿它編譯好的python文件

For non-C++ users, the simplest way to install the protocol compiler is to download a pre-built binary from our release page:
https://github.com/protocolbuffers/protobuf/releases

直接進行python安裝

python setup.py install

注意: protoc下載的文件里面也有protobuf,這個用不到,不要去編譯它protoc-3.6.1-win32\include\google\protobuf

特別注意的是,關於這一步網上有的人給的方案是讓你直接用編好的protos文件,但這很有可能會出現版本匹配的問題,這都是血的教訓。

為依賴包添加環境變量

models\researchmodels\research\slim添加到環境變量,可以有幾個方法

1.函數添加

import sys
sys.path.append("c:\\")

這需要去改啟動腳本,我這兒只是想完成安裝,所以不是優選項

2.修改環境變量

用戶可以修改系統環境變量PYTHONPATH,因為我是用virtualenv建的工程,還不知道怎么去添加它

3.增加.pth文件,推薦!

在site-packages添加一個路徑文件,如oda.pth,該文件必須以.pth為后綴,在文件里寫上你指定訪問的python庫位置,如下:

E:\Computer\virtualenv\oda\models\research\
E:\Computer\virtualenv\oda\models\research\slim

測試

運行下面命令

python object_detection/builders/model_builder_test.py

如果正常的話你看到的結果應該如下

models\research\object_detection\builders>model_builder_test.py 
......................
----------------------------------------------------------------------
Ran 22 tests in 0.438s

OK

運行 Jupyter notebook demo

啟動Jupyter

cd tensorflow/models/
jupyter notebook

它會啟動瀏覽器窗口,進入object_detection文件夾打開object_detection_tutorial.ipynb ,運行該文件Cell -> Run All

耐心點,幾分鍾之后(i5機器,不帶GPU)你就可以看到結果了。如果是i3機器,那你必須得升級了。

顯示效果如下

接下來,開啟你的Object Detection之旅吧!

參考文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM