AttributeError: module 'keras.api._v2.keras.mixed_precision' has no attribute 'experimental'

Hi, I am a beginner in using tensoflow and I want to build up an object detection android app. I need to build my own dataset to detect the object. Therefore, I would like to use pip install tflite-model-maker to make it.

However, when I run the code spec = model_spec.get(‘efficientdet_lite2’), there is an attribute error occured which says the module ‘keras.api._v2.keras.mixed_precision’ has no attribute ‘experimental’.

I have tried to reinstall different version of tensorflow, including 2.5.0, 2.8.0 and 2.9.1, but it is still in vein. I have also tried to reinstall keras and tflite-model-maker but it is still in vein again.

All of the instruction that I follow depends on Google Colab , I am really frustrated with that situation. Can anyone help me? Thank you.

The tf.keras.mixed_precision.experimental API has been removed. The non-experimental symbols under tf.keras.mixed_precision have been available since TensorFlow 2.4 and should be used instead.

Please uninstall all existing installations of Tensorflow and keras . Only go with Tensorflow 2.9 or nightly in a new environment( Don’t install keras again in the same environment). Thank you.

Yes, The experimental version was removed depreciating the experimental functions.
Those functionality is now exposed with another API.
Go to the source file where these lines are defined
policy = tf.keras.mixed_precision.experimental.Policy(precision)
tf.keras.mixed_precision.experimental.set_policy(policy)

MODIFY these lines to
policy = tf.keras.mixed_precision.Policy(precision)
tf.keras.mixed_precision.set_global_policy(policy)

USE:
precision = ‘mixed_float16’ if you have GPU
OR
precision =‘float32’ if you have CPU.

It worked for me while training object detector model with tflite-model-maker using efficientnet backbone.

**Hi, **
I change 2 lines and I have the same problem.

policy = tf.keras.mixed_precision.Policy(precision)*
234 tf.keras.mixed_precision.set_global_policy(policy)*

AttributeError: module ‘keras.api._v2.keras.mixed_precision’ has no attribute ‘experimental’

It’OK after reboot!

Thank a lot.

I’m a but confused on how you access the source file, any help would be appreciated

@ Bjarne_Ungemach,

Check the error description and trace the line where the error comes from traceback

E.g.: File "/.../..../..../..../..../.../.../XXXXXXX.py", line XX, in <module> 'XXXX':

The file path above with the line number gives you the exact source of the problem. Thank you!

Hello,

Here’s the adapted and clarified message for the technical context of computer science, translated into English:

Today is May 4th, 2023, and the problem that has been persisting since 2020 is still ongoing. I am using TensorFlow 2.8.4 and Python 3.8.16 for training, and the process goes smoothly, resulting in my tflite model. However, when I try to use it on Android, I keep getting the same error:

java.lang.AssertionError: Error occurred when initializing ObjectDetector: Output tensor at index 0 is expected to have 3 dimensions, found 2.
at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)

Additionally, when I try to switch to a virtual environment with Conda, using python=3.8, tensorflow=2.5, and tflite_model_maker=0.3.2, I still encounter an error with Keras:
Error:

policy = tf.keras.mixed_precision.experimental.Policy(precision)
AttributeError: module 'keras.api._v2.keras.mixed_precision' has no attribute 'experimental'

I am wondering how I can make my tflite model usable on Android, and I am asking for help if anyone has a solution so that I can create my models with the latest versions of the various technologies.

Thank you in advance!

@Sidath_GUEYE,

AttributeError: module 'keras.api._v2.keras.mixed_precision' has no attribute 'experimental'

This issue is due to Tensorflow removed tf.keras.mixed_precision.experimental API.

java.lang.AssertionError: Error occurred when initializing ObjectDetector: Output tensor at index 0 is expected to have 3 dimensions, found 2.
at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)

This issue will be resolved by using the latest version of the Android app example for TensorFlow.

Thank you!

1 Like

Thank you! resovelved also!