adap / flower

Flower: A Friendly Federated AI Framework
https://flower.ai
Apache License 2.0
4.98k stars 852 forks source link

`convert_to_tflite.py` in Android example error: `AttributeError: module 'keras.api._v1.keras.experimental' has no attribute 'export_saved_model'` #1911

Open SichangHe opened 1 year ago

SichangHe commented 1 year ago

Describe the bug

I tried running this script in the Android example but it failed: https://github.com/adap/flower/blob/main/examples/android/tflite_convertor/convert_to_tflite.py

The latest time these files have been modified seems to be #1001, @akhilmathurs. That PR seemed to fix #956, but somehow it did not work for me.

Please ask for more information as needed. Thanks.

Steps/Code to Reproduce

Running at /examples/android/tflite_convertor:

python3 convert_to_tflite.py

Or:

poetry run python3 convert_to_tflite.py

Initially, I ran with Python 3.11.3 installed with Homebrew and tensorflow-macos-2.12.0 installed with Pip. I later tried using Python 3.8.16 and Python 3.10.11 with tensorflow-macos-2.9.1 using Poetry, both failed with the same error message.

My machine is a M1 Mac running macOS 12.6.3.

Expected Results

The script should run successfully and produce 5 .tflite files.

Actual Results

From Python 3.11:

Traceback (most recent call last):
  File "tflite_convertor/convert_to_tflite.py", line 54, in <module>
    10, base_path, heads.KerasModelHead(head), optimizers.SGD(1e-3), train_batch_size=32
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "tflite_convertor/tfltransfer/heads/keras_model_head.py", line 44, in __init__
    tf.compat.v1.keras.experimental.export_saved_model(keras_model, saved_model_dir)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/tensorflow/python/util/module_wrapper.py", line 232, in _getattr
    attr = getattr(self._tfmw_wrapped_module, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'keras.api._v1.keras.experimental' has no attribute 'export_saved_model'

From Python 3.8 and Python 3.10:

Traceback (most recent call last):
  File "convert_to_tflite.py", line 54, in <module>
    10, base_path, heads.KerasModelHead(head), optimizers.SGD(1e-3), train_batch_size=32
  File "tflite_convertor/tfltransfer/heads/keras_model_head.py", line 44, in __init__
    tf.compat.v1.keras.experimental.export_saved_model(keras_model, saved_model_dir)
AttributeError: module 'keras.api._v1.keras.experimental' has no attribute 'export_saved_model'
SichangHe commented 1 year ago

I can verify that with tensorflow-cpu==2.9.2, it works on a x86_64 Windows machine. So, the problem is probably with Tensorflow changing their API between 2.9.2 and 2.12.

I could not, unfortunately, test with a lower tensorflow version on my Mac because I Numpy wouldn't install…