From dfd089be7b5ae4e9b9ede0f6a629be92d035f658 Mon Sep 17 00:00:00 2001 From: Wenbing Li Date: Tue, 28 Jan 2020 16:38:51 -0800 Subject: [PATCH] add tf2onnx wrapper test. (#367) * add tf2onnx wrapper test. * install tf 1.15.0 * install tf2onnx --- .azure-pipelines/linux-CI-nightly.yml | 3 +++ .azure-pipelines/linux-conda-CI.yml | 8 ++++---- .azure-pipelines/win32-CI-nightly.yml | 8 ++++---- .azure-pipelines/win32-conda-CI.yml | 3 +++ tests/utils/test_utils.py | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.azure-pipelines/linux-CI-nightly.yml b/.azure-pipelines/linux-CI-nightly.yml index e17f012c..95dec279 100644 --- a/.azure-pipelines/linux-CI-nightly.yml +++ b/.azure-pipelines/linux-CI-nightly.yml @@ -36,7 +36,10 @@ jobs: conda install -c conda-forge numpy conda install -c conda-forge cmake python -m pip install $(ONNX_PATH) + test '$(python.version)' != '2.7' && python -m pip install tensorflow-cpu==1.15.0 + python -m pip install tf2onnx python -m pip install git+https://github.com/microsoft/onnxconverter-common + python -m pip install git+https://github.com/onnx/keras-onnx python -m pip install -r requirements.txt python -m pip install -r requirements-dev.txt python -m pip install $(ORT_PATH) diff --git a/.azure-pipelines/linux-conda-CI.yml b/.azure-pipelines/linux-conda-CI.yml index 4d086adf..20703794 100644 --- a/.azure-pipelines/linux-conda-CI.yml +++ b/.azure-pipelines/linux-conda-CI.yml @@ -50,10 +50,10 @@ jobs: conda install -c conda-forge numpy conda install -c conda-forge cmake pip install $(ONNX_PATH) - git clone https://github.com/microsoft/onnxconverter-common - cd onnxconverter-common - pip install -e . - cd .. + test '$(python.version)' != '2.7' && python -m pip install tensorflow-cpu==1.15.0 + python -m pip install tf2onnx + python -m pip install git+https://github.com/microsoft/onnxconverter-common + python -m pip install git+https://github.com/onnx/keras-onnx pip install -r requirements.txt pip install -r requirements-dev.txt test '$(python.version)' != '2.7' && pip install $(ONNXRT_PATH) diff --git a/.azure-pipelines/win32-CI-nightly.yml b/.azure-pipelines/win32-CI-nightly.yml index babbbd4e..524b196b 100644 --- a/.azure-pipelines/win32-CI-nightly.yml +++ b/.azure-pipelines/win32-CI-nightly.yml @@ -37,10 +37,10 @@ jobs: python -m pip install --upgrade pip numpy echo Test numpy installation... && python -c "import numpy" pip install %COREML_PATH% %ONNX_PATH% - git clone https://github.com/microsoft/onnxconverter-common - cd onnxconverter-common - pip install -e . - cd .. + python -m pip install tensorflow-cpu==1.15.0 + python -m pip install tf2onnx + python -m pip install git+https://github.com/microsoft/onnxconverter-common + python -m pip install git+https://github.com/onnx/keras-onnx echo Test onnxconverter-common installation... && python -c "import onnxconverter_common" pip install -r requirements.txt pip install -r requirements-dev.txt diff --git a/.azure-pipelines/win32-conda-CI.yml b/.azure-pipelines/win32-conda-CI.yml index 75e56c00..1e378429 100644 --- a/.azure-pipelines/win32-conda-CI.yml +++ b/.azure-pipelines/win32-conda-CI.yml @@ -56,7 +56,10 @@ jobs: python -m pip install --upgrade pip numpy echo Test numpy installation... && python -c "import numpy" python -m pip install %COREML_PATH% %ONNX_PATH% + python -m pip install tensorflow-cpu==1.15.0 + python -m pip install tf2onnx python -m pip install git+https://github.com/microsoft/onnxconverter-common + python -m pip install git+https://github.com/onnx/keras-onnx echo Test onnxconverter-common installation... && python -c "import onnxconverter_common" python -m pip install -r requirements.txt python -m pip install -r requirements-dev.txt diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index b73b0679..36a1cddc 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -2,7 +2,9 @@ Tests utilities. """ import os +import six import unittest +import onnxmltools from onnxmltools.utils import load_model, save_model from onnxmltools.utils import set_model_version, set_model_domain, set_model_doc_string @@ -53,5 +55,21 @@ def test_set_docstring_blank(self): self.assertEqual(onnx_model.doc_string, "") +@unittest.skipIf(six.PY2, "Keras and Tensorflow converter not support python 2.x") +class TestWrapper(unittest.TestCase): + + def test_keras_with_tf2onnx(self): + import keras2onnx + from keras2onnx.proto import keras + from keras2onnx.proto.tfcompat import is_tf2 + if not is_tf2: # tf2onnx is not available for tensorflow 2.0 yet. + model = keras.Sequential() + model.add(keras.layers.Dense(units=4, input_shape=(10,), activation='relu')) + model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['binary_accuracy']) + graph_def = keras2onnx.export_tf_frozen_graph(model) + onnx_model = onnxmltools.convert_tensorflow(graph_def, **keras2onnx.build_io_names_tf2onnx(model)) + self.assertTrue(len(onnx_model.graph.node) > 0) + + if __name__ == "__main__": unittest.main()