Skip to content

Commit d66a43c

Browse files
committed
fix reset_uids
Signed-off-by: xadupre <[email protected]>
2 parents 14f04ff + b75fe78 commit d66a43c

File tree

9 files changed

+40
-19
lines changed

9 files changed

+40
-19
lines changed

.github/actions/keras_application_test/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ runs:
5858
pip install numpy==1.19.0
5959
else
6060
pip install transformers
61+
pip install tf_keras
6162
pip install "numpy<2"
6263
fi
6364
@@ -71,5 +72,7 @@ runs:
7172
run: |
7273
python -c "import onnxruntime"
7374
pytest tests/keras2onnx_unit_tests --doctest-modules --junitxml=junit/test-results.xml
75+
76+
export TF_USE_LEGACY_KERAS=True
7477
cd tests/keras2onnx_applications/nightly_build
7578
python run_all_v2.py

.github/actions/keras_unit_test/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ runs:
3636
if [[ ${{ inputs.tf_version }} == 1.* ]]; then
3737
pip install numpy==1.19.0
3838
else
39+
pip install tf_keras
3940
pip install "numpy<2"
4041
fi
4142
@@ -48,6 +49,7 @@ runs:
4849
shell: bash
4950
if: runner.os == 'Linux'
5051
run: |
52+
export TF_USE_LEGACY_KERAS=True
5153
python -c "import onnxruntime"
5254
python -c "import onnxconverter_common"
5355
pytest tests/keras2onnx_unit_tests --doctest-modules --junitxml=junit/test-results.xml

.github/actions/unit_test/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ runs:
4646
export TF2ONNX_SKIP_TFLITE_TESTS=${{ inputs.skip_tflite }}
4747
export TF2ONNX_SKIP_TFJS_TESTS=True
4848
export TF2ONNX_SKIP_TF_TESTS=False
49+
export TF_USE_LEGACY_KERAS=True
4950
python -m pytest --cov=tf2onnx --cov-report=term --disable-pytest-warnings -r s tests --cov-append --junitxml=junit/test-results.xml
5051
ls
5152
@@ -58,5 +59,6 @@ runs:
5859
set TF2ONNX_SKIP_TFLITE_TESTS=${{ inputs.skip_tflite }}
5960
set TF2ONNX_SKIP_TFJS_TESTS=True
6061
set TF2ONNX_SKIP_TF_TESTS=False
62+
set TF_USE_LEGACY_KERAS=True
6163
python -m pytest --cov=tf2onnx --cov-report=term --disable-pytest-warnings -r s tests --cov-append --junitxml=junit/test-results.xml
6264
ls

.github/workflows/keras_application_test_ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ jobs:
4848
- name: Checkout code
4949
uses: actions/checkout@v4
5050

51-
- name: Run Tests (Py310-TF2.15)
51+
- name: Run Tests (Py310-TF2.18)
5252
uses: ./.github/actions/keras_application_test
5353
with:
54-
tf_version: '2.15.0'
54+
tf_version: '2.18.0'
5555
python_version: '3.10'
5656
ort_version: '1.16.3'
5757
onnx_version: '1.16.1'
@@ -60,7 +60,7 @@ jobs:
6060
if: always()
6161
uses: actions/upload-artifact@v4
6262
with:
63-
name: Test Results (Py310-TF2.15-ubuntu)
63+
name: Test Results (Py310-TF2.18-ubuntu)
6464
path: ./**/test-results-*.xml
6565

6666
Test_py37_with_tf1_15: # Do not change this name because it is used in Ruleset of this repo.

.github/workflows/keras_unit_test_ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ jobs:
4747
- name: Checkout code
4848
uses: actions/checkout@v4
4949

50-
- name: Run Tests (Py310-TF2.15)
50+
- name: Run Tests (Py310-TF2.18)
5151
uses: ./.github/actions/keras_unit_test
5252
with:
53-
tf_version: '2.15.0'
53+
tf_version: '2.18.0'
5454
python_version: '3.10'
5555
ort_version: '1.16.3'
5656
onnx_version: '1.16.1'
@@ -59,7 +59,7 @@ jobs:
5959
if: always()
6060
uses: actions/upload-artifact@v4
6161
with:
62-
name: Test Results (Py310-TF2.15-ubuntu)
62+
name: Test Results (Py310-TF2.18-ubuntu)
6363
path: ./**/test-results-*.xml
6464

