Python深度學習筆記(二):使用TensorFlow API進行圖像辨識

前置作業

  1. 閱讀 TensorFlow Object Detection API installation documentation
  2. 下載 git for Windows.
  3. 下載 tensorflow-model repository.

下載 tensorflow-model repository

git clone https://github.com/tensorflow/models.git#下載到指定資料夾(D:\tensorflow)

安裝TensorFlow

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

安裝相關模組

pip install pillow
pip install lxml
pip install jupyter
pip install matplotlib

下載COCO API

git clone https://github.com/cocodataset/cocoapi.git並將檔案移動到 tensorflow/models/research的目錄當中

下載 Google Protobuf

https://github.com/google/protobuf/releases/tag/v3.4.0選擇 “protoc-3.4.0-win32.zip”將Protobuf解壓縮到 C:\Program Files\

將下列路徑加到環境變數當中

系統-> 進階系統設定 -> 環境變數 -> 編輯-> 新增下列路徑

path\Anaconda3path\Anaconda3\Scriptspath\Anaconda3\Library\binpath-to-tensorflow\models\researchpath-to-tensorflow\models\research\slimpath-to-tensorflow\models\research\object_detection

重新開機

重新開機讓環境變數生效

使用CMD

cd tensorflow\models\research“C:\Program Files\protoc-3.4.0-win32\bin\protoc.exe” object_detection/protos/*.proto --python_out=.到 object_detection/protos 檢查是否有 .py 檔案

測試是否成功

Image for post
Image for post
python object_detection/builders/model_builder_test.py
在Anaconda\Lib\site-packages資料夾中,寫一個tensorflow_model.pth。內容為兩條路徑D:\tensor-flow\models\research
D:\tensor-flow\models\research\slim

開啟object_detection_tutorial.ipynb

在D:\tensor-flow\models\research\object_detection目錄底下執行object_detection_tutorial.ipynb

官方範例:

Image for post
Image for post
Image for post
Image for post
海邊風景

使用自己的照片辨識

替換D:\tensor-flow\models\research\object_detection\test_images的照片
必須以image1,image2,image3,.....為檔名
修改object_detection_tutorial.ipynb中的程式Detection的部分 #有多張照片就要修改range範圍
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]
或者也可自訂程式,做更彈性的辨識
Image for post
Image for post
人們
Image for post
Image for post
Image for post
Image for post
石虎被當成貓

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store