Working object detection API installation

I am trying to train an object detection model using Tensorflow 2.16.1 in a Kaggle notebook. Here is my installation procedure:

!git clone https://github.com/tensorflow/models.git
%cd /kaggle/working/models/research
!protoc ./object_detection/protos/*.proto --python_out=.
!cp ./object_detection/packages/tf2/setup.py .
!python -m pip install --user
!python object_detection/builders/model_builder_tf2_test.py

When using this installation out of the box, the last line fails with
AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

which has to do with an incompatible keras version.

I have also tried other Tensorflow versions to fix the problem. However either installation fails instantly or I am getting the keras error message.

Has anyone a working Tensorflow 2 object detection API installation?

1 Like

Ok, I am now one step further thanks to this post:

I had to add these two lines on top of the installation:

import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"

Then the AttributeError disappears.

Now when trying to train my model with model_main_tf2.py, I am getting this error:

ImportError: cannot import name 'estimator' from 'tensorflow.compat.v1' (/root/.local/lib/python3.10/site-packages/tensorflow/_api/v2/compat/v1/__init__.py)

1 Like

This post suggests to go back to Tensorflow 2.15.0:

So I am adding these lines at the bottom of the installation:

!pip uninstall tensorflow --y
!pip install tensorflow==2.15.0

But now I am running into

AttributeError: module 'tensorflow.python.ops.control_flow_ops' has no attribute 'case'

Next attempt is to go further back to Tensorflow 2.13.0 as suggested here:

But now my GPUs are not detected anymore (which worked well with the later Tensorflow versions). So running the following lines

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

returns 0 intead of 2.

So it seems I am stuck. What a mess!

i have totally no idea of how to object detection, but i solved that problem by running tensorflow on docker

I managed to find a working installation but I had to do some code modifications to achieve this. Here is my configuration:

import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"

!git clone https://github.com/tensorflow/models.git
%cd /kaggle/working/Tensorflow/models/research
!protoc ./object_detection/protos/*.proto --python_out=.
!cp ./object_detection/packages/tf2/setup.py .
!python -m pip install --user .

!pip install tensorflow==2.15.0

I also had to modify tfexample_decoder.py as described in this post:

Now object detection is running fine with GPU.

I don’t know whether the issue has to do with the Kaggle environment but it would be great to use object detection out of the box with the latest Tensorflow version.