Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing optimizers to from_keras conversion function #1907

Merged
merged 3 commits into from
Apr 29, 2022

Conversation

shelkesagar29
Copy link
Contributor

_convert_common function in convert.py has optimizers parameter which allows user to select a subset of _optimizers from tf2onnx.optimizer while conversion is being done.

One way of using it to convert a TF saved model can be as follows.

from tf2onnx.optimizer import _optimizers
# Get all optimizers
updated_optimizers = copy.deepcopy(_optimizers)
# Remove two optimizers and then apply remaining optimizers during conversion
del updated_optimizers["q_dq_optimizer"]
del updated_optimizers["const_dequantize_optimizer"]

# 1. Load TensorFlow saved model
graph_def, inputs, outputs = tf_loader.from_saved_model(
    model_path=saved_model_path,
    input_names=None,
    output_names=None,
    tag="serve",
    signatures=["serving_default"]
)

# 2. Convert tf2onnx and save onnx file
model_proto, _ = convert._convert_common(
    graph_def,
    name=saved_model,
    opset=13,
    input_names=inputs,
    output_names=outputs,
    output_path=onnx_output_path,
    optimizers=updated_optimizers
)

However, currently there is no way to pass optimizers when using from_keras function to directly convert a keras model (without saving and loading). Converting from Keras directly is important since TF gives error many times for saving custom layers (this is especially true for QAT).

This PR adds optimizers parameter to from_keras function and passes it to _convert_common.

@shelkesagar29 shelkesagar29 changed the title Allow to select optimizers in from_keras conversion function Allow to pass optimizers in from_keras conversion function Apr 7, 2022
@shelkesagar29 shelkesagar29 changed the title Allow to pass optimizers in from_keras conversion function Allow passing optimizers to from_keras conversion function Apr 7, 2022
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Sagar Shelke seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

tf2onnx/convert.py Outdated Show resolved Hide resolved
@hwangdeyu
Copy link
Contributor

Thanks for contribution!
Could you sign off every commit message following DCO required?

Signed-off-by: Author Name <authoremail@example.com>

Signed-off-by: Sagar Shelke <sagshelke@nvidia.com>
@shelkesagar29 shelkesagar29 force-pushed the allow_optimizers_in_from_keras branch from 28750fd to 20f2803 Compare April 28, 2022 21:57
shelkesagar29 and others added 2 commits April 28, 2022 17:00
Copy link
Contributor

@hwangdeyu hwangdeyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@hwangdeyu hwangdeyu merged commit 4d8d59f into onnx:main Apr 29, 2022
hwangdeyu added a commit to hwangdeyu/tensorflow-onnx that referenced this pull request May 6, 2022
* Allow to select optimizers in from_keras conversion function

Signed-off-by: Sagar Shelke <sagshelke@nvidia.com>

* remove trailing whitespace

Signed-off-by: Sagar Shelke <sagshelke@nvidia.com>

Co-authored-by: Sagar Shelke <sagshelke@nvidia.com>
Co-authored-by: Deyu Huang <deyhuang@microsoft.com>
hwangdeyu added a commit that referenced this pull request May 6, 2022
* Allow to select optimizers in from_keras conversion function

Signed-off-by: Sagar Shelke <sagshelke@nvidia.com>

* remove trailing whitespace

Signed-off-by: Sagar Shelke <sagshelke@nvidia.com>

Co-authored-by: Sagar Shelke <sagshelke@nvidia.com>
Co-authored-by: Deyu Huang <deyhuang@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants