LoginSignup
0
1

More than 1 year has passed since last update.

TensorFlow/Object Detection APIをローカルで使ってみた

Last updated at Posted at 2022-08-17

TensorFlow/Object Detection APIのチュートリアルをローカル環境でやってみた。クラウド環境は別の機会に。
※学習開始まで

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md

#1. 環境構築
##1-1. python3.7の仮想環境を作成する

conda create -n py37 python=3.7

作成した環境をアクティベートし、pythonのバージョンを確認する。3.7になっていればOK

conda activate py37
python -V

##1-2. TensorFlowをインストールする

pip install tensorflow==1.15.0rc3

※Object Detection APIはTensorFlow2系に対応していないので1系を導入する。
依存関係のライブラリーをインストール。

pip install --user Cython
pip install --user contextlib2
pip install --user pillow
pip install --user lxml
pip install --user jupyter
pip install --user matplotlib

###1-3. Object Detection APIを使用するための準備
tensorflow/modelsのクローン

# From root directory
mkdir tensorflow
cd tensorflow
git clone git@github.com:tensorflow/models.git

COCO APIのインストール

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools ~/tensorflow/models/research/

※tensorflow/models/research/にpycocotoolsが存在することを確認する。
Protobufのコンパイル
protocがインストールされているかの確認

protoc --version

なければ下記でインストールする

brew install protoc
# From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

PYTHONPATHの追加

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

インストールの確認

# From tensorflow/models/research/
python object_detection/builders/model_builder_test.py
...
[       OK ] ModelBuilderTest.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 17 tests in 0.195s

OK (skipped=1)

となればインストールの完了。

#2. 機械学習を行う(ローカル)
tensorflow/models/researchにpetsディレクトリーを作成する。

# From tensorflow/models/research/
mkdir pets

##2-1. データセットの準備
データセットのダウンロード

# 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

imagesとannotationsのディレクトリーが存在することを確認する。

TFRecordの作成

# 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`

pet_faces_trainとpet_faces_valのファイルが10個ずつ生成されていることを確認する。
生成されたTFRecordをコピー

# From tensorflow/models/research/
cp pet_faces_train.record-* pets
cp pet_faces_val.record-* pets

ラベルマップをコピー

# From tensorflow/models/research/
cp object_detection/data/pet_label_map.pbtxt pets

##2-2. 事前学習モデルを準備
COCOの事前学習モデルをダウンロードし、中身を移動させる

# From tensorflow/models/research/pets
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/frozen_inference_graph.pb ~/tensorflow/models/research/pets
cp faster_rcnn_resnet101_coco_11_06_2017/graph.pbtxt ~/tensorflow/models/research/pets
cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* ~/tensorflow/models/research/pets

##2-3. Configファイルの準備
Configファイルの修正

# From tensorflow/models/research
cp object_detection/samples/configs/faster_rcnn_resnet101_pets.config pets
sed -i "" "s|PATH_TO_BE_CONFIGURED|pets|g" pets/faster_rcnn_resnet101_pets.config

Petsディレクトリに下記のファイルがあればOK

faster_rcnn_resnet101_pets.config
frozen_inference_graph.pb
graph.pbtxt
model.ckpt.data-00000-of-00001
model.ckpt.index
model.ckpt.meta
pet_faces_train.*
pet_faces_val.*
pet_label_map.pbtxt

##2-4. トレーニングを開始する

# From tensorflow/models/research/
 
PIPELINE_CONFIG_PATH=pets/faster_rcnn_resnet101_pets.config
MODEL_DIR=pets
NUM_TRAIN_STEPS=50000
NUM_EVAL_STEPS=2000
time python object_detection/model_main.py \
    --pipeline_config_path=${PIPELINE_CONFIG_PATH} \
    --model_dir=${MODEL_DIR} \
    --num_train_steps=${NUM_TRAIN_STEPS} \
    --num_eval_steps=${NUM_EVAL_STEPS} \
    --alsologtostderr

参考URL

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md
https://www.petitmonte.com/python/running_pets.html
https://qiita.com/IchiLab/items/fd99bcd92670607f8f9b

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1