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: open
  • Created 3 months ago
  • Comments: 15 (1 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!

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),