【Tensorflow v2:物体検出】Object Detection APIのインストール方法

スポンサーリンク
【Tensorflow v2物体検出】Object Detection APIのインストール方法AIを作ってみる
この記事を読んで分かること
  • Object Detection APIのインストール方法
  • Object Detection APIの動作確認方法

 

実行環境

  • Ubuntu18.04
  • RTX3070
  • Nvidia-driver 460.91
  • CUDA11.2
  • tensorflow 2.5

Object Detection API公式チュートリアルに則って、インストールを行っていきます。

 

Object Detection APIとは

Object Detection APIとは、

tensorflowで物体検出をするためのOSS

です。

 

Googleが開発しているOSSで、物体検出モデルの構築・学習・デプロイを簡単に行うことができます。

ソースコードはこちらにあり、ここからダウンロードしてインストールしていきます。

では早速インストールしていきましょう。

 

Tensorflowの環境確認

まず、必要なライブラリをインポートします。

import tensorflow as tf
from google.protobuf import text_format

%matplotlib inline

 

インストールされているtensorflowのバージョンを確認します。

バージョンが表示されることを確認して、tensorflowが動作する環境であることを確認できました。

print(tf.__version__)
実行結果
2.5.0

 

Object detection APIのインストール

ソースコードのダウンロード

まずはgit cloneしてソースコードをダウンロードします。

! rm -rf ./models && git clone https://github.com/tensorflow/models.git
Cloning into ‘models’…
remote: Enumerating objects: 65213, done.
remote: Counting objects: 100% (718/718), done.
remote: Compressing objects: 100% (154/154), done.52% (81/154)
remote: Total 65213 (delta 563), reused 713 (delta 563), pack-reused 64495
Receiving objects: 100% (65213/65213), 575.46 MiB | 2.76 MiB/s, done.
Resolving deltas: 100% (45623/45623), done.
実行結果
Cloning into 'models'...
remote: Enumerating objects: 65213, done.
remote: Counting objects: 100% (718/718), done.
remote: Compressing objects: 100% (154/154), done.52% (81/154)
remote: Total 65213 (delta 563), reused 713 (delta 563), pack-reused 64495
Receiving objects: 100% (65213/65213), 575.46 MiB | 2.76 MiB/s, done.
Resolving deltas: 100% (45623/45623), done.

 

Protobufライブラリのセットアップ

続いて、Object Detection APIを使えるように必要なライブラリをセットアップしていきます。

Object Detection APIはモデルと学習パラメータを構築するためにProtobufライブラリを使用しています。

Protobufライブラリをインストールしてからライブラリのセットアップをしてください。

! cd models/research && \
protoc object_detection/protos/*.proto --python_out=.

 

Object Detection APIのインストール

Object Detection APIインストールします。

! cd models/research && \
cp object_detection/packages/tf2/setup.py . && \
python -m pip install --use-feature=2020-resolver .
実行結果
︙
Successfully built object-detection
Installing collected packages: object-detection
  Attempting uninstall: object-detection
    Found existing installation: object-detection 0.1
    Uninstalling object-detection-0.1:
      Successfully uninstalled object-detection-0.1
Successfully installed object-detection-0.1

 

Object detection APIの動作確認

Object Detection APIが正しくインストールされたことを確認します。

動作確認用のプログラムが用意されているので、pythonで実行します。

!python3 ./models/research/object_detection/builders/model_builder_test.py
実行結果

[ OK ] ModelBuilderTF2Test.test_create_ssd_models_from_config [ RUN ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update): 0.0s I1101 11:01:14.552159 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update): 0.0s [ OK ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update [ RUN ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold): 0.0s I1101 11:01:14.553363 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold): 0.0s [ OK ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold [ RUN ] ModelBuilderTF2Test.test_invalid_model_config_proto INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_invalid_model_config_proto): 0.0s I1101 11:01:14.553707 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_invalid_model_config_proto): 0.0s [ OK ] ModelBuilderTF2Test.test_invalid_model_config_proto [ RUN ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_invalid_second_stage_batch_size): 0.0s I1101 11:01:14.554795 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_invalid_second_stage_batch_size): 0.0s [ OK ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size [ RUN ] ModelBuilderTF2Test.test_session [ SKIPPED ] ModelBuilderTF2Test.test_session [ RUN ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor): 0.0s I1101 11:01:14.555807 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor): 0.0s [ OK ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor [ RUN ] ModelBuilderTF2Test.test_unknown_meta_architecture INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_unknown_meta_architecture): 0.0s I1101 11:01:14.556086 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_unknown_meta_architecture): 0.0s [ OK ] ModelBuilderTF2Test.test_unknown_meta_architecture [ RUN ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s I1101 11:01:14.556818 139656160487232 test_util.py:2103] time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s [ OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor ---------------------------------------------------------------------- Ran 24 tests in 15.472s OK (skipped=1)

OK (skipped=1)と表示されたら正常にインストールできています。

 

以上でObject detection APIのインストールは完了です。

 

まとめ

公式チュートリアルに則って、Object Detection APIのインストールを行いました。

Object Detection APIは、tensorflowで物体検出をするためのOSSです。

物体検出モデルの構築・学習・デプロイを簡単に行う事ができるのでぜひ使ってみてください。

 

参考資料

Installation — TensorFlow 2 Object Detection API tutorial documentation

 

コメント

タイトルとURLをコピーしました