TensorFlow Object Detection API 實踐

2022-01-04 慢慢學TensorFlow

前面的文章TensorFlow 上基於 Faster RCNN 的目標檢測已經介紹過目標檢測概念,直觀表述是用算法在圖片中用框框標記各種各樣的目標,讓機器能像人眼一樣迅速發現場景中物體的位置和大小,從而做出進一步判斷(例如自動駕駛汽車要主動避讓障礙物,沿正確路線前行)。

然而構建準確率高的、能定位和識別單張圖片裡多種物體的模型仍然是計算機視覺領域一大挑戰。TF Object Detection API 【1】是一個構建在 TensorFlow 之上的可以簡化構建、訓練、部署目標檢測模型的開源框架。

TF Object Detection API 安裝步驟參考【2】。

獲取 TensorFlow Models 源碼:

git clone https://github.com/tensorflow/models --recursive
cd models/

接下來的步驟,默認都在 models/ 目錄下進行。

安裝最新版 TensorFlow,懶人步驟:

接著安裝必要的 Python 包:

TF Object Detection API 使用 Protobuf 配置模型、訓練參數。為了正確編譯 proto 文件,需要手動編譯安裝 Protobuf 3.3:

wget https://github.com/google/protobuf/archive/v3.3.0.tar.gz
tar zxvf v3.3.0.tar.gz
cd protobuf-3.3.0/./autogen.sh./configure
make -j
make install

編譯 proto :

設置 PYTHONPATH 環境變量

通過這一步,可以看到 TF Object Detection API 使用了 TF-Slim API,我們上篇文章使用 TF-Slim 設計複雜網絡已介紹過如何使用該 API 設計複雜網絡。

測試安裝是否成功:

今天我們利用 TF Object Detection API 從頭訓練一個 Faster RCNN 目標檢測模型,使用 ResNet-152 網絡和 Oxford-IIIT 寵物數據集。

下載訓練數據

將訓練數據轉換為 tfrecord 格式

運行結束後,你會看到 pet_train.record 和 pet_val.record 兩個文件。

準備工作目錄 20170820,將轉換的 tfrecord 文件放入這個目錄。將 object_detection/data/pet_label_map.pbtxt 和  object_detection/samples/configs/faster_rcnn_resnet152_pets.config 兩個文件也拷貝到這個目錄。該目錄內容如下:

運行訓練


其中--train_dir 指向 20170820/,訓練產生的中間結果都存放在該目錄。 ${PATH_TO_YOUR_PIPELINE_CONFIG} 指向配置文件 20170820/faster_rcnn_resnet152_pets.config,該文件內容中幾個 「PATH_TO_BE_CONFIGURED」 都替換為 「20170820」,第 110 和 111 行內容改為:

  #fine_tune_checkpoint: "20170820/model.ckpt"

  from_detection_checkpoint: false

訓練中

查看 TensorBoard

先啟動 tensorboard 服務

這裡 --logdir= 路徑與前面訓練目錄 --train_dir 相同。

通過網頁 localhost:6006 打開後,查看訓練 loss 曲線如下圖所示。

評估訓練好的網絡

這裡 --eval_dir= 路徑與之前 --train_dir 相同。

進階

完成這些任務之後,可以嘗試(1)將訓練數據改為自己的數據集;(2)將模型改為使用其他網絡如 VGG、MobileNets;(3)利用 TF Object Detection API 實現 YOLOv2 。

參考文獻

【1】 https://github.com/tensorflow/models/tree/master/object_detection

【2】https://github.com/tensorflow/models/blob/master/object_detection/g3doc/installation.md

【3】 "Speed/accuracy trade-offs for modern convolutional object detectors." Huang J, Rathod V, Sun C, Zhu M, Korattikara A, Fathi A, Fischer I, Wojna Z, Song Y, Guadarrama S, Murphy K, CVPR 2017

如果你覺得本文對你有幫助,請關注公眾號,將來會有更多更好的文章推送!

相關焦點

  • Windows系統如何安裝Tensorflow Object Detection API
    什麼是Tensorflow Object Detection API基於tensorflow的一套高級訓練、檢測、可配置的API框架,支持多種預訓練模型檢測,自定義對象檢測、遷移學習等訓練。下載tensorflow object detection API通過git命令clone到指定目錄即可,控制臺執行如下:點擊回車開始clone tensorflow models代碼
  • Tensorflow Object Detection API 終於支持tensorflow1.x與tensorflow2.x了
    基於tensorflow框架構建的快速對象檢測模型構建、訓練、部署框架,是針對計算機視覺領域對象檢測任務的深度學習框架。之前tensorflow2.x一直不支持該框架,最近Tensorflow Object Detection API框架最近更新了,同時支持tensorflow1.x與tensorflow2.x。
  • 使用Tensorflow Object Detection API實現對象檢測
    一:預訓練模型介紹Tensorflow Object Detection API自從發布以來,其提供預訓練模型也是不斷更新發布,功能越來越強大,
  • 如何在windows系統上安裝Tensorflow Object Detection API?
    所以先說下自己的版本情況:Tensorflow-gpu-1.10.0 cuda9 cuDNN7Python3.6.8Protobuf3.7.1從GitHub下載官方存儲庫官方github網站連結為:https://github.com/tensorflow/models在本地建立文件夾用於存儲
  • Tensorflow Object Detection API介紹和應用(mac版)——(2.預訓練模型應用)
    沒有及時更新tf object detection更多的應用。今天有時間來補上一課,直接應用已訓練好的模型去識別物體。Tensorflow Object Detection API是谷歌TensorFlow下面的物體檢測項目,可以實現圖像內物體檢測,支持視頻和圖片,可以在圖片上框框出實體,標註出實體具體類別(類別是自己打的標籤,或者項目內提供的預訓練模型和標籤數據)。
  • 業界 | 谷歌開源TensorFlow Object Detection API物體識別系統
    代碼:https://github.com/tensorflow/models/tree/master/object_detectionJupyter notebook:https://github.com/tensorflow/models/blob/master/object_detection/object_detection_tutorial.ipynb
  • 如何用Tensorflow object-detection API訓練模型,找到聖誕老爺爺?
    本文將教會你如何通過Tensorflow object-detection API訓練自己的目標檢測模型(object detector),來找到聖誕老人。本文的代碼可見於github:https://github.com/turnerlabs/character-finder代碼產生的模型可被延伸用於抓取其他的動畫或者真實人物。
  • Object Detection API 現已支持 TensorFlow 2
    分布策略https://tensorflow.google.cn/guide/distributed_training如果您想立即嘗試這些內容,建議您查看我們全新的 Colab 演示(適用於推理和小樣本訓練)。我們在其中加入了一個有趣的示例,是一個演示如何使用基於微調的小樣本訓練(僅使用五個示例圖像!)來訓練橡膠鴨子檢測器的教程。
  • 教程 | 如何使用TensorFlow API構建視頻物體識別系統
    所以,讓我們來看看它能夠做到什麼吧,先看結果:來自 TensorFlow API 的視頻物體檢測你可以在 GitHub 上找到這個小項目的全部代碼:https://github.com/priya-dwivedi/Deep-Learning/blob/master/Object_Detection_Tensorflow_API.ipynb
  • 深度學習入門篇——手把手教你用 TensorFlow 訓練模型
    $ protoc object_detection/protos/*.(我是把protoc加到環境變量中,遇到找不到*.proto文件的報錯,後來把protoc.exe放到models/object_detection目錄下,重新執行才可以)然後將models和slim(tf高級框架)加入python環境變量:PYTHONPATH=$PYTHONPATH:/your/path/to/tensorflow/
  • 如何在Windows系統上使用Object Detection API訓練自己的數據?
    (這是github上生成文件的修改版)"""name: generate_tfrecord.pyUsage:  # From tensorflow/models/  # Create train data:  python generate_tfrecord.py --csv_input=data/train_labels.csv
  • tensorflow使用object detection實現目標檢測超詳細全流程(視頻+圖像集檢測)
    使用tensorflow object detection進行訓練檢測。參考原始代碼:https://github.com/tensorflow/models/tree/master/research本文以mobilenet-ssd-v2為例進行處理,通過換模型即可實現faster RCNN等的訓練檢測。
  • TF Object Detection 終於支持TF2了!
    不過,如果你想使用TF1,那麼你可以查看對應的Model Zoo(https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md),要比TF2支持的模型少一些。
  • 谷歌更新TensorFlow目標檢測API
    谷歌同時撰文介紹了如何使用Cloud TPUs在30分鐘內訓練和部署實時目標檢測模型,訪問www.52cv.net,或者點擊閱讀全文即可查看。[1][1]https://ai.googleblog.com/2017/11/automl-for-large-scale-image.html[2]https://www.nytimes.com/2018/03/15/nyregion/bike-lane-blocked-new-york.html[3]https://www.youtube.com/watch
  • 教程 | 如何使用Docker、TensorFlow目標檢測API和OpenCV實現實時目標檢測和視頻處理
    以下是我安裝 TensorFlow 目標檢測的方法(按照官方安裝指南進行):# Install tensorFlowRUN pip install -U tensorflow# Install tensorflow models object detectionRUN git clone https://github.com/tensorflow/models
  • 聲網:基於 TensorFlow 在實時音視頻中實現圖像識別
    註:物體檢測 API 連結https://github.com/tensorflow/models/tree/master/research/object_detection物體檢測是檢測圖片中所出現的全部物體並且用矩形 (Anchor Box) 進行標註,物體的類別可以包括多種,例如人、車、動物、路標等。
  • tensorflow+目標檢測:龍哥教你學視覺—LabVIEW深度學習教程
    實現觀察模型評估結果圖像7、學會利用labview實現導出tensorflow凍結圖模型文件pb8、學會利用labview實現導出tensorflow凍結圖模型文件pb轉為openvino模型文件IR9、學會利用labview實現tensorflow/openvino 模型pb/IR文件的加載10、學會利用labview實現tensorflow
  • Keras的標準化:TensorFlow 2.0中的高級api指南
    例如,如果在Colab筆記本中運行: pip install tensorflow  import tensorflow as tf  Dense = tf.keras.layers.Denseloss=’sparse_categorical_crossentropy』,                metrics=[『accuracy』])  model.fit(x_train, y_train, epochs=5)  model.evaluate(x_test, y_test)你可以在tensorflow.org
  • 教程 | 如何使用TensorFlow中的高級API:Estimator、Experiment和Dataset
    /versions/r1.3/programmers_guide/datasetstf.estimator.Estimator:https://www.tensorflow.org/api_docs/python/tf/estimator/Estimatortf.contrib.learn.RunConfig:https://www.tensorflow.org/api_docs
  • TensorFlow Hub, 給您帶來全新的 Web 體驗
    通過 Google Brain 團隊開發的新模塊,我們正在不斷擴展 TensorFlow Hub 的庫存。 最近新增一個在 Open Images v4 上訓練的 FasterRCNN 模塊。以下示例來自 unsplash.com 的圖像以及檢測到的對象。圖3:用於應用 TF 模塊的圖像 Colab notebook 將引導您完成下載模塊並應用,所有這些都能在短短幾分鐘內全部完成。