1. 程式人生 > >Tensorflow object detection API(1)---環境搭建與測試

Tensorflow object detection API(1)---環境搭建與測試

 

 

參考:

https://blog.csdn.net/dy_guox/article/details/79081499

https://blog.csdn.net/u010103202/article/details/79899293

https://blog.csdn.net/u010103202/article/details/79899293

一、開發環境搭建

作業系統:Windows7 64位

TensorFlow:1.8

python:3.5

 

1、下載TensorFlow object detection API

https://github.com/tensorflow/models

我們需要的目標檢測在model資料夾下的research資料夾裡

也可以直接訪問:

https://github.com/tensorflow/models/tree/master/research/object_detection

下載程式碼庫

在models程式碼庫的介面中,有下載整個程式碼庫的按鈕,如下圖紅色箭頭標註處所示:

 

 下載完後解壓到桌面目標檢測資料夾(需要自己建立)

 2.下載Protobuf

目的是將proto檔案轉成py檔案

Protobuf(Google Protocol Buffers)是google開發的的一套用於資料儲存,網路通訊時用於協議編解碼的工具庫。它和XML和Json資料差不多,把資料已某種形式儲存起來。Protobuf相對與XML和Json的不同之處,它是一種二進位制的資料格式,具有更高的傳輸,打包和解包效率。
下載Protobuf網址: https://github.com/google/protobuf/releases
下載Protobuf網址頁面如下圖所示,下圖中紅色箭頭標註處是Protobuf在作業系統Windows中可以直接執行的protoc程式,下載該壓縮檔案 protoc-3.6.1-win32.zip



 將壓縮檔案protoc-3.6.1-win32.zip解壓後的bin資料夾protoc.exe複製到路徑:C:\\Windows

 

將object_detection\protos資料夾下的.proto檔案轉成.py

可以看到有很多proto檔案

 用這段程式碼將object_detection\protos資料夾下的.proto檔案轉成.py,只需將E:\目標檢測\models-master\research\object_detection\protos/改成你的檔案路徑

 1 import os
 2 file_list = os.listdir(r'E:\目標檢測\models-master\research\object_detection\protos/')
 3 proto_list = [file for file in file_list if '.proto' in file]
 4 print('object_detection/proto資料夾中共有%d個proto檔案' %len(proto_list))
 5 for proto in proto_list:
 6     execute_command = 'protoc object_detection/protos/%s --python_out=.' %proto
 7     os.popen(execute_command)
 8 file_list = os.listdir(r'E:\目標檢測\models-master\research\object_detection\protos/')
 9 py_list = [file for file in file_list if '.py' in file]
10 print('通過protoc命令產生的py檔案共有%d個' %(len(py_list) - 1))

結果是:

object_detection/proto資料夾中共有29個proto檔案
通過protoc命令產生的py檔案共有29個

就是修改成功,你可以進入protos資料夾檢視每個proto檔案下都有一個對應的py檔案。

3、安裝model和slim

在research目錄下執行
pythonsetup.py install
在slim目錄下執行
pythonsetup.py build
pythonsetup.py install

在slim安裝時會出現

error:could not create 'build': 當檔案已存在時,無法建立該檔案。

這是因為原來clone的資料夾裡有一個BUILD檔案,將其刪除後執行上述命令安裝slim即可。

4、PYTHONPATH 環境變數設定

在 ‘此電腦’-‘屬性’- ‘高階系統設定’ -‘環境變數’-‘系統變數’ 中新建名為‘PYTHONPATH’的變數,將

models/research/ 及 models/research/slim 兩個資料夾的完整目錄新增,分號隔開。

 

接下來可以測試API,在 models/research/ 資料夾下執行命令列:

 

python object_detection/builders/model_builder_test.p

 

 

二、測試自帶案例

 

“開始-Anaconda3-Anaconda Prompt”調出命令列,改變工作目錄至 models\research\object_detection


然後輸入jupyter notebook,就會呼叫瀏覽器開啟當前資料夾,點開 object_detection_tutorial.ipynb,

 



在新標籤頁中開啟 Object Detection Demo,點選上方的 “Cell”-"Run All",

就可以直接看到結果,最後輸出的是兩張圖片的識別結果,分別是狗,以及沙灘。第一次執行由於需要下載訓練好的模型,耗時較長。第二次之後可以將 .ipynb檔案中 Download Model 即 in[5]部分的程式碼註釋掉,以加快執行速度。



如果在notebook中執行有問題,可以將.ipynb中in[]的程式碼複製到.py中,然後在 開始-Anaconda3-spyder 中執行。

至此Tensorflow object detection API 的環境搭建與測試工作完成。