はじめに
CUDA10まではTensorFlow Object Detection APIを使用する際、TensorFlow1.xで環境を構築していたケースが多いと思います。一方で、最近のGPUは新しいCUDAバージョンしかサポートされていないため、古い環境で開発したモデルを再利用したいニーズがある場合は、新しいCUDA上に古いTensorFlow環境を構築する必要があります。
本記事では、CUDA12上にTensorFlow1.15とTF1 Object Detection APIの実行環境を構築する手順を示します。
目次
NVIDIAの提供するコンテナイメージの利用
CUDA12上に自前でTensorFlow1.15の環境を構築するのは手間がかかりますが、NVIDIAが提供するコンテナイメージに、以下のソフトウェアスタックで構成されるイメージがあるようなので、こちらを利用するのが一番簡単そうです。
nvcr.io/nvidia/tensorflow:23.03-tf1-py3
の構成
- Ubuntu20.04
- CUDA12.1
- CuDNN8.8.1.3
- TensorFlow1.15.5
- TensorRT8.5.3-1
- Python3.8.10
上記のコンテナイメージを取得して、コンテナを実行します。
# podman run -it -d --privileged --device nvidia.com/gpu=all --security-opt=label=disable --net=host --name tf1_test nvcr.io/nvidia/tensorflow:23.03-tf1-py3
# podman exec -it tf1_test bash
Object Detection APIの導入
まず、コンテナ上でprotobufパッケージをインストールします。
# apt update
# apt install -y protobuf-compiler
次に、TensorFlow Modelsレポジトリをクローンします。最新のレポジトリだとビルドに失敗するため、ビルド可能な古めのバージョンをチェックアウトします。
# git clone https://github.com/tensorflow/models.git
# git checkout 3d0ac19
TF1版のsetup.pyを実行して、パッケージをインストールします。
# cd research/
# protoc object_detection/protos/*.proto --python_out=.
# cp object_detection/packages/tf1/setup.py .
# python3 -m pip install .
動作チェックのスクリプトを実行して、結果がOKとなっていれば、Object Detection APIが利用可能な状態となっています。
# python3 object_detection/builders/model_builder_tf1_test.py
Running tests under Python 3.8.10: /usr/bin/python3
[ RUN ] ModelBuilderTF1Test.test_create_context_rcnn_from_config_with_params0 (True)
/usr/local/lib/python3.8/dist-packages/tensorflow_core/python/training/tracking/data_structures.py:669: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
if not isinstance(wrapped_dict, collections.Mapping):
[ OK ] ModelBuilderTF1Test.test_create_context_rcnn_from_config_with_params0 (True)
[ RUN ] ModelBuilderTF1Test.test_create_context_rcnn_from_config_with_params1 (False)
[ OK ] ModelBuilderTF1Test.test_create_context_rcnn_from_config_with_params1 (False)
[ RUN ] ModelBuilderTF1Test.test_create_experimental_model
[ OK ] ModelBuilderTF1Test.test_create_experimental_model
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_from_config_with_crop_feature0 (True)
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_from_config_with_crop_feature0 (True)
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_from_config_with_crop_feature1 (False)
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_from_config_with_crop_feature1 (False)
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_model_from_config_with_example_miner
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_model_from_config_with_example_miner
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
[ RUN ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
[ OK ] ModelBuilderTF1Test.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
[ RUN ] ModelBuilderTF1Test.test_create_rfcn_model_from_config
[ OK ] ModelBuilderTF1Test.test_create_rfcn_model_from_config
[ RUN ] ModelBuilderTF1Test.test_create_ssd_fpn_model_from_config
[ OK ] ModelBuilderTF1Test.test_create_ssd_fpn_model_from_config
[ RUN ] ModelBuilderTF1Test.test_create_ssd_models_from_config
[ OK ] ModelBuilderTF1Test.test_create_ssd_models_from_config
[ RUN ] ModelBuilderTF1Test.test_invalid_faster_rcnn_batchnorm_update
[ OK ] ModelBuilderTF1Test.test_invalid_faster_rcnn_batchnorm_update
[ RUN ] ModelBuilderTF1Test.test_invalid_first_stage_nms_iou_threshold
[ OK ] ModelBuilderTF1Test.test_invalid_first_stage_nms_iou_threshold
[ RUN ] ModelBuilderTF1Test.test_invalid_model_config_proto
[ OK ] ModelBuilderTF1Test.test_invalid_model_config_proto
[ RUN ] ModelBuilderTF1Test.test_invalid_second_stage_batch_size
[ OK ] ModelBuilderTF1Test.test_invalid_second_stage_batch_size
[ RUN ] ModelBuilderTF1Test.test_session
[ SKIPPED ] ModelBuilderTF1Test.test_session
[ RUN ] ModelBuilderTF1Test.test_unknown_faster_rcnn_feature_extractor
[ OK ] ModelBuilderTF1Test.test_unknown_faster_rcnn_feature_extractor
[ RUN ] ModelBuilderTF1Test.test_unknown_meta_architecture
[ OK ] ModelBuilderTF1Test.test_unknown_meta_architecture
[ RUN ] ModelBuilderTF1Test.test_unknown_ssd_feature_extractor
[ OK ] ModelBuilderTF1Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 21 tests in 0.060s
OK (skipped=1)
まとめ
比較的新しいCUDA12の環境に、TensorFlow1.15とTF1 Object Detection APIの実行環境を作成する手順を示しました。NVIDIAの提供するコンテナイメージを利用することで、簡単にTF1 Object Detection APIの実行環境が構築できることが分かりました。