Skip to content

Commit c0d864b

Browse files
committed
fix: resolve TensorFlow keras lazy loader recursion error
Set TF_USE_LEGACY_KERAS=1 to force TensorFlow 2.19.0 to use tf_keras instead of Keras 3.x if the environment has Keras 3.x installed. This work around prevents TensorFlow's lazy loader from infinitely recursing when trying to determine which Keras version to use. See: https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility. This fixes generate_test_models.py which was failing with: "RecursionError: maximum recursion depth exceeded in comparison" at line checking "if self._tfll_keras_version == 'keras_3'" BUG=#3125
1 parent 3200ccd commit c0d864b

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

tensorflow/lite/micro/testing/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ py_library(
8383
deps = [
8484
requirement("numpy"),
8585
requirement("tensorflow"),
86+
requirement("tf_keras"),
8687
],
8788
)
8889

tensorflow/lite/micro/testing/generate_test_models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
from __future__ import division
2727
from __future__ import print_function
2828

29+
import os
30+
# Workaround for TensorFlow 2.19.0 keras lazy loader issue. If Keras 3.0 is
31+
# installed, TensorFlow's lazy loader infinitely recurses when trying to
32+
# determine which Keras version to use. This forces TensorFlow to use the
33+
# one provided by package tf_keras. This must be set before importing
34+
# `tensorflow`. See https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility.
35+
os.environ['TF_USE_LEGACY_KERAS'] = '1'
36+
2937
from absl import app
3038
import numpy as np
3139
import tensorflow as tf

third_party/python_requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
bitarray
3030
tensorflow
31+
tf_keras
3132
twine
3233
numpy
3334
mako

third_party/python_requirements.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,9 @@ tensorflow==2.18.0 \
10341034
--hash=sha256:bfdd65ea7e064064283dd78d529dd621257ee617218f63681935fd15817c6286 \
10351035
--hash=sha256:ec4133a215c59314e929e7cbe914579d3afbc7874d9fa924873ee633fe4f71d0 \
10361036
--hash=sha256:f8c946df1cb384504578fac1c199a95322373b8e04abd88aa8ae01301df469ea
1037-
# via -r third_party/python_requirements.in
1037+
# via
1038+
# -r third_party/python_requirements.in
1039+
# tf-keras
10381040
tensorflow-io-gcs-filesystem==0.37.1 \
10391041
--hash=sha256:0df00891669390078a003cedbdd3b8e645c718b111917535fa1d7725e95cdb95 \
10401042
--hash=sha256:249c12b830165841411ba71e08215d0e94277a49c551e6dd5d72aab54fe5491b \
@@ -1057,6 +1059,10 @@ termcolor==2.5.0 \
10571059
--hash=sha256:37b17b5fc1e604945c2642c872a3764b5d547a48009871aea3edd3afa180afb8 \
10581060
--hash=sha256:998d8d27da6d48442e8e1f016119076b690d962507531df4890fcd2db2ef8a6f
10591061
# via tensorflow
1062+
tf-keras==2.18.0 \
1063+
--hash=sha256:c431d04027eef790fcd3261cf7fdf93eb74f3cb32e05078b57b7f5a54bd53262 \
1064+
--hash=sha256:ebf744519b322afead33086a2aba872245473294affd40973694f3eb7c7ad77d
1065+
# via -r third_party/python_requirements.in
10601066
tomli==2.0.2 \
10611067
--hash=sha256:2ebe24485c53d303f690b0ec092806a085f07af5a5aa1464f3931eec36caaa38 \
10621068
--hash=sha256:d46d457a85337051c36524bc5349dd91b1877838e2979ac5ced3e710ed8a60ed

0 commit comments

Comments
 (0)