Tensorflow object detection - step by step
  • 5,117 views,
  • 2018-10-18,
  • 上傳者: Kuann Hung,
  •  0
69461943139994c294972c2ee747bd58.jpeg
Environment
Ubuntu 16.04 
步驟
1.
Tensorflow
# For CPU
pip install tensorflow
# For GPU
pip install tensorflow-gpu
2.
Libraries
sudo apt-get update
sudo apt-get -y install protobuf-compiler python-pil python-lxml python-tk
pip install --user Cython contextlib2 pillow lxml jupyter matplotlib pandas opencv-python
3.
下載 tensorflow models
mkdir ~/Desktop/tensorflow
cd ~/Desktop/tensorflow
git clone https://github.com/tensorflow/models.git
4.
設定 PYTHONPATH
vi ~/.bashrc
# 最後加上這行
export PYTHONPATH=$PYTHONPATH:~/Desktop/tensorflow/models/research:~/Desktop/tensorflow/models/research/slim
5.
COCO API installation
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools ~/Desktop/tensorflow/models/research
 
Windows 版的安裝方式如下
  1. 先下載並安裝 Visual Studio 2017
    https://visualstudio.microsoft.com/zh-hant/downloads/?rr=https%3A%2F%2Fwww.google.com.tw%2F
  2. 因為 Visual Studio 不支援一些參數,所以要修改一下 setup.sh
    # 原
    extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'],
    
    # 改為
    extra_compile_args=['-std=c99'],
  3. 到 cocoapi\PythoinAPI 下面執行
    python setup.py install
6.
Protobuf Compilation
# From tensorflow/models/research/
wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip
unzip protobuf.zip

./bin/protoc object_detection/protos/*.proto --python_out=.
 
Window 版
直接到 https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1 下載即可
然後在 models\research 下執行
bin\protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto
完成後記得 build & install
python setup.py build
python setup.py install
7.
Testing the Installation
# From tensorflow/models/research/
python object_detection/builders/model_builder_test.py
Quick Start: Distributed Training on the Oxford-IIIT Pets Dataset 
8.
Getting the Oxford-IIIT Pets Dataset 
# From tensorflow/models/research/
wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz
tar -xvf images.tar.gz
tar -xvf annotations.tar.gz
9.
Create Tensorflow Record
產生 pet_label_map.pbtxt
# From tensorflow/models/research/
python object_detection/dataset_tools/create_pet_tf_record.py \
    --label_map_path=object_detection/data/pet_label_map.pbtxt \
    --data_dir=`pwd` \
    --output_dir=`pwd`
 
10.
將 TFRecord 複製到 data 目錄下
# From tensorflow/models/research/
mkdir data
cp pet_faces_train.record-* data/
cp pet_faces_val.record-* data/
cp object_detection/data/pet_label_map.pbtxt data/pet_label_map.pbtxt
11.
Downloading a COCO-pretrained Model
# From tensorflow/models/research/
wget http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz
tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz
cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* data/
12.
Configuring the Object Detection Pipeline
# From tensorflow/models/research/

# 編輯以下檔案,並將 PATH_TO_BE_CONFIGURED 修改為所在的目錄
# object_detection/samples/configs/faster_rcnn_resnet101_pets.config

# Copy edited template to cloud.
cp object_detection/samples/configs/faster_rcnn_resnet101_pets.config \
    data/faster_rcnn_resnet101_pets.config
13.
Starting Training and Evaluation Jobs
# From tensorflow/models/research/
bash object_detection/dataset_tools/create_pycocotools_package.sh /tmp/pycocotools
python setup.py sdist
(cd slim && python setup.py sdist)
14.
Running the Training Job
# From the tensorflow/models/research/ directory
PIPELINE_CONFIG_PATH={path to pipeline config file}
MODEL_DIR={path to model directory}
NUM_TRAIN_STEPS=50000
SAMPLE_1_OF_N_EVAL_EXAMPLES=1

python object_detection/model_main.py \
    --pipeline_config_path=${PIPELINE_CONFIG_PATH} \
    --model_dir=${MODEL_DIR} \
    --num_train_steps=${NUM_TRAIN_STEPS} \
    --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \
    --alsologtostderr
15.
Running Tensorboard
tensorboard --logdir=${MODEL_DIR}
Sample files
16.
annotation
<annotation>
    <folder>OXIIIT</folder>
    <filename>Abyssinian_100.jpg</filename>
    <source>
        <database>OXFORD-IIIT Pet Dataset</database>
        <annotation>OXIIIT</annotation>
        <image>flickr</image>
    </source>
    <size>
        <width>394</width>
        <height>500</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>cat</name>
        <pose>Frontal</pose>
        <truncated>0</truncated>
        <occluded>0</occluded>
        <bndbox>
            <xmin>151</xmin>
            <ymin>71</ymin>
            <xmax>335</xmax>
            <ymax>267</ymax>
        </bndbox>
        <difficult>0</difficult>
    </object>
</annotation>
訪客如要回應,請先 登入
    資料夾 :
    發表時間 :
    2018-10-18 22:17:47
    觀看數 :
    5,117
    發表人 :
    Kuann Hung
    部門 :
    老洪的 IT 學習系統
    QR Code :