Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rajcr2 opened this issue Mar 23, 2024 · 18 comments
Assignees
Labels
comp:keras Keras related issues TF 2.15 For issues related to 2.15.x type:bug Bug

Comments

@Rajcr2
Copy link

Rajcr2 commented Mar 23, 2024

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

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@Rajcr2 Rajcr2 reopened this Mar 23, 2024
@Rajcr2
Copy link
Author

Rajcr2 commented Mar 23, 2024

after installing tf.nightly same issue is occuring.

from tensorflow.keras.layers.experimental import einsum
print("Done")

ModuleNotFoundError Traceback (most recent call last)
in <cell line: 1>()
----> 1 from tensorflow.keras.layers.experimental import einsum
2 print("Done")

ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental'

How to reslove this.

@DineshNeupane
Copy link

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

@Venkat6871 Venkat6871 added comp:keras Keras related issues TF 2.15 For issues related to 2.15.x labels Mar 26, 2024
@Venkat6871
Copy link

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!

@Venkat6871 Venkat6871 added the stat:awaiting response Status - Awaiting response from author label Mar 26, 2024
@nishii-2
Copy link

hey how do i ensure whether i'm importing the correct modules and classes in the script model_builder_tf2_test.py?

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Status - Awaiting response from author label Mar 31, 2024
@girraj96
Copy link

girraj96 commented Apr 5, 2024

Same issue. @DineshNeupane commenting keras also not working.

@nishii-2
Copy link

nishii-2 commented Apr 5, 2024 via email

@neverexz
Copy link

neverexz commented Apr 7, 2024

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

@SuryanarayanaY
Copy link
Collaborator

Hi @Rajcr2 ,

The issue seems to be a compatible issues related to TF/Keras version vs tf_models version. As Keras3 now built to support multiple backends there are some changes in design. Please provide more context and environment details like which TF/keras version,tf-models-official version etc for more details.

Please check setup.py of models repo for TF version compatibility.

@SuryanarayanaY SuryanarayanaY added the stat:awaiting response Status - Awaiting response from author label Apr 18, 2024
Copy link

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Apr 26, 2024
@google-ml-butler google-ml-butler bot removed stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author labels Apr 27, 2024
@Quado2
Copy link

Quado2 commented Apr 27, 2024

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

@HasithaKutala
Copy link

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

Hi im still facing this error can you please help me

@Quado2
Copy link

Quado2 commented Apr 30, 2024

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

Hi im still facing this error can you please help me

Can I see your code ?

@HasithaKutala
Copy link

yea can i text you in private ?

@zstreet87
Copy link
Contributor

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
tf.keras.experimental.CosineDecay)
AttributeError: module 'keras._tf_keras.keras' has no attribute 'experimental'

@devVegaAn
Copy link

I am also dealing with this problem.

@devVegaAn
Copy link

devVegaAn commented May 7, 2024

Ok so I found the solution for this.
TL;DR: there is more information about migrating to tf 2versions here: https://www.tensorflow.org/guide/migrate

I found that tensorflow version 2.16.1 changed keras module such as the current calls using 1.x fail.

I found the fix by searching similar issues and looking at tf docs.
old version tf 1.15 --> https://typeoverflow.com/developer/docs/tensorflow~1.15/keras/experimental/cosinedecay
tf.keras.experimental.CosineDecay(
initial_learning_rate, decay_steps, alpha=0.0, name=None
)

latest version 2.16.1 --> https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/schedules/CosineDecay
tf.keras.optimizers.schedules.CosineDecay(
initial_learning_rate,
decay_steps,
alpha=0.0,
name='CosineDecay',
warmup_target=None,
warmup_steps=0
)

@Rajcr2 Please, consider trying this and marking it as solved.

@HasithaKutala
Copy link

So, can we do object detection in latest version of tf?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues TF 2.15 For issues related to 2.15.x type:bug Bug
Projects
None yet
Development

No branches or pull requests