diff --git a/.github/actions/keras_application_test/action.yml b/.github/actions/keras_application_test/action.yml index e22d0ad8e..e7e725e03 100644 --- a/.github/actions/keras_application_test/action.yml +++ b/.github/actions/keras_application_test/action.yml @@ -58,6 +58,7 @@ runs: pip install numpy==1.19.0 else pip install transformers + pip install tf_keras pip install "numpy<2" fi @@ -71,5 +72,7 @@ runs: run: | python -c "import onnxruntime" pytest tests/keras2onnx_unit_tests --doctest-modules --junitxml=junit/test-results.xml + + export TF_USE_LEGACY_KERAS=True cd tests/keras2onnx_applications/nightly_build python run_all_v2.py diff --git a/.github/actions/keras_unit_test/action.yml b/.github/actions/keras_unit_test/action.yml index 4c78945f1..4ba0aea8b 100644 --- a/.github/actions/keras_unit_test/action.yml +++ b/.github/actions/keras_unit_test/action.yml @@ -36,6 +36,7 @@ runs: if [[ ${{ inputs.tf_version }} == 1.* ]]; then pip install numpy==1.19.0 else + pip install tf_keras pip install "numpy<2" fi @@ -48,6 +49,7 @@ runs: shell: bash if: runner.os == 'Linux' run: | + export TF_USE_LEGACY_KERAS=True python -c "import onnxruntime" python -c "import onnxconverter_common" pytest tests/keras2onnx_unit_tests --doctest-modules --junitxml=junit/test-results.xml diff --git a/.github/actions/unit_test/action.yml b/.github/actions/unit_test/action.yml index 00cb28386..ebcad6c80 100644 --- a/.github/actions/unit_test/action.yml +++ b/.github/actions/unit_test/action.yml @@ -46,6 +46,7 @@ runs: export TF2ONNX_SKIP_TFLITE_TESTS=${{ inputs.skip_tflite }} export TF2ONNX_SKIP_TFJS_TESTS=True export TF2ONNX_SKIP_TF_TESTS=False + export TF_USE_LEGACY_KERAS=True python -m pytest --cov=tf2onnx --cov-report=term --disable-pytest-warnings -r s tests --cov-append --junitxml=junit/test-results.xml ls @@ -58,5 +59,6 @@ runs: set TF2ONNX_SKIP_TFLITE_TESTS=${{ inputs.skip_tflite }} set TF2ONNX_SKIP_TFJS_TESTS=True set TF2ONNX_SKIP_TF_TESTS=False + set TF_USE_LEGACY_KERAS=True python -m pytest --cov=tf2onnx --cov-report=term --disable-pytest-warnings -r s tests --cov-append --junitxml=junit/test-results.xml ls diff --git a/.github/workflows/keras_application_test_ci.yml b/.github/workflows/keras_application_test_ci.yml index 1529cbc75..d0dd69c3d 100644 --- a/.github/workflows/keras_application_test_ci.yml +++ b/.github/workflows/keras_application_test_ci.yml @@ -48,10 +48,10 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Run Tests (Py310-TF2.15) + - name: Run Tests (Py310-TF2.18) uses: ./.github/actions/keras_application_test with: - tf_version: '2.15.0' + tf_version: '2.18.0' python_version: '3.10' ort_version: '1.16.3' onnx_version: '1.16.1' @@ -60,7 +60,7 @@ jobs: if: always() uses: actions/upload-artifact@v3 with: - name: Test Results (Py310-TF2.15-ubuntu) + name: Test Results (Py310-TF2.18-ubuntu) path: ./**/test-results-*.xml Test_py37_with_tf1_15: # Do not change this name because it is used in Ruleset of this repo. diff --git a/.github/workflows/keras_unit_test_ci.yml b/.github/workflows/keras_unit_test_ci.yml index b6702698c..0c2d2e090 100644 --- a/.github/workflows/keras_unit_test_ci.yml +++ b/.github/workflows/keras_unit_test_ci.yml @@ -47,10 +47,10 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Run Tests (Py310-TF2.15) + - name: Run Tests (Py310-TF2.18) uses: ./.github/actions/keras_unit_test with: - tf_version: '2.15.0' + tf_version: '2.18.0' python_version: '3.10' ort_version: '1.16.3' onnx_version: '1.16.1' @@ -59,7 +59,7 @@ jobs: if: always() uses: actions/upload-artifact@v3 with: - name: Test Results (Py310-TF2.15-ubuntu) + name: Test Results (Py310-TF2.18-ubuntu) path: ./**/test-results-*.xml Test_py37_with_tf1_15: # Do not change this name because it is used in Ruleset of this repo. diff --git a/.github/workflows/pretrained_model_test_ci.yml b/.github/workflows/pretrained_model_test_ci.yml index 96077d68a..34c31d624 100644 --- a/.github/workflows/pretrained_model_test_ci.yml +++ b/.github/workflows/pretrained_model_test_ci.yml @@ -51,11 +51,11 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Run Tests (Py310-TF2.15-18) + - name: Run Tests (Py310-TF2.18-18) uses: ./.github/actions/pretrained_model_test with: os: 'ubuntu-latest' - tf_version: '2.15.0' + tf_version: '2.18.0' python_version: '3.10' ort_version: '1.16.3' onnx_version: '1.16.1' @@ -66,7 +66,7 @@ jobs: if: always() uses: actions/upload-artifact@v3 with: - name: Test Results (Py310-TF2.15-18-ubuntu) + name: Test Results (Py310-TF2.18-18-ubuntu) path: ./**/test-results-*.xml Test_py37_with_tf1_15: # Do not change this name because it is used in 'publish-test-results' section below. diff --git a/.github/workflows/unit_test_ci.yml b/.github/workflows/unit_test_ci.yml index bdfae9277..46d03a9de 100644 --- a/.github/workflows/unit_test_ci.yml +++ b/.github/workflows/unit_test_ci.yml @@ -51,11 +51,11 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Run Tests (Py310-TF2.15-18) + - name: Run Tests (Py310-TF2.18-18) uses: ./.github/actions/unit_test with: os: 'ubuntu-latest' - tf_version: '2.15.0' + tf_version: '2.18.0' python_version: '3.10' ort_version: '1.16.3' onnx_version: '1.16.1' @@ -66,7 +66,7 @@ jobs: if: always() uses: actions/upload-artifact@v3 with: - name: Test Results (Py310-TF2.15-18-ubuntu) + name: Test Results (Py310-TF2.18-18-ubuntu) path: ./**/test-results-*.xml Extra_tests: diff --git a/tests/keras2onnx_unit_tests/conftest.py b/tests/keras2onnx_unit_tests/conftest.py index 758c31f7f..4745d1f86 100644 --- a/tests/keras2onnx_unit_tests/conftest.py +++ b/tests/keras2onnx_unit_tests/conftest.py @@ -13,6 +13,12 @@ K = keras.backend +def is_keras_3(): + if hasattr(keras, '__version__'): + return keras.__version__.startswith("3.") + + return False + @pytest.fixture(scope='function') def runner(): np.random.seed(42) @@ -25,10 +31,15 @@ def runner(): def runner_func(*args, **kwargs): return run_onnx_runtime(*args, model_files, **kwargs) - # Ensure Keras layer naming is reset for each function - K.reset_uids() - # Reset the TensorFlow session to avoid resource leaking between tests - K.clear_session() + if is_keras_3(): + import tf_keras + tf_keras.backend.reset_uids() + tf_keras.backend.clear_session() + else: + # Ensure Keras layer naming is reset for each function + K.reset_uids() + # Reset the TensorFlow session to avoid resource leaking between tests + K.clear_session() # Provide wrapped run_onnx_runtime function yield runner_func diff --git a/tf2onnx/convert.py b/tf2onnx/convert.py index 6ee66c096..f7ec42add 100644 --- a/tf2onnx/convert.py +++ b/tf2onnx/convert.py @@ -328,7 +328,8 @@ def _rename_duplicate_keras_model_names(model): IMPORTANT: model may be edited. Assign model.output_names to old_out_names to restore. """ old_out_names = None - if model.output_names and len(set(model.output_names)) != len(model.output_names): + if hasattr(model, "output_names") and model.output_names \ + and len(set(model.output_names)) != len(model.output_names): # In very rare cases, keras has a bug where it will give multiple outputs the same name # We must edit the model or the TF trace will fail old_out_names = model.output_names