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?
Ok, I am now one step further thanks to this post:
According to the documentation you need to set that flag in your python file before importing tensorflow os.environ[“TF_USE_LEGACY_KERAS”] = “1”
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)
This post suggests to go back to Tensorflow 2.15.0:
opened 02:21PM - 13 Mar 24 UTC
closed 01:48AM - 02 Apr 24 UTC
stat:awaiting response
type:bug
stale
comp:apis
TF 2.15
### Issue type
Bug
### Have you reproduced the bug with TensorFlow Nightly?
Y… es
### Source
source
### TensorFlow version
2.15.0
### Custom code
Yes
### OS platform and distribution
_No response_
### Mobile device
_No response_
### Python version
_No response_
### Bazel version
_No response_
### GCC/compiler version
_No response_
### CUDA/cuDNN version
_No response_
### GPU model and memory
_No response_
### Current behavior?
How to solve this issue - ImportError: cannot import name 'estimator' from 'tensorflow.compat.v1' (/root/.local/lib/python3.10/site-packages/tensorflow/_api/v2/compat/v1/__init__.py)
anyone ?
### Standalone code to reproduce the issue
```shell
2024-03-13 14:13:23.953994: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-13 14:13:25.304466: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
File "/content/drive/MyDrive/tf_CrimeDetection/workspace/CCTV_New/model_main_tf2.py", line 31, in <module>
from object_detection import model_lib_v2
File "/usr/local/lib/python3.10/dist-packages/object_detection/model_lib_v2.py", line 30, in <module>
from object_detection import inputs
File "/usr/local/lib/python3.10/dist-packages/object_detection/inputs.py", line 24, in <module>
from tensorflow.compat.v1 import estimator as tf_estimator
ImportError: cannot import name 'estimator' from 'tensorflow.compat.v1' (/root/.local/lib/python3.10/site-packages/tensorflow/_api/v2/compat/v1/__init__.py)
```
### Relevant log output
_No response_
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:
Hey! I got solved when i degraded from 2.15.0–> 2.13.0…And, i got no issues after degrading in errors…
But, when i ran the training command its taking a lot of time to load at certain point.I cant understand whats going wrong…its not showing next process i’ve waited for 1hour.
It not showing anything before loss metrics .
kindly,help me in resolving this issue… And,my CPU is 81% utilised…
is GPU required for model training i’ve Iris Xe graphic card.
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!
ssr765
April 21, 2024, 11:50pm
#7
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:
python, tensorflow
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.