tensorflow: how to solve this error ? : AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 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?

Got this Error while installing object detection tensorflow api.

command : !python object_detection/builders/model_builder_tf2_test.py

Standalone code to reproduce the issue

2024-03-23 08:19:24.008197: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
  File "/content/tf_CrimeDetection/models/research/object_detection/builders/model_builder_tf2_test.py", line 24, in <module>
    from object_detection.builders import model_builder
  File "/usr/local/lib/python3.10/dist-packages/object_detection/builders/model_builder.py", line 26, in <module>
    from object_detection.builders import hyperparams_builder
  File "/usr/local/lib/python3.10/dist-packages/object_detection/builders/hyperparams_builder.py", line 27, in <module>
    from object_detection.core import freezable_sync_batch_norm
  File "/usr/local/lib/python3.10/dist-packages/object_detection/core/freezable_sync_batch_norm.py", line 20, in <module>
    class FreezableSyncBatchNorm(tf.keras.layers.experimental.SyncBatchNormalization
AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 35 (3 by maintainers)

Most upvoted comments

workaround is to limit the tensorflow version and comment out keras from object detection>packages>tf2>setup.py as:

 'tf-models-official >=2.5.1, <2.16.0',
    'tensorflow_io',
    #'keras',

try this, helped for me pip install tensorflow==2.13.1

Hi @Rajcr2 , -Sorry for the delay, Make sure you have the latest versions of TensorFlow and TensorFlow Object Detection API installed.

pip install --upgrade tensorflow
pip install --upgrade tf-models-official

-Verify the versions of TensorFlow and Keras you have installed to ensure compatibility.

import tensorflow as tf
print(tf.__version__)
print(tf.keras.__version__)

-In your script model_builder_tf2_test.py, ensure that you’re importing the correct modules and classes. -Ensure that your environment is correctly set up with the necessary libraries and dependencies. You might need to restart your environment or kernel after making changes. Thank you!

shits not working bro

I am also dealing with this problem.

pip install tensorflow==2.13.1

If you are using the latest version of tensorflow, you will likey encounter this error. Tensorflow no longer have ‘experimental’ module under the ‘keras.layers’ module. You can access the preprocessing libraries directly under tf.keras.layers instead of tf.keras.layers.experimental.preprocessing. For example, you can do:

tf.keras.layers.RandomRotation(0.15), tf.keras.layers.RandomFlip(‘horizontal’), tf.keras.layers.RandomZoom(0.12), tf.keras.layers.RandomContrast(0.12),

instead of:

tf.keras.layers.experimental.preprocessing.RandomRotation(0.15), tf.keras.layers.experimental.preprocessing.RandomFlip(‘horizontal’), tf.keras.layers.experimental.preprocessing.RandomZoom(0.12), tf.keras.layers.experimental.preprocessing.RandomContrast(0.12),

Same issue. @DineshNeupane commenting keras also not working.

Running this on a Mac, getting the same error with Tensorflow 2.19.0. Have tried limiting my tf-models-official version to <2.16.0 and commenting out keras on a fresh install. Using the latest version of tf-models-official fails, so does version 2.7.2. Any ideas on how I can solve this, below is the traceback from running model_builder_tf2_test.py?

AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

Follow the below steps, it works for me on Google Colab Run each of the code on separate cels

import re with open(‘/content/models/research/object_detection/packages/tf2/setup.py’) as f: s = f.read()

with open(‘/content/models/research/setup.py’, ‘w’) as f: s = re.sub(‘tf-models-official>=2.5.1’, ‘tf-models-official==2.16.0’, s) f.write(s)

!pip install pyyaml !pip install /content/models/research/ !pip install tensorflow==2.13.1

Best of luck

excuse me, it seems I also have a similar problem, namely as follows: AttributeError Traceback (most recent call last) in <cell line: 22>() 20 from object_detection.utils import visualization_utils as viz_utils 21 from object_detection.utils import colab_utils —> 22 from object_detection.builders import model_builder 23 24 get_ipython().run_line_magic(‘matplotlib’, ‘inline’) 2 frames /usr/local/lib/python3.10/dist-packages/object_detection/core/freezable_sync_batch_norm.py in 18 19 —> 20 classes FreezableSyncBatchNorm(tf.keras.layers.experimental.SyncBatchNormalization 21 ): 22 “”"Sync Batch normalization layer (Ioffe and Szegedy, 2014). AttributeError: module ‘keras._tf_keras.keras.layers’ has no attribute ‘experimental’ is there someone who can help me? Screenshot 2024-06-21 101925

pip install tensorflow==2.13.1

does this version support cuda. because even 2.15 is out of support and can’t support cuda drivers.

no

excuse me, it seems I also have a similar problem, namely as follows:

AttributeError Traceback (most recent call last) in <cell line: 22>() 20 from object_detection.utils import visualization_utils as viz_utils 21 from object_detection.utils import colab_utils —> 22 from object_detection.builders import model_builder 23 24 get_ipython().run_line_magic(‘matplotlib’, ‘inline’)

2 frames /usr/local/lib/python3.10/dist-packages/object_detection/core/freezable_sync_batch_norm.py in 18 19 —> 20 classes FreezableSyncBatchNorm(tf.keras.layers.experimental.SyncBatchNormalization 21 ): 22 “”"Sync Batch normalization layer (Ioffe and Szegedy, 2014).

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

is there someone who can help me?

Screenshot 2024-06-21 101925

pip install tensorflow==2.13.1

Also seeing this. This is for automation so would rather the fix come from upstream than any manual patch so I’ll be watching - appreciate the effort!

File “models/official/modeling/optimization/lr_schedule.py”, line 89, in <module> tf.keras.experimental.CosineDecay) AttributeError: module ‘keras._tf_keras.keras’ has no attribute ‘experimental’