深層学習 Ubuntu 20.04 に TensorFlow Object Detection API をインストール」 の TensorFlow Object Detection API を新しく構築した Ubuntu24.04 の環境(「深層学習 Ubuntu 24.04 に Tensorflow GPU をインストール」)でインストールしてみましたが、互換問題などがあり難航しました。

【互換問題の発生したバージョン】
Ubuntu 24.04
PyTorch 2.9.0
Python 3.12.3
TernsorFlow(GPU) 2.20.0
CUDA 12.6

この環境とバージョンで公式チュートリアル(https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html)の TensorFlow Object Detection API Installation 以降の手順で Object Detection API インストールをしましたが、最後のテストプログラム(model_builder_tf2_test.py)の実行でエラーが発生しました。

AttributeError: module ‘keras._tf_keras.keras.layers’ has no attribute ‘experimental’

このエラーの対処については下記のサイトを参考にしました。

https://github.com/tensorflow/tensorflow/issues/64349
how to solve this error ? : AttributeError: module ‘keras._tf_keras.keras.layers’ has no attribute ‘experimental’ #64349

他にも Numpy の互換問題もありましたので、とりえあずは Anaconda仮想環境, TensorFlow 2.16.0 未満の古いバージョンが良さそうです。(ただし、古いバージョンのインストールの影響でpytorch環境側に影響があった場合、pytorch側を再設定することになる可能性があります。)

【インストールしたバージョン】
Ubuntu 24.04
Anaconda 4.10.1
Python 3.9.25
TernsorFlow(GPU) 2.15.1
CUDA 13.0

基本的な手順はリンク先の公式チュートリアルに沿っています。
TensorFlow 2 Object Detection API tutorial
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html

1. Anaconda のインストール

TensorFlow Object Detection API の公式チュートリアルサイトのリンクからインストーラをダウンロードして実行

ダウンロードしたインストーラ(https://repo.anaconda.com/archive/
Anaconda3-2021.05-Linux-x86_64.sh

実行

~$ chmod 777 ./Anaconda3-2021.05-Linux-x86_64.sh
~$ ./Anaconda3-2021.05-Linux-x86_64.sh

ターミナル再起動

確認
~$ conda -V
conda 4.10.1

Anaconda仮想環境で python 3.9 を指定

~$ conda create -n tfod pip python=3.9
~$ conda activate tfod

確認
(tfod):~$ python -V
Python 3.9.25

以降は仮想環境での作業になります。

2. TensorFlow GPU インストール
仮想環境にインストール

(tfod):~/anaconda3/envs/tfod$ pip install "tensorflow[and-cuda]==2.15.1"

バージョンの確認
Python 実行
>>> import tensorflow as tf
>>> print(tf.__version__)
2.15.1

公式サイトの通りの確認を実行

(tfod):~/anaconda3/envs/tfod$ python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

実行結果--------------------
(中略)
2025-12-10 14:06:44.983239: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 7197 MB memory: -> device: 0, name: NVIDIA GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1
tf.Tensor(-712.41534, shape=(), dtype=float32)
---------------------------

正常にインストールされました。

3. GPU 設定
GPUのドライバ等については「深層学習 Ubuntu 24.04 に Tensorflow GPU をインストール」で設定済み

確認
Python 実行
>>> import tensorflow as tf
>>> tf.config.list_physical_devices('GPU')
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

4. TensorFlow Object Detection API インストール
4.1 TensorFlow Model Garden を取得

仮想環境で TensorFlow Model Garden を gitで取得

(tfod) :~/anaconda3/envs/tfod$ git clone https://github.com/tensorflow/models.git

確認
(tfod):~/anaconda3/envs/tfod$ ls models
AUTHORS CONTRIBUTING.md README.md docs research
CODEOWNERS ISSUES.md SECURITY.md official tensorflow_models
CODE_OF_CONDUCT.md LICENSE community orbit

4.2 Protobuf インストール
今回は、pip でインストールしてみました。

(tfod):~/anaconda3/envs/tfod$ python -m pip install --upgrade pip setuptools
(tfod):~/anaconda3/envs/tfod$ pip install protobuf

models/research/に移動して、チュートリアルに書いてあるコマンド実行。

(tfod):~/anaconda3/envs/tfod/models/research$ protoc object_detection/protos/*.proto --python_out=.

4.3 COCO API インストール

(tfod) :~/anaconda3/envs/tfod$ git clone https://github.com/cocodataset/cocoapi.git
(tfod) :~/anaconda3/envs/tfod$ cd cocoapi/PythonAPI
(tfod) :~/anaconda3/envs/tfod$ make

実行結果(makeでエラー)--------------------
(中略)
cc1: fatal error: pycocotools/_mask.c: そのようなファイルやディレクトリはありません
compilation terminated.
error: command '/usr/bin/gcc' failed with exit code 1
make: *** [Makefile:3: all] エラー 1
---------------------------

makeでエラーが発生しました。
エラー対処の参考
https://cocoinit23.com/no-such-file-or-directory-pycocotools-_mask-c/
cythonをインストール

(tfod):~/anaconda3/envs/tfod$ pip install cython

makeを再実行してエラーなし

pycocotoolsをコピー

(tfod) :~/anaconda3/envs/tfod/cocoapi/PythonAPI$ cp -r pycocotools ~/anaconda3/envs/tfod/models/research/

ターミナル再起動

4.4 Object Detection API インストール
models/researchに移動

(tfod) :~/anaconda3/envs/tfod/models/research$ cp object_detection/packages/tf2/setup.py .

バージョンを 2.16.0 未満にするために setup.py を下記のように変更しました。

元 'tf-models-official>=2.5.1',
修正 'tf-models-official>=2.5.1,<2.16.0',

インストール実行

(tfod) :~/anaconda3/envs/tfod/models/research$ python -m pip install .

4.5 インストールのテスト

(tfod) :~/anaconda3/envs/tfod/models/research$ python object_detection/builders/model_builder_tf2_test.py

実行結果--------------------
(中略)
INFO:tensorflow:time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s
I1210 14:53:06.966625 128757106296320 test_util.py:2574] time(__main__.ModelBuilderTF2Test.test_unknown_ssd_feature_extractor): 0.0s
[ OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 24 tests in 27.476s

OK (skipped=1)
---------------------------

インストール出来ました。

<<サンプル実行>>
以下のページのサンプルを JupyterNotebook で実行してみました。

Object Detection From TF2 Saved Model
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/auto_examples/plot_object_detection_saved_model.html#sphx-glr-auto-examples-plot-object-detection-saved-model-py

仮想環境に jupyter notebook インストール

(tfod) :~/anaconda3/envs/tfod$pip install jupyter

公式ページのサンプルを Jupyter notebook にコピーペーストして順次実行
(結果の画像を表示させるためサンプルソースに %matplotlib inline を追加しました)

実行結果
ObjectdetectionAPIinstallサンプル結果

正常に動作しました。

(2025/12/12 Shin Onda)