2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TensorFlow Model Garden 環境構築 備忘録

Posted at

概要

Object detectionのtransfer learningを試したくて、まず環境構築を行った。

最初は下記サイトのとおり行ったがProtoBuf関連でうまく行かず、

下記オフィシャルがあることに気づき、これも参照しながら作業した。

手こずったので備忘録に残す。

  • 実施時期: 2022年4月
  • OS: Ubuntu20.04LTS
  • Anaconda: Python3.9.12

ProtoBuf compilerのインストール

四苦八苦しながら最初にusr/localにProtoBufをインストールしておくべきだと気づいた。ただし既にProtoBufを使っている環境であればむしろ入れるのはリスクなので、protocコマンドでコンパイルできるか確認し、できなければインストールすればよし。
尚、pip install protobufでインストールできるProtoBufとは別物。
下記サイトからProtoBufの最新版(例えばprotoc-3.20.0-linux-x86_64.zip)をDownloadsフォルダにダウンロードする。

cd Downloads
sudo unzip -o protoc-3.20.0-linux-x86_64.zip -d /usr/local bin/protoc
sudo unzip -o protoc-3.20.0-linux-x86_64.zip -d /usr/local 'include/*'

.bashrcに下記のPATHを追加しsourceしておく。

export PATH="$PATH:$HOME/.local/bin"

これらは下記を参考にした。

Python環境

オフィシャルに従いAnacondaの仮想環境とする。尚、working directoryは下記とする。

Documents/tf_env1

また、conda仮想環境も同名のtf_env1とする。

conda create -n tf_env1 pip python=3.9
conda activate tf_env1
cd Documents/tf_env1    # ここに環境作成する

以降、この仮想環境内での作業となる。
Tensorflow2(以下TF)のインストールとGPUを認識できたか動作確認を行う。

pip install --ignore-installed --upgrade tensorflow==2.5.0
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

下記が表示されることを確認する。
CUDA等の設定は完了していることとする。

2022-04-10 14:13:27.184545: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.187504: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.187810: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.188251: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-04-10 14:13:27.188638: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.188931: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.189206: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.456498: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.456807: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.457071: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-04-10 14:13:27.457323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 5738 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3070, pci bus id: 0000:07:00.0, compute capability: 8.6
tf.Tensor(-837.38684, shape=(), dtype=float32)

TensorFlow Model GardenをDocuments/tf_env1直下にダウンロードする。

git clone https://github.com/tensorflow/models.git
tf_env1/
└─ models/
   ├─ community/
   ├─ official/
   ├─ orbit/
   ├─ research/
   └── ...

Object detection用のprotoファイルをはじめにインストールしたprotocでコンパイルする。

cd
cd Documents/tf_env1/models/research
protoc object_detection/protos/*.proto --python_out=.

COCO APIをtf_env1直下にダウンロードする。

cd
cd Documents/tf_env1
git clone https://github.com/cocodataset/cocoapi.git
tf_env1/
└─ models/
   ├─ community/
   ├─ official/
   ├─ orbit/
   ├─ research/
   └── ...
└─ cocoapi/
   ├─ common/
   ├─ LuaAPI/
   ├─ MatlabAPI/
   ├─ PythonAPI/
   └── ...

コンパイルする。

pip install cython
cd cocoapi/PythonAPI
make

makeで作成されたcocoapi/PythonAPI内のpycocotoolsをコピーする。

cp -r pycocotools ~/Documents/tf_env1/models/research/

working directoryに戻り、Object Detection APIをインストールする。

cd
cd Documents/tf_env1/models/research
cp object_detection/packages/tf2/setup.py .
python -m pip install .

下記がインストールされたらしい。

Successfully installed apache-beam-2.37.0 avro-python3-1.10.2 cloudpickle-2.0.0 colorama-0.4.4 contextlib2-21.6.0 crcmod-1.7 cycler-0.11.0 dill-0.3.1.1 dm-tree-0.1.6 docopt-0.6.2 fastavro-1.4.10 fonttools-4.32.0 gin-config-0.5.0 google-api-core-2.7.1 google-api-python-client-2.43.0 google-auth-httplib2-0.1.0 googleapis-common-protos-1.56.0 hdfs-2.7.0 httplib2-0.19.1 joblib-1.1.0 kaggle-1.5.12 keras-2.8.0 kiwisolver-1.4.2 libclang-13.0.0 lvis-0.5.3 lxml-4.8.0 matplotlib-3.5.1 numpy-1.21.5 oauth2client-4.1.3 object-detection-0.1 opencv-python-4.5.5.64 opencv-python-headless-4.5.5.64 orjson-3.6.7 packaging-21.3 pandas-1.4.2 pillow-9.1.0 portalocker-2.4.0 promise-2.3 proto-plus-1.20.3 psutil-5.9.0 py-cpuinfo-8.0.0 pyarrow-6.0.1 pycocotools-2.0.4 pydot-1.4.2 pymongo-3.12.3 pyparsing-2.4.7 python-dateutil-2.8.2 python-slugify-6.1.1 pytz-2022.1 pyyaml-5.4.1 regex-2022.3.15 sacrebleu-2.0.0 scikit-learn-1.0.2 scipy-1.8.0 sentencepiece-0.1.96 seqeval-1.2.2 tabulate-0.8.9 tensorflow-2.8.0 tensorflow-addons-0.16.1 tensorflow-datasets-4.5.2 tensorflow-hub-0.12.0 tensorflow-io-gcs-filesystem-0.24.0 tensorflow-metadata-1.7.0 tensorflow-model-optimization-0.7.2 tensorflow-text-2.8.1 tensorflow_io-0.24.0 text-unidecode-1.3 tf-estimator-nightly-2.8.0.dev2021122109 tf-models-official-2.8.0 tf-slim-1.1.0 threadpoolctl-3.1.0 tqdm-4.64.0 typeguard-2.13.3 uritemplate-4.1.1

research内のテストコードで動作確認する。

python object_detection/builders/model_builder_tf2_test.py

下記のように24個のtestが実行されOKで完了する。

2022-04-10 13:42:10.566262: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero

 【中略】

INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s
I0410 13:42:22.714373 140155632805696 test_util.py:2373] time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s
[       OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 24 tests in 12.120s

OK (skipped=1)

以上

2
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?