From 13bab8a91e17ccd87541b2f361ab60e8e38359d3 Mon Sep 17 00:00:00 2001 From: Jay Zhang <36183870+fatcat-z@users.noreply.github.com> Date: Tue, 16 Jan 2024 08:45:27 +0800 Subject: [PATCH] Complete changes to support latest tensorflow. (#2292) Signed-off-by: Jay Zhang --- README.md | 4 +- .../keras2onnx_application_tests.yml | 65 +++++++++---------- .../azure_pipelines/keras2onnx_unit_test.yml | 35 ++++------ .../onnxruntime_nightly_test.yml | 6 +- .../pretrained_model_test-matrix.yml | 43 +++++------- .../azure_pipelines/pretrained_model_test.yml | 4 +- .../trimmed_keras2onnx_application_tests.yml | 18 ++--- .../trimmed_keras2onnx_unit_test.yml | 22 +++---- ci_build/azure_pipelines/unit_test-matrix.yml | 12 ++-- ci_build/azure_pipelines/unit_test.yml | 4 +- tests/test_api.py | 1 - tests/test_tflite_utils.py | 3 +- tf2onnx/tf_loader.py | 60 +++++++++++------ 13 files changed, 138 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index e272ab7a9..deea4667f 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ The common issues we run into we try to document here [Troubleshooting Guide](Tr | Build Type | OS | Python | TensorFlow | ONNX opset | Status | | --- | --- | --- | --- | --- | --- | -| Unit Test - Basic | Linux, Windows | 3.7-3.10 | 1.15, 2.9-2.13 | 14-18 | [![Build Status](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_apis/build/status/unit_test?branchName=main)](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_build/latest?definitionId=16&branchName=main) | -| Unit Test - Full | Linux, Windows | 3.7-3.10 | 1.15, 2.9-2.13 | 14-18 | [![Build Status](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_apis/build/status/unit_test-matrix?branchName=main)](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_build/latest?definitionId=18&branchName=main) | | +| Unit Test - Basic | Linux, Windows | 3.7-3.10 | 1.15, 2.9-2.15 | 14-18 | [![Build Status](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_apis/build/status/unit_test?branchName=main)](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_build/latest?definitionId=16&branchName=main) | +| Unit Test - Full | Linux, Windows | 3.7-3.10 | 1.15, 2.9-2.15 | 14-18 | [![Build Status](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_apis/build/status/unit_test-matrix?branchName=main)](https://dev.azure.com/tensorflow-onnx/tensorflow-onnx/_build/latest?definitionId=18&branchName=main) | |
## Supported Versions diff --git a/ci_build/azure_pipelines/keras2onnx_application_tests.yml b/ci_build/azure_pipelines/keras2onnx_application_tests.yml index a81d586df..1db4dce93 100644 --- a/ci_build/azure_pipelines/keras2onnx_application_tests.yml +++ b/ci_build/azure_pipelines/keras2onnx_application_tests.yml @@ -8,18 +8,17 @@ jobs: vmImage: 'ubuntu-latest' strategy: matrix: - Python37-tf-1.15: - python.version: '3.7' # Max version that supports tf 1.15 - ONNX_PATH: onnx==1.14.1 # Max version that supports python 3.7 - INSTALL_KERAS: pip install keras==2.3.1 + Python310-tf2-2.13: + python.version: '3.10' + ONNX_PATH: onnx==1.15.0 + INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==1.15.0 - INSTALL_ORT: pip install onnxruntime==1.14.1 # Max version that supports python 3.7 + INSTALL_TENSORFLOW: pip install tensorflow==2.13.0 + INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_KERAS_RESNET: pip install keras-resnet - INSTALL_TRANSFORMERS: + INSTALL_TRANSFORMERS: pip install transformers==3.4.0 INSTALL_NUMPY: pip install numpy==1.19.0 - INSTALL_LEGACY: pip install h5py==2.9.0 - NIGHTLY_BUILD_TEST: python run_all_v2.py --exclude "test_keras_applications_v2.py" + NIGHTLY_BUILD_TEST: python run_all_v2.py Python39-tf2-2.12: python.version: '3.9' @@ -45,8 +44,8 @@ jobs: INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python38-tf2-2.10: - python.version: '3.8' + Python39-tf2-2.10: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: @@ -57,18 +56,6 @@ jobs: INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python310-tf2-oldest: - python.version: '3.10' - ONNX_PATH: onnx==1.15.0 - INSTALL_KERAS: - UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.9.0 - INSTALL_ORT: pip install onnxruntime==1.15.0 - INSTALL_KERAS_RESNET: pip install keras-resnet - INSTALL_TRANSFORMERS: pip install transformers==4.2.0 - INSTALL_NUMPY: - NIGHTLY_BUILD_TEST: python run_all_v2.py - steps: - template: 'templates/keras2onnx_application_tests.yml' parameters: @@ -93,32 +80,32 @@ jobs: INSTALL_LEGACY: pip install h5py==2.9.0 NIGHTLY_BUILD_TEST: python run_all_v2.py --exclude "test_keras_applications_v2.py" - Python310-tf2-2.13: + Python310-tf2-2.15: python.version: '3.10' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.13.0 + INSTALL_TENSORFLOW: pip install tensorflow==2.15.0 INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_KERAS_RESNET: pip install keras-resnet INSTALL_TRANSFORMERS: pip install transformers==4.2.0 INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python39-tf2-2.12: - python.version: '3.9' + Python310-tf2-2.14: + python.version: '3.10' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.12.0 - INSTALL_ORT: pip install onnxruntime==1.15.1 + INSTALL_TENSORFLOW: pip install tensorflow==2.14.0 + INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_KERAS_RESNET: pip install keras-resnet - INSTALL_TRANSFORMERS: pip install transformers==3.4.0 - INSTALL_NUMPY: pip install numpy==1.19.0 + INSTALL_TRANSFORMERS: pip install transformers==4.2.0 + INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python310-tf2-2.11: - python.version: '3.10' + Python39-tf2-2.11: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: @@ -141,6 +128,18 @@ jobs: INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py + Python38-tf2-2.9: + python.version: '3.8' + ONNX_PATH: onnx==1.15.0 + INSTALL_KERAS: + UNINSTALL_KERAS: + INSTALL_TENSORFLOW: pip install tensorflow==2.9.0 + INSTALL_ORT: pip install onnxruntime==1.15.1 + INSTALL_KERAS_RESNET: pip install keras-resnet + INSTALL_TRANSFORMERS: pip install transformers==4.12.0 + INSTALL_NUMPY: + NIGHTLY_BUILD_TEST: python run_all_v2.py + steps: - template: 'templates/keras2onnx_application_tests.yml' parameters: diff --git a/ci_build/azure_pipelines/keras2onnx_unit_test.yml b/ci_build/azure_pipelines/keras2onnx_unit_test.yml index f4523b80b..47adaf412 100644 --- a/ci_build/azure_pipelines/keras2onnx_unit_test.yml +++ b/ci_build/azure_pipelines/keras2onnx_unit_test.yml @@ -15,8 +15,8 @@ jobs: INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: - Python38-tf2-2.12: - python.version: '3.8' + Python39-tf2-2.12: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 TENSORFLOW_PATH: tensorflow==2.12.0 INSTALL_ORT: pip install onnxruntime==1.15.0 @@ -29,20 +29,13 @@ jobs: INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: - Python310-tf2-2.10: - python.version: '3.10' + Python38-tf2-2.10: + python.version: '3.8' ONNX_PATH: onnx==1.15.0 TENSORFLOW_PATH: tensorflow==2.10.0 INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: - Python310-tf2-oldest: - python.version: '3.10' - ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.9.0 - INSTALL_ORT: pip install onnxruntime==1.15.0 - INSTALL_NUMPY: - ############ Pure Keras Unit Tests ############ Keras-Py37-tf1.15.0: python.version: '3.7' # Max version that supports tf 1.15 @@ -63,9 +56,16 @@ jobs: strategy: matrix: ############ TF Keras Unit Tests ############ - Python310-tf2-2.13: + Python310-tf2-2.14: python.version: '3.10' ONNX_PATH: onnx==1.15.0 + TENSORFLOW_PATH: tensorflow==2.14.0 + INSTALL_ORT: pip install onnxruntime==1.15.0 + INSTALL_NUMPY: + + Python39-tf2-2.13: + python.version: '3.9' + ONNX_PATH: onnx==1.15.0 TENSORFLOW_PATH: tensorflow==2.13.0 INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: @@ -84,20 +84,13 @@ jobs: INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: - Python310-tf2-2.10: - python.version: '3.10' + Python38-tf2-2.10: + python.version: '3.8' ONNX_PATH: onnx==1.15.0 TENSORFLOW_PATH: tensorflow==2.10.0 INSTALL_ORT: pip install onnxruntime==1.15.0 INSTALL_NUMPY: - Python310-tf2-oldest: - python.version: '3.10' - ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.9.0 - INSTALL_ORT: pip install onnxruntime==1.15.0 - INSTALL_NUMPY: - ############ Pure Keras Unit Tests ############ Keras-Py37-tf1.15.0: python.version: '3.7' # Max version that supports tf 1.15 diff --git a/ci_build/azure_pipelines/onnxruntime_nightly_test.yml b/ci_build/azure_pipelines/onnxruntime_nightly_test.yml index bfeaa75ae..af22662f8 100644 --- a/ci_build/azure_pipelines/onnxruntime_nightly_test.yml +++ b/ci_build/azure_pipelines/onnxruntime_nightly_test.yml @@ -19,7 +19,7 @@ stages: parameters: platforms: ['linux', 'windows'] python_versions: ['3.8'] - tf_versions: ['2.9.0', '2.10.0', '2.13.0'] + tf_versions: ['2.9.0', '2.10.0', '2.15.0'] onnx_backends: {onnxruntime: ['nightly']} job: steps: @@ -30,7 +30,7 @@ stages: parameters: platforms: ['linux', 'windows'] python_versions: ['3.9'] - tf_versions: ['2.12.0'] + tf_versions: ['2.11.0', '2.12.0'] onnx_backends: {onnxruntime: ['nightly']} job: steps: @@ -41,7 +41,7 @@ stages: parameters: platforms: ['linux', 'windows'] python_versions: ['3.10'] - tf_versions: ['2.9.0', '2.13.0'] + tf_versions: ['2.9.0', '2.15.0'] onnx_backends: {onnxruntime: ['nightly']} job: steps: diff --git a/ci_build/azure_pipelines/pretrained_model_test-matrix.yml b/ci_build/azure_pipelines/pretrained_model_test-matrix.yml index c29903637..30af716a2 100755 --- a/ci_build/azure_pipelines/pretrained_model_test-matrix.yml +++ b/ci_build/azure_pipelines/pretrained_model_test-matrix.yml @@ -8,7 +8,7 @@ jobs: python_versions: ['3.7'] # Max version that supports tf 1.15 tf_versions: ['1.15.5'] onnx_versions: ['1.14.1'] # Max version that supports python 3.7 - onnx_opsets: ['18', '17', '16', '15'] + onnx_opsets: ['18', '17', '16', '15', '14'] onnx_backends: {onnxruntime: ['1.14.1']} # Max version that supports python 3.7 job: steps: @@ -16,10 +16,12 @@ jobs: - template: 'templates/job_generator.yml' parameters: - platforms: ['windows'] - python_versions: ['3.8'] - tf_versions: ['2.9.0'] - onnx_opsets: ['18', '17', '16', '15'] + platforms: ['linux'] + python_versions: ['3.7'] # Max version that supports tf 1.15 + tf_versions: ['1.15.5'] + onnx_versions: ['1.14.1'] # Max version that supports python 3.7 + onnx_opsets: ['18', '17', '16', '14'] + onnx_backends: {onnxruntime: ['1.14.1']} # Max version that supports python 3.7 job: steps: - template: 'pretrained_model_test.yml' @@ -28,27 +30,15 @@ jobs: parameters: platforms: ['windows'] python_versions: ['3.9'] - tf_versions: ['2.13.0'] - onnx_opsets: ['18', '17', '16', '15'] - job: - steps: - - template: 'pretrained_model_test.yml' - -- template: 'templates/job_generator.yml' - parameters: - platforms: ['linux', 'windows'] - python_versions: ['3.7'] # Max version that supports tf 1.15 - tf_versions: ['1.15.5'] - onnx_versions: ['1.14.1'] # Max version that supports python 3.7 - onnx_opsets: ['18', '17', '16', '14'] - onnx_backends: {onnxruntime: ['1.14.1']} # Max version that supports python 3.7 + tf_versions: ['2.9.0', '2.15.0'] + onnx_opsets: ['18', '17', '16', '15', '14'] job: steps: - template: 'pretrained_model_test.yml' - template: 'templates/job_generator.yml' parameters: - platforms: ['linux', 'windows'] + platforms: ['linux'] python_versions: ['3.8'] tf_versions: ['2.9.0'] onnx_opsets: ['17', '16', '14'] @@ -58,9 +48,9 @@ jobs: - template: 'templates/job_generator.yml' parameters: - platforms: ['linux', 'windows'] - python_versions: ['3.9'] - tf_versions: ['2.13.0'] + platforms: ['linux'] + python_versions: ['3.10'] + tf_versions: ['2.15.0'] onnx_opsets: ['17', '16', '14'] job: steps: @@ -69,7 +59,7 @@ jobs: - template: 'templates/job_generator.yml' parameters: platforms: ['linux', 'windows'] - python_versions: ['3.10'] + python_versions: ['3.8'] tf_versions: ['2.10.0'] job: steps: @@ -86,10 +76,9 @@ jobs: - template: 'templates/job_generator.yml' parameters: - platforms: ['linux'] + platforms: ['linux', 'windows'] python_versions: ['3.10'] - tf_versions: ['2.9.0', '2.13.0'] - onnx_opsets: ['17', '16', '14'] + tf_versions: ['2.14.0'] job: steps: - template: 'pretrained_model_test.yml' diff --git a/ci_build/azure_pipelines/pretrained_model_test.yml b/ci_build/azure_pipelines/pretrained_model_test.yml index 3ac548a81..a5b757daa 100644 --- a/ci_build/azure_pipelines/pretrained_model_test.yml +++ b/ci_build/azure_pipelines/pretrained_model_test.yml @@ -27,7 +27,7 @@ jobs: parameters: platforms: ['linux'] python_versions: ['3.9'] - tf_versions: ['2.13.0'] + tf_versions: ['2.15.0'] onnx_opsets: ['18', '15'] job: steps: @@ -37,7 +37,7 @@ jobs: parameters: platforms: ['linux'] python_versions: ['3.10'] - tf_versions: ['2.9.0', '2.13.0'] + tf_versions: ['2.9.0', '2.15.0'] onnx_opsets: ['18', '15'] job: steps: diff --git a/ci_build/azure_pipelines/trimmed_keras2onnx_application_tests.yml b/ci_build/azure_pipelines/trimmed_keras2onnx_application_tests.yml index f30d2f8e4..2b0e66dd5 100644 --- a/ci_build/azure_pipelines/trimmed_keras2onnx_application_tests.yml +++ b/ci_build/azure_pipelines/trimmed_keras2onnx_application_tests.yml @@ -19,7 +19,7 @@ jobs: INSTALL_TRANSFORMERS: INSTALL_NUMPY: pip install numpy==1.19.0 INSTALL_LEGACY: pip install h5py==2.9.0 - NIGHTLY_BUILD_TEST: python run_all.py --exclude "test_keras_applications_v2.py" + NIGHTLY_BUILD_TEST: python run_all_v2.py --exclude "test_keras_applications_v2.py" Python38-tf2-oldest: python.version: '3.8' @@ -33,36 +33,36 @@ jobs: INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python38-tf2-latest: + Python310-tf2-latest: python.version: '3.8' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.13.0 + INSTALL_TENSORFLOW: pip install tensorflow==2.15.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_KERAS_RESNET: pip install keras-resnet INSTALL_TRANSFORMERS: pip install transformers==4.2.0 INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python310-tf2-oldest: - python.version: '3.10' + Python38-tf2-oldest-plus-1: + python.version: '3.8' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.9.0 + INSTALL_TENSORFLOW: pip install tensorflow==2.10.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_KERAS_RESNET: pip install keras-resnet INSTALL_TRANSFORMERS: pip install transformers==4.2.0 INSTALL_NUMPY: NIGHTLY_BUILD_TEST: python run_all_v2.py - Python310-tf2-latest: - python.version: '3.10' + Python39-tf2-latest-minus-1: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 INSTALL_KERAS: UNINSTALL_KERAS: - INSTALL_TENSORFLOW: pip install tensorflow==2.13.0 + INSTALL_TENSORFLOW: pip install tensorflow==2.14.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_KERAS_RESNET: pip install keras-resnet INSTALL_TRANSFORMERS: pip install transformers==4.2.0 diff --git a/ci_build/azure_pipelines/trimmed_keras2onnx_unit_test.yml b/ci_build/azure_pipelines/trimmed_keras2onnx_unit_test.yml index 4f4aee22e..6fc84a41d 100644 --- a/ci_build/azure_pipelines/trimmed_keras2onnx_unit_test.yml +++ b/ci_build/azure_pipelines/trimmed_keras2onnx_unit_test.yml @@ -23,24 +23,24 @@ jobs: INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: - Python38-tf2-latest: - python.version: '3.8' + Python39-tf2-latest: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.13.0 + TENSORFLOW_PATH: tensorflow==2.15.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: - Python310-tf2-oldest: - python.version: '3.10' + Python39-tf2-oldest-plus-1: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.9.0 + TENSORFLOW_PATH: tensorflow==2.10.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: - Python310-tf2-latest: + Python310-tf2-latest-minus-1: python.version: '3.10' ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.13.0 + TENSORFLOW_PATH: tensorflow==2.14.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: @@ -79,10 +79,10 @@ jobs: INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: - Python38-tf2-latest: - python.version: '3.8' + Python39-tf2-latest: + python.version: '3.9' ONNX_PATH: onnx==1.15.0 - TENSORFLOW_PATH: tensorflow==2.13.0 + TENSORFLOW_PATH: tensorflow==2.15.0 INSTALL_ORT: pip install onnxruntime==1.16.3 INSTALL_NUMPY: diff --git a/ci_build/azure_pipelines/unit_test-matrix.yml b/ci_build/azure_pipelines/unit_test-matrix.yml index 7d51ae95a..25cc33596 100644 --- a/ci_build/azure_pipelines/unit_test-matrix.yml +++ b/ci_build/azure_pipelines/unit_test-matrix.yml @@ -21,7 +21,7 @@ stages: parameters: python_versions: ['3.9'] platforms: ['linux', 'windows'] - tf_versions: ['2.13.0'] + tf_versions: ['2.15.0'] onnx_opsets: ['17', '16', '14'] skip_tflite_tests: 'False' skip_tf_tests: 'False' @@ -60,8 +60,8 @@ stages: - template: 'templates/job_generator.yml' parameters: platforms: ['linux', 'windows'] - python_versions: ['3.8'] - tf_versions: ['2.12.0'] + python_versions: ['3.10'] + tf_versions: ['2.14.0'] job: steps: - template: 'unit_test.yml' @@ -71,7 +71,7 @@ stages: parameters: platforms: ['linux', 'windows'] python_versions: ['3.9'] - tf_versions: ['2.10.0', '2.11.0', '2.12.0'] + tf_versions: ['2.12.0'] job: steps: - template: 'unit_test.yml' @@ -80,8 +80,8 @@ stages: - template: 'templates/job_generator.yml' parameters: platforms: ['linux', 'windows'] - python_versions: ['3.10'] - tf_versions: ['2.9.0', '2.13.0'] + python_versions: ['3.8'] + tf_versions: ['2.10.0', '2.11.0'] job: steps: - template: 'unit_test.yml' diff --git a/ci_build/azure_pipelines/unit_test.yml b/ci_build/azure_pipelines/unit_test.yml index 2f55d80a7..0149196ed 100644 --- a/ci_build/azure_pipelines/unit_test.yml +++ b/ci_build/azure_pipelines/unit_test.yml @@ -21,7 +21,7 @@ stages: parameters: python_versions: ['3.9'] platforms: ['linux', 'windows'] - tf_versions: ['2.13.0'] + tf_versions: ['2.15.0'] onnx_opsets: ['18', '15'] skip_tflite_tests: 'False' skip_tf_tests: 'False' @@ -35,7 +35,7 @@ stages: parameters: python_versions: ['3.10'] platforms: ['linux'] - tf_versions: ['2.9.0', '2.13.0'] + tf_versions: ['2.9.0', '2.15.0'] onnx_opsets: ['18', '15'] skip_tflite_tests: 'False' skip_tf_tests: 'False' diff --git a/tests/test_api.py b/tests/test_api.py index 7c583e755..19544c733 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -80,7 +80,6 @@ def test_keras_api_large(self): @requires_custom_ops() @check_tf_min_version("1.15") @check_opset_min_version(11, "SparseToDense") - @skip_tf_versions(["2.1", "2.13"], "TF ['2.1', '2.13'] keras model doesn't work; table not initialized") def test_keras_hashtable(self): feature_cols = [ diff --git a/tests/test_tflite_utils.py b/tests/test_tflite_utils.py index e4125c938..4b194fda7 100644 --- a/tests/test_tflite_utils.py +++ b/tests/test_tflite_utils.py @@ -61,9 +61,8 @@ def func(a, b, c): parse_tflite_graph(tflite_graphs[0], opcodes_map, model, tensor_shapes_override=tensor_shapes) self.assertEqual(2, op_cnt['TFL_MUL']) self.assertEqual(1, op_cnt['TFL_ADD']) - self.assertEqual(1, op_cnt['TFL_FULLY_CONNECTED']) - self.assertEqual(1, attr_cnt['WeightsFormat']) + self.assertEqual(1, attr_cnt['PotScaleInt16']) self.assertEqual(names, inputs) self.assertEqual(output_names, outputs) diff --git a/tf2onnx/tf_loader.py b/tf2onnx/tf_loader.py index 7ced1a9c9..b8196404f 100644 --- a/tf2onnx/tf_loader.py +++ b/tf2onnx/tf_loader.py @@ -8,7 +8,6 @@ from packaging.version import Version import tensorflow as tf -import numpy as np from google.protobuf.message import DecodeError from tensorflow.core.framework import tensor_pb2 from tensorflow.core.protobuf import saved_model_pb2 @@ -481,6 +480,26 @@ def _get_hash_table_info_from_trackable(trackable, table_info, placeholder_to_table_info[removed_resource_to_placeholder[table_handle]] = new_table_info[0] +def _get_resources_from_captures(concrete_func, graph_captures): + resource_id_to_placeholder = {} + placeholder_to_resource = {} + keys_to_be_removed = [] + + # pylint: disable=protected-access + variable_handles = {id(v.handle) for v in concrete_func.graph.variables} + for k, v in list(graph_captures.items()): + val_tensor, name_tensor = v + if val_tensor.dtype == tf.resource and id(val_tensor) not in variable_handles: + resource_id_to_placeholder[id(val_tensor)] = name_tensor.name.split(':')[0] + placeholder_to_resource[name_tensor.name.split(':')[0]] = val_tensor + keys_to_be_removed.append(k) + for i in reversed(range(len(concrete_func._captured_inputs))): + if concrete_func._captured_inputs[i] is val_tensor: + concrete_func._captured_inputs.pop(i) + + return resource_id_to_placeholder, placeholder_to_resource, keys_to_be_removed + + def _remove_non_variable_resources_from_captures(concrete_func): """ Removes all non-variable resources (such as tables) from a function's captured inputs to prevent tf from @@ -494,22 +513,24 @@ def _remove_non_variable_resources_from_captures(concrete_func): if hasattr(concrete_func.graph, '_captures') and hasattr(concrete_func, '_captured_inputs'): graph_captures_copy = concrete_func.graph._captures.copy() func_captures_copy = concrete_func._captured_inputs.copy() - variable_handles = {id(v.handle) for v in concrete_func.graph.variables} - for k, v in list(concrete_func.graph._captures.items()): - val_tensor, name_tensor = v - if val_tensor.dtype == tf.resource and id(val_tensor) not in variable_handles: - resource_id_to_placeholder[id(val_tensor)] = name_tensor.name.split(':')[0] - placeholder_to_resource[name_tensor.name.split(':')[0]] = val_tensor - del concrete_func.graph._captures[k] - for i in reversed(range(len(concrete_func._captured_inputs))): - if concrete_func._captured_inputs[i] is val_tensor: - concrete_func._captured_inputs.pop(i) - elif val_tensor.dtype != tf.resource: - npval = val_tensor.numpy() - if not hasattr(npval, 'dtype'): - # Hack around a TF bug until PR is merged: https://github.com/tensorflow/tensorflow/pull/45610 - arr = np.array(npval) - val_tensor.numpy = lambda arr=arr: arr + + resource_id_to_placeholder, placeholder_to_resource, keys_to_be_removed = \ + _get_resources_from_captures(concrete_func, concrete_func.graph._captures) + for key in keys_to_be_removed: + del concrete_func.graph._captures[key] + elif hasattr(concrete_func.graph, 'function_captures') and hasattr(concrete_func, '_captured_inputs'): + # Since tensorflow 2.13.0, _captures has been removed and replaced with function_captures + graph_captures = {} + for k, v in concrete_func.graph.function_captures.by_val_external.items(): + graph_captures[k] = (v, concrete_func.graph.function_captures.by_val_internal[k]) + graph_captures_copy = graph_captures.copy() + func_captures_copy = concrete_func._captured_inputs.copy() + + resource_id_to_placeholder, placeholder_to_resource, keys_to_be_removed = \ + _get_resources_from_captures(concrete_func, graph_captures) + for key in keys_to_be_removed: + del concrete_func.graph.function_captures.by_val_internal[key] + del concrete_func.graph.function_captures.by_val_external[key] else: logger.warning( "Could not search for non-variable resources. Concrete function internal representation may have changed.") @@ -636,14 +657,13 @@ def from_saved_model(model_path, input_names, output_names, tag=None, def from_keras(model_path, input_names, output_names): """Load keras model - experimental for now.""" - from tensorflow.python import keras as _keras - from tensorflow.python.eager import context + from tensorflow import keras as _keras from tensorflow.python.keras.saving import saving_utils as _saving_utils # Handles Keras when Eager mode is enabled. custom_objects = None with tf.device("/cpu:0"): - if context.executing_eagerly(): + if tf.executing_eagerly(): _keras.backend.clear_session() _keras.backend.set_learning_phase(False) keras_model = _keras.models.load_model(model_path, custom_objects)