1. 程式人生 > >基於win10,GPU的Tensorflow Object Detection API部署及USB攝像頭目標檢測

基於win10,GPU的Tensorflow Object Detection API部署及USB攝像頭目標檢測

1.TensorFlow安裝

安裝教程在CSDN上有很多文章,但最好依據官方教程,因為TensorFlow不斷更新,需要的CUDA、cudnn等版本也在變化。官方地址在GITHUB裡TensorFlow專案下可以找到安裝指南,https://www.tensorflow.org/install/。這裡我選擇的是Anaconda安裝,方便統一管理。
考慮到翻牆的原因,打不開官方網站的看這裡:

(1)Requirements to run TensorFlow with GPU support

  • If you are installing TensorFlow with GPU support using one of the mechanisms described in this guide, then the following NVIDIA software must be installed on your system:

  • CUDA® Toolkit 8.0. For details, see NVIDIA’s documentation Ensure that you append the relevant Cuda pathnames to the %PATH% environment variable as described in the NVIDIA documentation.
    The NVIDIA drivers associated with CUDA Toolkit 8.0.

  • cuDNN v6.1. For details, see NVIDIA’s documentation. Note that cuDNN is typically installed in a different location from the other CUDA DLLs. Ensure that you add the directory where you installed the cuDNN DLL to your %PATH% environment variable.
    GPU card with CUDA Compute Capability 3.0 or higher. See

    NVIDIA documentation for a list of supported GPU cards.

  • If you have a different version of one of the preceding packages, please change to the specified versions. In particular, the cuDNN version must match exactly: TensorFlow will not load if it cannot find cuDNN64_6.dll. To use a different version of cuDNN, you must build from source.

(2)Requirements to run TensorFlow with GPU support

  • The Anaconda installation is community supported, not officially supported.
    Take the following steps to install TensorFlow in an Anaconda environment:
    Follow the instructions on the Anaconda download site to download and install Anaconda.
    Create a conda environment named tensorflow by invoking the following command:
C:> conda create -n tensorflow python=3.5 
  • Activate the conda environment by issuing the following command:
C:> activate tensorflow
 (tensorflow)C:>  # Your prompt should change 
  • Issue the appropriate command to install TensorFlow inside your conda environment. To install the CPU-only version of TensorFlow, enter the following command:
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow
  • To install the GPU version of TensorFlow, enter the following command (on a single line):
(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu

(3)安裝後日常使用時開啟shell視窗,我用的conEmu,啟用TensorFlow環境再執行。

activate tensorflow

2.下載 TensorFlow models

3.配置依賴庫

  • 利用Anaconda安裝protobuf:
conda install protobuf
  • 編譯Protobuf庫,在object_detection同級目錄開啟終端執行:
cd E:\TensorFlow\GitHub\models\research
protoc object_detection\protos\*.proto --python_out=.
  • 在research目錄下執行:
python setup.py install
  • 進入slim目錄執行:
python setup.py install
  • 新增環境變數:

這裡寫圖片描述

  • 返回research目錄測試環境是否準備完畢:
python object_detection/builders/model_builder_test.py

若出現缺乏相關庫的提示,根據提示利用conda install安裝,若仍然失敗,重複(3)(4)

4.程式配置和執行

# What model to download.  
MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'  
#MODEL_NAME = 'faster_rcnn_resnet101_coco_11_06_2017'
#MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'
MODEL_FILE = MODEL_NAME + '.tar.gz'  
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'  
python --version

我的是3.5.4,所以我選擇:opencv_python-3.3.1-cp35-cp35m-win_amd64.whl
安裝:

pip install opencv_python-3.3.0-cp35-cp35m-win_amd64.whl
  • 編寫視訊採集及目標檢測程式碼,執行
python webcamdetect.py

這裡寫圖片描述



參考文獻: