天天看點

Tensorflow Object Detection API使用一、安裝 Tensorflow 及 Object Detection API二、使用 Oxford-IIIT Pet 資料集進行測試三、使用模型進行檢測

一、安裝 Tensorflow 及 Object Detection API

1.安裝Tensorflow

TensorFlow中文社群文檔:Tensorflow安裝。

2.安裝Object Detection API

參考Github上,TensorFlow

models/research/object_detection

裡的安裝教程。

添加庫的路徑到PYTHONPATH,使用編輯器如gedit打開

~/.bashrc

檔案,将以下指令添加到一個新行。其中

pwd

代表

models/research

的路徑,可去掉`符号,然後在終端

bash

一下。

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
           

注:pip install 如果是用sudo,則安裝系統預設python下的包,如果TensorFlow是安裝在其他python下(如miniconda),則不加sudo。

二、使用 Oxford-IIIT Pet 資料集進行測試

1.下載下傳解壓資料集,并将其轉換為TFRecords

參考Github上,TensorFlow

models/research/object_detection

裡的說明文檔。

Pet資料集裡有部分圖檔損毀,應該删除掉,否則會在運作eval.py時報以下錯誤:

Corrupt JPEG data: 240 extraneous bytes before marker 0xd9

。參考文檔可知,

egyptian_mau_14, 139, 145, 156, 167, 177, 186, 191; abyssinian_5, 34; chihuahua_121; beagle_116

為損壞的檔案,應删去對應的

.jpg; .xmls;

以及

list.txt、test.txt、trainval.txt

裡對應的行。

2.下載下傳用于遷移學習的預訓練模型

在Tensorflow Detection Model Zoo裡下載下傳 COCO-pretrained SSD with MobileNet model,存放在

models/research

檔案夾中,并解壓。

3.配置對象檢測管道

參考文檔

在 Tensorflow object detection API 裡,配置檔案定義了模型參數、訓練參數等參數,檔案為

object_detection/protos/pipeline.proto

。在此實驗中,采用一些預定義模闆,選擇在

object_detection/samples/configs

檔案夾中的模闆,此處采用

ssd_mobilenet_v1_pets.config

作為而配置管道的起點。如果不采用預定義模闆配置管道,參考Configuring the Object Detection Training Pipeline。

ssd_mobilenet_v1_pets.config

檔案中,修改

fine_tune_checkpoint

input_path

label_map_path

路徑。

fine_tune_checkpoint

應該提供一個已存在的checkpoint路 ,在下載下傳的預訓練模型的檔案中包含有

model.ckpt.data-00000-of-00001, model.ckpt.index, model.ckpt.meta

,比如

fine_tune_checkpoint

的路徑可設定為

/home/cyj/Project/models/research/ssd_mobilenet_v1_coco_2017_11_17/model.ckpt

4.運作

運作訓練程式

# From the tensorflow/models/research/ directory
python object_detection/train.py \
    --logtostderr \
    --pipeline_config_path=${PATH_TO_YOUR_PIPELINE_CONFIG} \
    --train_dir=${PATH_TO_TRAIN_DIR} \
           

${PATH_TO_YOUR_PIPELINE_CONFIG}

指向管道訓練檔案,如

/home/cyj/Project/models/research/object_detection/samples/configs/ssd_mobilenet_v1_pets.config

${PATH_TO_TRAIN_DIR}

定義訓練事件寫入路徑,先建立相關

train

檔案夾,如

/home/cyj/Project/models/research/train

output_inference_graph.pb

為導出的訓練的模型。

Tensorflow Object Detection API使用一、安裝 Tensorflow 及 Object Detection API二、使用 Oxford-IIIT Pet 資料集進行測試三、使用模型進行檢測

TensorFlow 模型儲存參考文檔。

運作測試程式

# From the tensorflow/models/research/ directory
python object_detection/eval.py \
    --logtostderr \
    --pipeline_config_path=${PATH_TO_YOUR_PIPELINE_CONFIG} \
    --checkpoint_dir=${PATH_TO_TRAIN_DIR} \
    --eval_dir=${PATH_TO_EVAL_DIR}
           

${PATH_TO_EVAL_DIR}

定義測試事件寫入路徑,先建立相關

eval

檔案夾,如

/home/cyj/Project/models/research/eval

運作Tensorboard

模型訓練和測試的過程可通過Tensorboard進行檢視,使用以下指令運作Tensorboard:

其中

${PATH_TO_MODEL_DIRECTORY}

指向包含

train

eval

的目錄。例如

~$ tensorboard --logdir=/home/cyj/Project/models/research

Tensorflow Object Detection API使用一、安裝 Tensorflow 及 Object Detection API二、使用 Oxford-IIIT Pet 資料集進行測試三、使用模型進行檢測

輸出訓練的模型

模型訓練後,将其導出為TensorFlow Graph樣式。一個checkpoint由以下三個部分組成:

model.ckpt-${CHECKPOINT_NUMBER}.data--of-,
model.ckpt-${CHECKPOINT_NUMBER}.index
model.ckpt-${CHECKPOINT_NUMBER}.meta
           

當要把已有的checkpoint轉化為graph,執行以下代碼即可得到名為

output_inference_graph.pb

的graph。

# From tensorflow/models/research/
python object_detection/export_inference_graph.py \
    --input_type image_tensor \
    --pipeline_config_path ${PIPELINE_CONFIG_PATH} \
    --trained_checkpoint_prefix ${TRAIN_PATH} \
    --output_directory output_inference_graph.pb
           

三、使用模型進行檢測

參考代碼 Github: Video_Object_Detection, 此實驗中可通過模型,進行照片和視訊中的目标檢測。

繼續閱讀