6565
Test_py37_with_tf1_15: # Do not change this name because it is used in Ruleset of this repo.

.github/workflows/pretrained_model_test_ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ jobs:
5151
- name: Checkout code
5252
uses: actions/checkout@v4
5353

54-
- name: Run Tests (Py310-TF2.15-18)
54+
- name: Run Tests (Py310-TF2.18-18)
5555
uses: ./.github/actions/pretrained_model_test
5656
with:
5757
os: 'ubuntu-latest'
58-
tf_version: '2.15.0'
58+
tf_version: '2.18.0'
5959
python_version: '3.10'
6060
ort_version: '1.16.3'
6161
onnx_version: '1.16.1'
@@ -66,7 +66,7 @@ jobs:
6666
if: always()
6767
uses: actions/upload-artifact@v4
6868
with:
69-
name: Test Results (Py310-TF2.15-18-ubuntu)
69+
name: Test Results (Py310-TF2.18-18-ubuntu)
7070
path: ./**/test-results-*.xml
7171

7272
Test_py37_with_tf1_15: # Do not change this name because it is used in 'publish-test-results' section below.

.github/workflows/unit_test_ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ jobs:
5151
- name: Checkout code
5252
uses: actions/checkout@v4
5353

54-
- name: Run Tests (Py310-TF2.15-18)
54+
- name: Run Tests (Py310-TF2.18-18)
5555
uses: ./.github/actions/unit_test
5656
with:
5757
os: 'ubuntu-latest'
58-
tf_version: '2.15.0'
58+
tf_version: '2.18.0'
5959
python_version: '3.10'
6060
ort_version: '1.16.3'
6161
onnx_version: '1.16.1'
@@ -66,7 +66,7 @@ jobs:
6666
if: always()
6767
uses: actions/upload-artifact@v4
6868
with:
69-
name: Test Results (Py310-TF2.15-18-ubuntu)
69+
name: Test Results (Py310-TF2.18-18-ubuntu)
7070
path: ./**/test-results-*.xml
7171

7272
Extra_tests:

tests/keras2onnx_unit_tests/conftest.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
K = keras.backend
1414

1515

16+
def is_keras_3():
17+
if hasattr(keras, '__version__'):
18+
return keras.__version__.startswith("3.")
19+
20+
return False
21+
1622
@pytest.fixture(scope='function')
1723
def runner():
1824
np.random.seed(42)
@@ -25,12 +31,19 @@ def runner():
2531
def runner_func(*args, **kwargs):
2632
return run_onnx_runtime(*args, model_files, **kwargs)
2733

28-
# Ensure Keras layer naming is reset for each function
29-
if hasattr(K, "reset_uids"):
30-
# see https://github.com/onnx/tensorflow-onnx/issues/2370
31-
K.reset_uids()
32-
# Reset the TensorFlow session to avoid resource leaking between tests
33-
K.clear_session()
34+
if is_keras_3():
35+
import tf_keras
36+
if hasattr(K, "reset_uids"):
37+
# see https://github.com/onnx/tensorflow-onnx/issues/2370
38+
K.reset_uids()
39+
tf_keras.backend.clear_session()
40+
else:
41+
# Ensure Keras layer naming is reset for each function
42+
if hasattr(K, "reset_uids"):
43+
# see https://github.com/onnx/tensorflow-onnx/issues/2370
44+
K.reset_uids()
45+
# Reset the TensorFlow session to avoid resource leaking between tests
46+
K.clear_session()
3447

3548
# Provide wrapped run_onnx_runtime function
3649
yield runner_func

tf2onnx/convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ def _rename_duplicate_keras_model_names(model):
328328
IMPORTANT: model may be edited. Assign model.output_names to old_out_names to restore.
329329
"""
330330
old_out_names = None
331-
if model.output_names and len(set(model.output_names)) != len(model.output_names):
331+
if hasattr(model, "output_names") and model.output_names \
332+
and len(set(model.output_names)) != len(model.output_names):
332333
# In very rare cases, keras has a bug where it will give multiple outputs the same name
333334
# We must edit the model or the TF trace will fail
334335
old_out_names = model.output_names

0 commit comments

Comments
 (0)